diff --git a/azure/misc.go b/azure/misc.go index 8d6b3a3..f28e24b 100644 --- a/azure/misc.go +++ b/azure/misc.go @@ -16,12 +16,14 @@ import ( "syscall" "time" - "repositories.action2quare.com/ayo/gocommon" + "repositories.action2quare.com/ayo/gocommon/flagx" "repositories.action2quare.com/ayo/gocommon/logger" "go.mongodb.org/mongo-driver/bson" ) +var linkupdate = flagx.String("updatelink", "", "") + func SortVersions(versions []string) []string { sort.Slice(versions, func(i, j int) bool { leftnum := 0 @@ -204,10 +206,10 @@ func ReplyUpdateComplete() { } }() - if len(*gocommon.Linkupdate) > 0 { - cache := DeserializeMessageReplyCache(*gocommon.Linkupdate) + if len(*linkupdate) > 0 { + cache := DeserializeMessageReplyCache(*linkupdate) if cache != nil { - os.Remove(*gocommon.Linkupdate) + os.Remove(*linkupdate) if cache.ReplyWrap != nil { cache.ReplyWrap.Update(cache.Replyaid, "업데이트 완료") } diff --git a/flags.go b/flags.go deleted file mode 100644 index b9d8c46..0000000 --- a/flags.go +++ /dev/null @@ -1,22 +0,0 @@ -package gocommon - -import ( - "flag" - "io" - "os" -) - -var commandLine = flag.NewFlagSet("gocommon", flag.ContinueOnError) -var devflag = commandLine.Bool("dev", false, "") -var configfileflag = commandLine.String("config", "", "") -var PrefixPtr = commandLine.String("prefix", "", "'") -var portptr = commandLine.Int("port", 80, "") -var tls = commandLine.String("tls", "", "") -var NoSessionFlag = commandLine.Bool("nosession", false, "nosession=[true|false]") -var NeedPrefetch = commandLine.Bool("prefetch", false, "") -var Linkupdate = commandLine.String("updatelink", "", "") - -func init() { - commandLine.SetOutput(io.Discard) - commandLine.Parse(os.Args[1:]) -} diff --git a/flagx/commandline.go b/flagx/commandline.go new file mode 100644 index 0000000..3b09aa2 --- /dev/null +++ b/flagx/commandline.go @@ -0,0 +1,175 @@ +package flagx + +import ( + "encoding" + "flag" + "fmt" + "os" + "strings" + "time" +) + +var commandLine = []*flag.FlagSet{ + flag.NewFlagSet(os.Args[0], flag.ContinueOnError), +} + +func init() { + Parse() +} + +func findProperFlagSet(name string) *flag.FlagSet { + for _, cl := range commandLine { + if cl.Lookup(name) == nil { + return cl + } + } + next := flag.NewFlagSet(fmt.Sprintf("%s-%d", os.Args[0], len(commandLine)), flag.ContinueOnError) + commandLine = append(commandLine, next) + return next +} + +func VisitAll(fn func(*flag.Flag)) { + for _, cl := range commandLine { + cl.VisitAll(fn) + } +} + +// Visit visits the command-line flags in lexicographical order, calling fn +// for each. It visits only those flags that have been set. +func Visit(fn func(*flag.Flag)) { + for _, cl := range commandLine { + cl.Visit(fn) + } +} + +func Lookup(name string) *flag.Flag { + for _, cl := range commandLine { + f := cl.Lookup(name) + if f != nil { + return f + } + } + return nil +} + +func Set(name, value string) error { + for _, cl := range commandLine { + if cl.Lookup(name) != nil { + return cl.Set(name, value) + } + } + return fmt.Errorf("no such flag -%v", name) +} + +func PrintDefaults() { + for _, cl := range commandLine { + cl.PrintDefaults() + } +} + +func BoolVar(p *bool, name string, value bool, usage string) { + findProperFlagSet(name).BoolVar(p, name, value, usage) +} + +func Bool(name string, value bool, usage string) *bool { + return findProperFlagSet(name).Bool(name, value, usage) +} + +func IntVar(p *int, name string, value int, usage string) { + findProperFlagSet(name).IntVar(p, name, value, usage) +} +func Int(name string, value int, usage string) *int { + return findProperFlagSet(name).Int(name, value, usage) +} +func Int64Var(p *int64, name string, value int64, usage string) { + findProperFlagSet(name).Int64Var(p, name, value, usage) +} + +func Int64(name string, value int64, usage string) *int64 { + return findProperFlagSet(name).Int64(name, value, usage) +} + +func UintVar(p *uint, name string, value uint, usage string) { + findProperFlagSet(name).UintVar(p, name, value, usage) +} + +func Uint(name string, value uint, usage string) *uint { + return findProperFlagSet(name).Uint(name, value, usage) +} + +func Uint64Var(p *uint64, name string, value uint64, usage string) { + findProperFlagSet(name).Uint64Var(p, name, value, usage) +} + +func Uint64(name string, value uint64, usage string) *uint64 { + return findProperFlagSet(name).Uint64(name, value, usage) +} + +func StringVar(p *string, name string, value string, usage string) { + findProperFlagSet(name).StringVar(p, name, value, usage) +} + +func String(name string, value string, usage string) *string { + return findProperFlagSet(name).String(name, value, usage) +} + +func Float64Var(p *float64, name string, value float64, usage string) { + findProperFlagSet(name).Float64Var(p, name, value, usage) +} + +func Float64(name string, value float64, usage string) *float64 { + return findProperFlagSet(name).Float64(name, value, usage) +} + +func DurationVar(p *time.Duration, name string, value time.Duration, usage string) { + findProperFlagSet(name).DurationVar(p, name, value, usage) +} + +func Duration(name string, value time.Duration, usage string) *time.Duration { + return findProperFlagSet(name).Duration(name, value, usage) +} + +func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string) { + findProperFlagSet(name).TextVar(p, name, value, usage) +} +func Func(name, usage string, fn func(string) error) { + findProperFlagSet(name).Func(name, usage, fn) +} + +func Var(value flag.Value, name string, usage string) { + findProperFlagSet(name).Var(value, name, usage) +} + +func Parse() { + args := os.Args[1:] + for _, cl := range commandLine { + if cl.Parsed() { + continue + } + + var filtered []string + cl.VisitAll(func(f *flag.Flag) { + for _, arg := range args { + test := strings.TrimLeft(arg, "-") + if test == f.Name || strings.HasPrefix(test, f.Name+"=") { + filtered = append(filtered, arg) + break + } + } + }) + cl.Parse(filtered) + } +} + +func Parsed() bool { + for _, cl := range commandLine { + if !cl.Parsed() { + return false + } + } + return true +} + +func NewFlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet { + return flag.NewFlagSet(name, errorHandling) +} diff --git a/misc.go b/misc.go index 5381b1a..a37d40e 100644 --- a/misc.go +++ b/misc.go @@ -12,8 +12,12 @@ import ( "strings" "sync/atomic" "time" + + "repositories.action2quare.com/ayo/gocommon/flagx" ) +var devflag = flagx.Bool("dev", false, "") + var sequenceStart = rand.Uint32() func MakeHttpHandlerPattern(n ...string) string { diff --git a/reflect_config.go b/reflect_config.go index 0215263..c8db5ce 100644 --- a/reflect_config.go +++ b/reflect_config.go @@ -4,8 +4,12 @@ import ( "encoding/json" "os" "time" + + "repositories.action2quare.com/ayo/gocommon/flagx" ) +var configfileflag = flagx.String("config", "", "") + func configFilePath() string { configfilepath := "config.json" if configfileflag != nil && len(*configfileflag) > 0 { diff --git a/server.go b/server.go index c3a8884..4dc4530 100644 --- a/server.go +++ b/server.go @@ -21,6 +21,7 @@ import ( "syscall" "time" + "repositories.action2quare.com/ayo/gocommon/flagx" "repositories.action2quare.com/ayo/gocommon/logger" "github.com/pires/go-proxyproto" @@ -84,6 +85,9 @@ func welcomeHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("welcome")) } +var tls = flagx.String("tls", "", "") +var portptr = flagx.Int("port", 80, "") + // NewHTTPServer : func NewHTTPServerWithPort(serveMux *http.ServeMux, port int) *Server { if len(*tls) > 0 && port == 80 { diff --git a/wshandler/wshandler.go b/wshandler/wshandler.go index baed5ac..0ad7fb0 100644 --- a/wshandler/wshandler.go +++ b/wshandler/wshandler.go @@ -12,6 +12,7 @@ import ( "sync" common "repositories.action2quare.com/ayo/gocommon" + "repositories.action2quare.com/ayo/gocommon/flagx" "repositories.action2quare.com/ayo/gocommon/logger" "github.com/go-redis/redis/v8" @@ -20,6 +21,8 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +var noSessionFlag = flagx.Bool("nosession", false, "nosession=[true|false]") + const ( connStateCachePrefix = "conn_state_" connStateScript = ` @@ -289,7 +292,7 @@ func (ws *WebsocketHandler) RegisterHandlers(ctx context.Context, serveMux *http } sh.url = common.MakeHttpHandlerPattern(prefix, region, "ws") sh.redisSync = ws.RedisSync - if *common.NoSessionFlag { + if *noSessionFlag { serveMux.HandleFunc(sh.url, sh.upgrade_nosession) } else { serveMux.HandleFunc(sh.url, sh.upgrade)