코드 정리
This commit is contained in:
@ -12,7 +12,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@ -244,57 +243,53 @@ func (mg *Maingate) discoverOpenIdConfiguration(name string, url string) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeErrorWithStack(err error) error {
|
|
||||||
return fmt.Errorf("%s\n%s", err.Error(), string(debug.Stack()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mg *Maingate) prepare(context context.Context) (err error) {
|
func (mg *Maingate) prepare(context context.Context) (err error) {
|
||||||
if err := mg.discoverOpenIdConfiguration(AuthPlatformMicrosoft, "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"); err != nil {
|
if err := mg.discoverOpenIdConfiguration(AuthPlatformMicrosoft, "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
if err := mg.discoverOpenIdConfiguration("google", "https://accounts.google.com/.well-known/openid-configuration"); err != nil {
|
if err := mg.discoverOpenIdConfiguration("google", "https://accounts.google.com/.well-known/openid-configuration"); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// redis에서 env를 가져온 후에
|
// redis에서 env를 가져온 후에
|
||||||
mg.mongoClient, err = gocommon.NewMongoClient(context, mg.Mongo)
|
mg.mongoClient, err = gocommon.NewMongoClient(context, mg.Mongo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionCouponUse, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionCouponUse, map[string]bson.D{
|
||||||
"idrounds": {{Key: "_id", Value: 1}, {Key: "rounds", Value: 1}},
|
"idrounds": {{Key: "_id", Value: 1}, {Key: "rounds", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionLink, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionLink, map[string]bson.D{
|
||||||
"platformuid": {{Key: "platform", Value: 1}, {Key: "uid", Value: 1}},
|
"platformuid": {{Key: "platform", Value: 1}, {Key: "uid", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionLink, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionLink, map[string]bson.D{
|
||||||
"emailplatform": {{Key: "email", Value: 1}, {Key: "platform", Value: 1}},
|
"emailplatform": {{Key: "email", Value: 1}, {Key: "platform", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeIndices(CollectionAccount, map[string]bson.D{
|
if err = mg.mongoClient.MakeIndices(CollectionAccount, map[string]bson.D{
|
||||||
"accid": {{Key: "accid", Value: 1}},
|
"accid": {{Key: "accid", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionFile, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionFile, map[string]bson.D{
|
||||||
"keyonly": {{Key: "key", Value: 1}},
|
"keyonly": {{Key: "key", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete대신 _ts로 expire시킴. pipeline에 삭제 알려주기 위함
|
// Delete대신 _ts로 expire시킴. pipeline에 삭제 알려주기 위함
|
||||||
if err = mg.mongoClient.MakeExpireIndex(CollectionWhitelist, 10); err != nil {
|
if err = mg.mongoClient.MakeExpireIndex(CollectionWhitelist, 10); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *devflag {
|
if *devflag {
|
||||||
@ -303,40 +298,40 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeExpireIndex(CollectionBlock, int32(3)); err != nil {
|
if err = mg.mongoClient.MakeExpireIndex(CollectionBlock, int32(3)); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionPlatformLoginToken, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionPlatformLoginToken, map[string]bson.D{
|
||||||
"platformauthtoken": {{Key: "platform", Value: 1}, {Key: "key", Value: 1}},
|
"platformauthtoken": {{Key: "platform", Value: 1}, {Key: "key", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeExpireIndex(CollectionPlatformLoginToken, int32(mg.SessionTTL+300)); err != nil {
|
if err = mg.mongoClient.MakeExpireIndex(CollectionPlatformLoginToken, int32(mg.SessionTTL+300)); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionUserToken, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionUserToken, map[string]bson.D{
|
||||||
"platformusertoken": {{Key: "platform", Value: 1}, {Key: "userid", Value: 1}},
|
"platformusertoken": {{Key: "platform", Value: 1}, {Key: "userid", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionGamepotUserInfo, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionGamepotUserInfo, map[string]bson.D{
|
||||||
"gamepotuserid": {{Key: "gamepotuserid", Value: 1}},
|
"gamepotuserid": {{Key: "gamepotuserid", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = mg.mongoClient.MakeUniqueIndices(CollectionFirebaseUserInfo, map[string]bson.D{
|
if err = mg.mongoClient.MakeUniqueIndices(CollectionFirebaseUserInfo, map[string]bson.D{
|
||||||
"firebaseuserid": {{Key: "firebaseuserid", Value: 1}},
|
"firebaseuserid": {{Key: "firebaseuserid", Value: 1}},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mg.sessionProvider, err = session.NewProviderWithConfig(context, mg.SessionConfig)
|
mg.sessionProvider, err = session.NewProviderWithConfig(context, mg.SessionConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var preall []struct {
|
var preall []struct {
|
||||||
@ -346,7 +341,7 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
|||||||
if err = mg.mongoClient.FindAllAs(CollectionFile, nil, &preall, options.Find().SetProjection(bson.M{
|
if err = mg.mongoClient.FindAllAs(CollectionFile, nil, &preall, options.Find().SetProjection(bson.M{
|
||||||
"link": 1,
|
"link": 1,
|
||||||
})); err != nil {
|
})); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pre := range preall {
|
for _, pre := range preall {
|
||||||
@ -361,23 +356,23 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
|||||||
"_id": pre.Id,
|
"_id": pre.Id,
|
||||||
}, &fulldoc)
|
}, &fulldoc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
err = fulldoc.Save()
|
err = fulldoc.Save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var whites []*whitelistmember
|
var whites []*whitelistmember
|
||||||
if err := mg.mongoClient.AllAs(CollectionWhitelist, &whites, options.Find().SetReturnKey(false)); err != nil {
|
if err := mg.mongoClient.AllAs(CollectionWhitelist, &whites, options.Find().SetReturnKey(false)); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
mg.wl.init(whites)
|
mg.wl.init(whites)
|
||||||
|
|
||||||
var blocks []*blockinfo
|
var blocks []*blockinfo
|
||||||
if err := mg.mongoClient.AllAs(CollectionBlock, &blocks); err != nil {
|
if err := mg.mongoClient.AllAs(CollectionBlock, &blocks); err != nil {
|
||||||
return makeErrorWithStack(err)
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
mg.bl.init(blocks)
|
mg.bl.init(blocks)
|
||||||
|
|
||||||
@ -390,7 +385,7 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
|||||||
func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMux, prefix string) error {
|
func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMux, prefix string) error {
|
||||||
var allServices []*serviceDescription
|
var allServices []*serviceDescription
|
||||||
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
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(allServices) > 0 {
|
if len(allServices) > 0 {
|
||||||
@ -409,7 +404,7 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
|||||||
host, _ := os.Hostname()
|
host, _ := os.Hostname()
|
||||||
addrs, err := net.InterfaceAddrs()
|
addrs, err := net.InterfaceAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
ipaddr := "127.0.0.1"
|
ipaddr := "127.0.0.1"
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
@ -441,7 +436,7 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
|||||||
}, options.Update().SetUpsert(true))
|
}, options.Update().SetUpsert(true))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,7 +449,7 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
|||||||
configraw, _ := json.Marshal(mg.maingateConfig)
|
configraw, _ := json.Marshal(mg.maingateConfig)
|
||||||
var convertedConfig map[string]any
|
var convertedConfig map[string]any
|
||||||
if err := json.Unmarshal(configraw, &convertedConfig); err != nil {
|
if err := json.Unmarshal(configraw, &convertedConfig); err != nil {
|
||||||
return err
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
serveMux.HandleFunc(gocommon.MakeHttpHandlerPattern(prefix, "config"), func(w http.ResponseWriter, r *http.Request) {
|
serveMux.HandleFunc(gocommon.MakeHttpHandlerPattern(prefix, "config"), func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -487,7 +482,7 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
|||||||
|
|
||||||
if err := os.MkdirAll("static", os.ModePerm); err != nil {
|
if err := os.MkdirAll("static", os.ModePerm); err != nil {
|
||||||
// 일반 엔드유저한테 오픈할 static 페이지
|
// 일반 엔드유저한테 오픈할 static 페이지
|
||||||
return err
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfsx := http.FileServer(http.Dir("console"))
|
cfsx := http.FileServer(http.Dir("console"))
|
||||||
@ -548,7 +543,7 @@ func (mg *Maingate) GetUserBrowserInfo(r *http.Request) (string, error) {
|
|||||||
|
|
||||||
cookie, err := r.Cookie("ActionSquareSessionExtraInfo")
|
cookie, err := r.Cookie("ActionSquareSessionExtraInfo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//requestinfo := fmt.Sprintf("%s_%s", cookie.Value, host) //-- RemoteAddr체크는 로드밸런서 IP 찍히는 문제 때문에 제외한다.
|
//requestinfo := fmt.Sprintf("%s_%s", cookie.Value, host) //-- RemoteAddr체크는 로드밸런서 IP 찍히는 문제 때문에 제외한다.
|
||||||
@ -570,7 +565,7 @@ func (mg *Maingate) setUserToken(info usertokeninfo) error {
|
|||||||
"accesstoken_expire_time": info.accesstoken_expire_time,
|
"accesstoken_expire_time": info.accesstoken_expire_time,
|
||||||
},
|
},
|
||||||
}, options.Update().SetUpsert(true))
|
}, options.Update().SetUpsert(true))
|
||||||
return err
|
return logger.ErrorWithCallStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mg *Maingate) getUserTokenWithCheck(platform string, userid string, brinfo string) (usertokeninfo, error) {
|
func (mg *Maingate) getUserTokenWithCheck(platform string, userid string, brinfo string) (usertokeninfo, error) {
|
||||||
|
|||||||
@ -565,12 +565,12 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
// !success일 때 빈 body를 보내면 클라이언트는 로그아웃 된다.
|
// !success일 때 빈 body를 보내면 클라이언트는 로그아웃 된다.
|
||||||
if success {
|
if success {
|
||||||
output := map[string]any{
|
json.NewEncoder(w).Encode(map[string]any{
|
||||||
"sk": sk,
|
"sk": sk,
|
||||||
"expirein": sh.sessionTTL.Seconds(),
|
"expirein": sh.sessionTTL.Seconds(),
|
||||||
}
|
})
|
||||||
bt, _ := json.Marshal(output)
|
} else {
|
||||||
w.Write(bt)
|
logger.Println("sh.sessionProvider.Touch failed :", sk)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -598,6 +598,7 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("getProviderInfo failed :", err)
|
logger.Error("getProviderInfo failed :", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if authtype != newType || uid != newId {
|
if authtype != newType || uid != newId {
|
||||||
@ -682,8 +683,8 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
|||||||
if *noauth {
|
if *noauth {
|
||||||
output["noauth"] = true
|
output["noauth"] = true
|
||||||
}
|
}
|
||||||
bt, _ := json.Marshal(output)
|
|
||||||
w.Write(bt)
|
json.NewEncoder(w).Encode(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sh *serviceDescription) findVersionSplit(version string) []byte {
|
func (sh *serviceDescription) findVersionSplit(version string) []byte {
|
||||||
|
|||||||
Reference in New Issue
Block a user