diff --git a/core/maingate.go b/core/maingate.go index d82bbb0..2eb9821 100644 --- a/core/maingate.go +++ b/core/maingate.go @@ -220,6 +220,18 @@ func (sl *servicelist) add(s *serviceDescription) { atomic.StorePointer(&sl.services, unsafe.Pointer(&next)) } +func (sl *servicelist) getByCode(code string) *serviceDescription { + ptr := atomic.LoadPointer(&sl.services) + src := *(*map[string]*serviceDescription)(ptr) + + for _, v := range src { + if v.ServiceCode == code { + return v + } + } + return nil +} + func (sl *servicelist) get(sn any) *serviceDescription { ptr := atomic.LoadPointer(&sl.services) src := *(*map[string]*serviceDescription)(ptr) @@ -553,7 +565,6 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu } logger.Println("RegisterHandlers...") - mg.services.init(allServices) for _, service := range allServices { if service.Closed { @@ -564,11 +575,23 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu serveMux.Handle(common.MakeHttpHandlerPattern(prefix, service.ServiceCode, "/"), service) } - serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "api", "/"), mg.api) - serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "query", "/"), mg.query) + serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "api/"), mg.api) + serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "query/"), mg.query) configraw, _ := json.Marshal(mg.maingateConfig) + var convertedConfig map[string]any + if err := json.Unmarshal(configraw, &convertedConfig); err != nil { + return err + } + serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "config"), func(w http.ResponseWriter, r *http.Request) { + defer func() { + s := recover() + if s != nil { + logger.Error(s) + } + }() + apitoken := r.Header.Get("MG-X-API-TOKEN") if len(apitoken) == 0 { logger.Println("MG-X-API-TOKEN is missing") @@ -576,14 +599,18 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu return } - _, exists := mg.apiTokenToService.get(apitoken) + code, exists := mg.apiTokenToService.get(apitoken) if !exists { logger.Println("MG-X-API-TOKEN is invalid :", apitoken) w.WriteHeader(http.StatusBadRequest) return } - w.Write(configraw) + service := mg.services.getByCode(code) + convertedConfig["divisions"] = service.Divisions + + enc := json.NewEncoder(w) + enc.Encode(convertedConfig) }) if err := os.MkdirAll("static", os.ModePerm); err != nil { @@ -592,10 +619,10 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu } fsx := http.FileServer(http.Dir("./console")) - serveMux.Handle(common.MakeHttpHandlerPattern(prefix, "console", "/"), http.StripPrefix("/console/", fsx)) + serveMux.Handle(common.MakeHttpHandlerPattern(prefix, "console/"), http.StripPrefix("/console/", fsx)) ssx := http.FileServer(http.Dir("./static")) - serveMux.Handle(common.MakeHttpHandlerPattern(prefix, "static", "/"), http.StripPrefix("/static/", ssx)) + serveMux.Handle(common.MakeHttpHandlerPattern(prefix, "static/"), http.StripPrefix("/static/", ssx)) serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "request_login_url", AuthPlatformGoogle), mg.platform_google_get_login_url) serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "authorize", AuthPlatformGoogle), mg.platform_google_authorize)