package server import ( "embed" "encoding/json" "io/fs" "net/http" "os" "path/filepath" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/api" _ "embed" ) var ( apps []App //go:embed apps/** appsFS embed.FS ) func init() { if err := initializeAppsList(); err != nil { panic(errors.WithStack(err)) } } func initializeAppsList() error { files, err := fs.Glob(appsFS, "apps/*") if err != nil { return errors.WithStack(err) } for _, f := range files { stat, err := fs.Stat(appsFS, f) if err != nil { return errors.WithStack(err) } if !stat.IsDir() { continue } rawManifest, err := fs.ReadFile(appsFS, filepath.Join(f, "manifest.json")) if err != nil { if errors.Is(err, os.ErrNotExist) { continue } return errors.WithStack(err) } var app App if err := json.Unmarshal(rawManifest, &app); err != nil { return errors.WithStack(err) } app.ID = filepath.Base(f) apps = append(apps, app) } return nil } func (s *Server) handleDefaultApp(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/apps/"+s.defaultWebApp, http.StatusTemporaryRedirect) } type AppsResponse struct { Apps []App } type App struct { ID string `json:"id"` Title map[string]string `json:"title"` Description map[string]string `json:"description"` Icon string `json:"icon"` } func (s *Server) handleApps(w http.ResponseWriter, r *http.Request) { api.DataResponse(w, http.StatusOK, struct { DefaultApp string `json:"defaultApp"` Apps []App `json:"apps"` }{ DefaultApp: s.defaultWebApp, Apps: apps, }) }