noauth와 devflag 분리

This commit is contained in:
2023-06-23 17:58:41 +09:00
parent 79e00de9f6
commit d3332f530f
3 changed files with 57 additions and 53 deletions

View File

@ -433,40 +433,7 @@ func whitelistKey(email string) string {
func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMux, prefix string) error {
var allServices []*serviceDescription
if *noauth {
host, _ := os.Hostname()
addrs, err := net.InterfaceAddrs()
if err != nil {
return err
}
ipaddr := "127.0.0.1"
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && ipnet.IP.IsPrivate() {
ipaddr = ipnet.IP.String()
}
}
}
empty := serviceDescription{
ServiceDescriptionSummary: ServiceDescriptionSummary{
ServiceCode: "000000000000",
},
Divisions: map[string]*Division{
host: {
DivisionForUser: DivisionForUser{
Priority: 0,
State: DivisionState_FullOpen,
},
Url: fmt.Sprintf("http://%s/warehouse", ipaddr),
},
},
}
empty.prepare(mg)
allServices = append(allServices, &empty)
} else if err := mg.mongoClient.AllAs(CollectionService, &allServices, options.Find().SetReturnKey(false)); err != nil {
if err := mg.mongoClient.AllAs(CollectionService, &allServices, options.Find().SetReturnKey(false)); err != nil {
return err
}
@ -481,6 +448,34 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
Id: primitive.NewObjectID(),
},
}
if *devflag {
host, _ := os.Hostname()
addrs, err := net.InterfaceAddrs()
if err != nil {
return err
}
ipaddr := "127.0.0.1"
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && ipnet.IP.IsPrivate() {
ipaddr = ipnet.IP.String()
}
}
}
empty.Divisions = map[string]*Division{
host: {
DivisionForUser: DivisionForUser{
Priority: 0,
State: DivisionState_FullOpen,
},
Url: fmt.Sprintf("http://%s/warehouse", ipaddr),
},
}
}
empty.prepare(mg)
atomic.StorePointer(&mg.serviceptr, unsafe.Pointer(&empty))
@ -513,7 +508,7 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
}
}()
if !*noauth {
if !*devflag {
apitoken := r.Header.Get("MG-X-API-TOKEN")
if len(apitoken) == 0 {
logger.Println("MG-X-API-TOKEN is missing")
@ -600,7 +595,7 @@ func (mg *Maingate) query(w http.ResponseWriter, r *http.Request) {
return
}
if !*noauth {
if !*devflag {
apitoken := r.Header.Get("MG-X-API-TOKEN")
if len(apitoken) == 0 {
logger.Println("MG-X-API-TOKEN is missing")