2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 23:18:43 +08:00
Files
panel/pkg/apploader/apploader.go
2024-11-11 01:07:37 +08:00

55 lines
970 B
Go

// Package apploader 面板应用加载器
package apploader
import (
"fmt"
"log"
"sync"
"github.com/go-chi/chi/v5"
"github.com/TheTNB/panel/pkg/types"
)
var apps sync.Map
func Register(app *types.App) {
if _, ok := apps.Load(app.Slug); ok {
log.Fatalf("app %s already exists", app.Slug)
}
apps.Store(app.Slug, app)
}
func Get(slug string) (*types.App, error) {
if app, ok := apps.Load(slug); ok {
return app.(*types.App), nil
}
return nil, fmt.Errorf("app %s not found", slug)
}
func All() []*types.App {
var list []*types.App
apps.Range(func(_, app any) bool {
if p, ok := app.(*types.App); ok {
list = append(list, p)
}
return true
})
// 排序
/*slices.SortFunc(list, func(a, b *types.App) int {
return cmp.Compare(a.Order, b.Order)
})*/
return list
}
func Boot(r chi.Router) {
apps.Range(func(_, app any) bool {
if p, ok := app.(*types.App); ok {
r.Route(fmt.Sprintf("/%s", p.Slug), p.Route)
}
return true
})
}