Refactor: Load UserTemplates from embedded yaml file (#575)

* Refactor: Load UserTemplates from embedded yaml file

* feat: add version field to UserTemplates

* refactor: use shell script to fetch frontends

* chore: add *-dist to .gitignore

* refactor: rename to FrontendTemplates

BREAKING CHANGE: This commit changes the `user_templates` filed
in the communication json between backend and the admin-frontend.

Keep user config.yml `user_template` filed.
This commit is contained in:
Moraxyc
2024-12-10 21:57:20 +08:00
committed by GitHub
parent 96cbec9dd3
commit 8f8a30c02c
9 changed files with 98 additions and 70 deletions

View File

@@ -0,0 +1,17 @@
- path: "admin-dist"
name: "OfficialAdmin"
repository: "https://github.com/nezhahq/admin-frontend"
author: "nezhahq"
version: "v1.1.1"
isadmin: true
- path: "user-dist"
name: "Official"
repository: "https://github.com/hamster1963/nezha-dash-v1"
author: "hamster1963"
version: "v1.2.5"
- path: "nazhua-dist"
name: "Nazhua"
repository: "https://github.com/hi2shark/nazhua"
author: "hi2hi"
version: "v0.4.10"
community: true

View File

@@ -1,10 +1,12 @@
package singleton
import (
_ "embed"
"log"
"time"
"github.com/patrickmn/go-cache"
"gopkg.in/yaml.v3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
@@ -15,27 +17,17 @@ import (
var Version = "debug"
var (
Conf *model.Config
Cache *cache.Cache
DB *gorm.DB
Loc *time.Location
UserTemplates = []model.UserTemplate{
{
Path: "user-dist",
Name: "Official",
Repository: "https://github.com/hamster1963/nezha-dash",
Author: "hamster1963",
}, {
Path: "nazhua-dist",
Name: "Nazhua",
Repository: "https://github.com/hi2shark/nazhua",
Author: "hi2hi",
Community: true,
},
}
Conf *model.Config
Cache *cache.Cache
DB *gorm.DB
Loc *time.Location
FrontendTemplates []model.FrontendTemplate
DashboardBootTime = uint64(time.Now().Unix())
)
//go:embed frontend-templates.yaml
var frontendTemplatesYAML []byte
func InitTimezoneAndCache() {
var err error
Loc, err = time.LoadLocation(Conf.Location)
@@ -56,10 +48,18 @@ func LoadSingleton() {
initDDNS()
}
// InitFrontendTemplates 从内置文件中加载FrontendTemplates
func InitFrontendTemplates() {
err := yaml.Unmarshal(frontendTemplatesYAML, &FrontendTemplates)
if err != nil {
panic(err)
}
}
// InitConfigFromPath 从给出的文件路径中加载配置
func InitConfigFromPath(path string) {
Conf = &model.Config{}
err := Conf.Read(path, UserTemplates)
err := Conf.Read(path, FrontendTemplates)
if err != nil {
panic(err)
}