2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 04:22:33 +08:00
Files
panel/pkg/apploader/apploader.go
2026-01-04 03:31:34 +08:00

60 lines
920 B
Go

package apploader
import (
"reflect"
"slices"
"strings"
"sync"
"github.com/go-chi/chi/v5"
"github.com/acepanel/panel/pkg/types"
)
var apps sync.Map
type Loader struct{}
func (r *Loader) Add(app ...types.App) {
for item := range slices.Values(app) {
slug := getSlug(item)
apps.Store(slug, item)
}
}
func (r *Loader) Register(mux chi.Router) {
apps.Range(func(key, value any) bool {
app := value.(types.App)
mux.Route("/"+key.(string), app.Route)
return true
})
}
func Slugs() []string {
var slugs []string
apps.Range(func(key, value any) bool {
slugs = append(slugs, key.(string))
return true
})
return slugs
}
func getSlug(app types.App) string {
if app == nil {
return ""
}
t := reflect.TypeOf(app)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
pkgPath := t.PkgPath()
if pkgPath == "" {
return ""
}
parts := strings.Split(pkgPath, "/")
return parts[len(parts)-1]
}