bson, json 소문자로 변경

This commit is contained in:
2023-06-07 11:56:52 +09:00
parent 5696f177e2
commit b8f5d71be1
3 changed files with 49 additions and 33 deletions

View File

@ -465,6 +465,11 @@ func (caller apiCaller) maintenanceAPI(w http.ResponseWriter, r *http.Request) e
} }
} else if r.Method == "POST" { } else if r.Method == "POST" {
servicename := queryvals.Get("name") servicename := queryvals.Get("name")
if valid, _ := caller.isValidUser(servicename, "service"); !valid {
logger.Println("maintenanceAPI failed. not vaild user :", r.Method, caller.userinfo)
w.WriteHeader(http.StatusBadRequest)
return nil
}
var divs map[string]*Division var divs map[string]*Division
dec := json.NewDecoder(r.Body) dec := json.NewDecoder(r.Body)

View File

@ -1,6 +1,7 @@
package core package core
import ( import (
"crypto/md5"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
@ -22,9 +23,9 @@ import (
) )
type blockinfo struct { type blockinfo struct {
Start primitive.DateTime Start primitive.DateTime `bson:"start" json:"start"`
End primitive.DateTime `bson:"_ts"` End primitive.DateTime `bson:"_ts"`
Reason string Reason string `bson:"reason" json:"reason"`
} }
type whitelistAuthType = string type whitelistAuthType = string
@ -35,12 +36,12 @@ const (
) )
type whitelistmember struct { type whitelistmember struct {
Service string Service string `bson:"service" json:"service"`
Email string Email string `bson:"email" json:"email"`
Platform string Platform string `bson:"platform" json:"platform"`
Desc string Desc string `bson:"desc" json:"desc"`
Auth []whitelistAuthType Auth []whitelistAuthType `bson:"auth" json:"auth"`
Expired primitive.DateTime `bson:"_ts,omitempty" json:"_ts,omitempty"` Expired primitive.DateTime `bson:"_ts,omitempty" json:"_ts,omitempty"`
} }
type whitelist struct { type whitelist struct {
@ -165,26 +166,30 @@ type Maintenance struct {
link string link string
} }
type DivisionForUser struct {
Priority int `bson:"priority" json:"priority"`
State DivisionStateName `bson:"state" json:"state"`
Maintenance *Maintenance `bson:",omitempty" json:",omitempty"`
}
type Division struct { type Division struct {
Url string // 요것은 클라이언트 빌드하고 나서 json:"-"으로 변경하자. 클라이언트에 직접 내려보내지 않음 DivisionForUser `bson:",inline" json:",inline"`
Priority int Url string `bson:"url" json:"url"`
State DivisionStateName
Maintenance *Maintenance `bson:",omitempty" json:",omitempty"`
} }
type ServiceDescriptionSummary struct { type ServiceDescriptionSummary struct {
Id primitive.ObjectID `bson:"_id"` Id primitive.ObjectID `bson:"_id" json:"_id"`
ServiceName string `bson:"service"` ServiceName string `bson:"service" json:"service"`
ServiceCode string `bson:"code"` ServiceCode string `bson:"code" json:"code"`
UseWhitelist bool `bson:"use_whitelist"` UseWhitelist bool `bson:"use_whitelist" json:"use_whitelist"`
Closed bool `bson:"closed"` Closed bool `bson:"closed" json:"closed"`
} }
type serviceDescription struct { type serviceDescription struct {
ServiceDescriptionSummary `bson:",inline"` ServiceDescriptionSummary `bson:",inline" json:",inline"`
Divisions map[string]*Division `bson:"divisions"` Divisions map[string]*Division `bson:"divisions" json:"divisions"`
ServerApiTokens []primitive.ObjectID `bson:"api_tokens"` ServerApiTokens []primitive.ObjectID `bson:"api_tokens" json:"api_tokens"`
ApiUsers map[string][]string `bson:"api_users"` ApiUsers map[string][]string `bson:"api_users" json:"api_users"`
auths *common.AuthCollection auths *common.AuthCollection
wl whitelist wl whitelist
@ -197,10 +202,11 @@ type serviceDescription struct {
updateUserinfo func(info usertokeninfo) (bool, string, string) updateUserinfo func(info usertokeninfo) (bool, string, string)
getProviderInfo func(platform string, uid string) (string, string, error) getProviderInfo func(platform string, uid string) (string, string, error)
apiUsers unsafe.Pointer apiUsers unsafe.Pointer
divisionsSerialized unsafe.Pointer divisionsForUsersSerialized unsafe.Pointer
serviceSerialized unsafe.Pointer divisionsSerialized unsafe.Pointer
serviceSummarySerialized unsafe.Pointer serviceSerialized unsafe.Pointer
serviceSummarySerialized unsafe.Pointer
} }
func (sh *serviceDescription) readProfile(authtype string, id string, binfo string) (email string, err error) { func (sh *serviceDescription) readProfile(authtype string, id string, binfo string) (email string, err error) {
@ -240,13 +246,13 @@ func (sh *serviceDescription) prepare(mg *Maingate) error {
sh.ServiceCode = hex.EncodeToString(sh.Id[6:]) sh.ServiceCode = hex.EncodeToString(sh.Id[6:])
} }
var closed []string divsForUsers := make(map[string]*DivisionForUser)
for dn, div := range divs { for dn, div := range divs {
if div.State == DivisionState_Closed { if div.State == DivisionState_Closed {
closed = append(closed, dn)
continue continue
} }
divsForUsers[dn] = &div.DivisionForUser
if len(div.State) == 0 { if len(div.State) == 0 {
div.State = DivisionState_FullOpen div.State = DivisionState_FullOpen
} }
@ -262,7 +268,11 @@ func (sh *serviceDescription) prepare(mg *Maingate) error {
} else if strings.HasPrefix(div.Maintenance.Notice, "http") { } else if strings.HasPrefix(div.Maintenance.Notice, "http") {
div.Maintenance.link = div.Maintenance.Notice div.Maintenance.link = div.Maintenance.Notice
} else { } else {
div.Maintenance.link = path.Join("static", sh.ServiceCode, div.Maintenance.Notice) hasher := md5.New()
hasher.Write([]byte(sh.ServiceName))
subfolder := hex.EncodeToString(hasher.Sum(nil))[:8]
div.Maintenance.link = path.Join("static", subfolder, div.Maintenance.Notice)
} }
} }
} else { } else {
@ -270,14 +280,14 @@ func (sh *serviceDescription) prepare(mg *Maingate) error {
} }
} }
for _, dn := range closed {
delete(divs, dn)
}
divmarshaled, _ := json.Marshal(divs) divmarshaled, _ := json.Marshal(divs)
devstr := string(divmarshaled) devstr := string(divmarshaled)
sh.divisionsSerialized = unsafe.Pointer(&devstr) sh.divisionsSerialized = unsafe.Pointer(&devstr)
divmarshaled2, _ := json.Marshal(divsForUsers)
devstr2 := string(divmarshaled2)
sh.divisionsForUsersSerialized = unsafe.Pointer(&devstr2)
sh.mongoClient = mg.mongoClient sh.mongoClient = mg.mongoClient
sh.auths = mg.auths sh.auths = mg.auths
sh.sessionTTL = time.Duration(mg.SessionTTL * int64(time.Second)) sh.sessionTTL = time.Duration(mg.SessionTTL * int64(time.Second))
@ -764,7 +774,7 @@ func (sh *serviceDescription) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
} }
} else { } else {
divstrptr := atomic.LoadPointer(&sh.divisionsSerialized) divstrptr := atomic.LoadPointer(&sh.divisionsForUsersSerialized)
divstr := *(*string)(divstrptr) divstr := *(*string)(divstrptr)
w.Write([]byte(divstr)) w.Write([]byte(divstr))
} }

View File

@ -283,6 +283,7 @@ func (mg *Maingate) watchServiceCollection(parentctx context.Context, serveMux *
case "update": case "update":
data.Service.prepare(mg) data.Service.prepare(mg)
if old := mg.services.get(data.Service.ServiceName); old != nil { if old := mg.services.get(data.Service.ServiceName); old != nil {
atomic.SwapPointer(&old.divisionsForUsersSerialized, data.Service.divisionsForUsersSerialized)
atomic.SwapPointer(&old.divisionsSerialized, data.Service.divisionsSerialized) atomic.SwapPointer(&old.divisionsSerialized, data.Service.divisionsSerialized)
atomic.SwapPointer(&old.apiUsers, data.Service.apiUsers) atomic.SwapPointer(&old.apiUsers, data.Service.apiUsers)
atomic.SwapPointer(&old.serviceSerialized, data.Service.serviceSerialized) atomic.SwapPointer(&old.serviceSerialized, data.Service.serviceSerialized)