flag를 flagx로 대체

This commit is contained in:
2023-06-21 14:33:29 +09:00
parent 2399f8cd27
commit 7cd5211779
7 changed files with 107 additions and 65 deletions

View File

@ -20,7 +20,6 @@ import (
common "repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/maingate/flag"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -76,7 +75,7 @@ func (fd *fileDocumentDesc) save() error {
}
func (caller apiCaller) isAdmin() bool {
if *flag.Noauth {
if *noauth {
return true
}
@ -389,7 +388,7 @@ func (mg *Maingate) api(w http.ResponseWriter, r *http.Request) {
var userinfo map[string]any
if !*flag.Noauth {
if !*noauth {
authheader := r.Header.Get("Authorization")
if len(authheader) == 0 {
logger.Println("Authorization header is not valid :", authheader)

View File

@ -18,8 +18,8 @@ import (
"unsafe"
common "repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/flagx"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/maingate/flag"
"github.com/golang-jwt/jwt"
"go.mongodb.org/mongo-driver/bson"
@ -32,6 +32,9 @@ import (
"google.golang.org/api/option"
)
var devflag = flagx.Bool("dev", false, "")
var noauth = flagx.Bool("noauth", false, "")
var (
CollectionLink = common.CollectionName("link")
CollectionAuth = common.CollectionName("auth")
@ -60,7 +63,7 @@ const (
)
func SessionTTL() time.Duration {
if *flag.Devflag {
if *devflag {
return sessionTTLDev
}
@ -71,22 +74,6 @@ type mongoAuthCell struct {
src *common.Authinfo
}
func init() {
if *flag.Devflag {
hostname, _ := os.Hostname()
CollectionLink = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionLink)))
CollectionAuth = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionAuth)))
CollectionWhitelist = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionWhitelist)))
CollectionService = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionService)))
CollectionAccount = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionAccount)))
CollectionBlock = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionBlock)))
CollectionPlatformLoginToken = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionPlatformLoginToken)))
CollectionUserToken = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionUserToken)))
CollectionGamepotUserInfo = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionGamepotUserInfo)))
CollectionFirebaseUserInfo = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionFirebaseUserInfo)))
}
}
func (ac *mongoAuthCell) ToAuthinfo() *common.Authinfo {
if ac.src == nil {
logger.Error("mongoAuthCell ToAuthinfo failed. ac.src is nil")
@ -187,8 +174,24 @@ type Maingate struct {
firebaseAppContext context.Context
}
var collectionNameMod = int32(0)
// New :
func New(ctx context.Context) (*Maingate, error) {
if *devflag && atomic.AddInt32(&collectionNameMod, 1) == 1 {
hostname, _ := os.Hostname()
CollectionLink = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionLink)))
CollectionAuth = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionAuth)))
CollectionWhitelist = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionWhitelist)))
CollectionService = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionService)))
CollectionAccount = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionAccount)))
CollectionBlock = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionBlock)))
CollectionPlatformLoginToken = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionPlatformLoginToken)))
CollectionUserToken = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionUserToken)))
CollectionGamepotUserInfo = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionGamepotUserInfo)))
CollectionFirebaseUserInfo = common.CollectionName(fmt.Sprintf("%s-%s", hostname, string(CollectionFirebaseUserInfo)))
}
var config maingateConfig
if err := common.LoadConfig(&config); err != nil {
return nil, err
@ -218,7 +221,7 @@ func New(ctx context.Context) (*Maingate, error) {
return nil, err
}
if !*flag.Noauth {
if !*noauth {
opt := option.WithCredentialsFile(mg.FirebaseAdminSDKCredentialFile)
firebaseApp, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
@ -446,7 +449,7 @@ func whitelistKey(email string) string {
func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMux, prefix string) error {
var allServices []*serviceDescription
if *flag.Noauth {
if *noauth {
host, _ := os.Hostname()
empty := serviceDescription{
ServiceDescriptionSummary: ServiceDescriptionSummary{
@ -576,7 +579,7 @@ func (mg *Maingate) query(w http.ResponseWriter, r *http.Request) {
return
}
if !*flag.Noauth {
if !*noauth {
apitoken := r.Header.Get("MG-X-API-TOKEN")
if len(apitoken) == 0 {
logger.Println("MG-X-API-TOKEN is missing")

View File

@ -16,7 +16,6 @@ import (
common "repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/maingate/flag"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -428,7 +427,7 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
session := queryvals.Get("sk")
var email string
if !*flag.Noauth {
if !*noauth {
//email, err := sh.readProfile(authtype, uid, accesstoken)
bfinfo, err := sh.getUserBrowserInfo(r)
if err != nil {