config 알려주는 api 추가
This commit is contained in:
@ -220,6 +220,18 @@ func (sl *servicelist) add(s *serviceDescription) {
|
|||||||
atomic.StorePointer(&sl.services, unsafe.Pointer(&next))
|
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 {
|
func (sl *servicelist) get(sn any) *serviceDescription {
|
||||||
ptr := atomic.LoadPointer(&sl.services)
|
ptr := atomic.LoadPointer(&sl.services)
|
||||||
src := *(*map[string]*serviceDescription)(ptr)
|
src := *(*map[string]*serviceDescription)(ptr)
|
||||||
@ -553,7 +565,6 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.Println("RegisterHandlers...")
|
logger.Println("RegisterHandlers...")
|
||||||
|
|
||||||
mg.services.init(allServices)
|
mg.services.init(allServices)
|
||||||
for _, service := range allServices {
|
for _, service := range allServices {
|
||||||
if service.Closed {
|
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.Handle(common.MakeHttpHandlerPattern(prefix, service.ServiceCode, "/"), service)
|
||||||
}
|
}
|
||||||
|
|
||||||
serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "api", "/"), mg.api)
|
serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "api/"), mg.api)
|
||||||
serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "query", "/"), mg.query)
|
serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "query/"), mg.query)
|
||||||
|
|
||||||
configraw, _ := json.Marshal(mg.maingateConfig)
|
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) {
|
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")
|
apitoken := r.Header.Get("MG-X-API-TOKEN")
|
||||||
if len(apitoken) == 0 {
|
if len(apitoken) == 0 {
|
||||||
logger.Println("MG-X-API-TOKEN is missing")
|
logger.Println("MG-X-API-TOKEN is missing")
|
||||||
@ -576,14 +599,18 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, exists := mg.apiTokenToService.get(apitoken)
|
code, exists := mg.apiTokenToService.get(apitoken)
|
||||||
if !exists {
|
if !exists {
|
||||||
logger.Println("MG-X-API-TOKEN is invalid :", apitoken)
|
logger.Println("MG-X-API-TOKEN is invalid :", apitoken)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
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 {
|
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"))
|
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"))
|
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, "request_login_url", AuthPlatformGoogle), mg.platform_google_get_login_url)
|
||||||
serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "authorize", AuthPlatformGoogle), mg.platform_google_authorize)
|
serveMux.HandleFunc(common.MakeHttpHandlerPattern(prefix, "authorize", AuthPlatformGoogle), mg.platform_google_authorize)
|
||||||
|
|||||||
Reference in New Issue
Block a user