From e2ddd97bf92ef6a26486ed428e282f2e687e63d3 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Fri, 28 Jul 2023 20:40:24 +0300 Subject: [PATCH] update vendors + update init app --- docs/README.md | 6 +-- examples/demo-basic/main.go | 1 - examples/demo-database/main.go | 1 - examples/demo-geoip/main.go | 1 - examples/demo-oauth/main.go | 1 - examples/demo-unix/main.go | 1 - examples/demo-ws-cli/main.go | 1 - examples/demo-ws/main.go | 1 - go.mod | 17 ++++---- go.sum | 36 +++++++++-------- goppy.go | 71 ++++++++++++++++++++-------------- plugins/geoip/maxmind.go | 2 +- 12 files changed, 74 insertions(+), 65 deletions(-) diff --git a/docs/README.md b/docs/README.md index be5ed87..f16aaba 100644 --- a/docs/README.md +++ b/docs/README.md @@ -56,15 +56,15 @@ import ( "fmt" "os" - "github.com/osspkg/go-sdk/console" "github.com/osspkg/goppy" "github.com/osspkg/goppy/plugins" "github.com/osspkg/goppy/plugins/web" ) func main() { + // Specify the path to the config via the argument: `--config`. + // Specify the path to the pidfile via the argument: `--pid`. app := goppy.New() - app.WithConfig("./config.yaml") // Reassigned via the `--config` argument when run via the console. app.Plugins( web.WithHTTP(), ) @@ -81,7 +81,7 @@ func main() { }, }, ) - app.Command("env", func(s console.CommandSetter) { + app.Command("env", func() { fmt.Println(os.Environ()) }) app.Run() diff --git a/examples/demo-basic/main.go b/examples/demo-basic/main.go index fd05730..f05174f 100644 --- a/examples/demo-basic/main.go +++ b/examples/demo-basic/main.go @@ -17,7 +17,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") // Reassigned via the `--config` argument when run via the console. app.Plugins( web.WithHTTP(), ) diff --git a/examples/demo-database/main.go b/examples/demo-database/main.go index 683a162..b1f4f9d 100644 --- a/examples/demo-database/main.go +++ b/examples/demo-database/main.go @@ -17,7 +17,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") app.Plugins( web.WithHTTPDebug(), web.WithHTTP(), diff --git a/examples/demo-geoip/main.go b/examples/demo-geoip/main.go index dc5d4f0..d72b7a2 100644 --- a/examples/demo-geoip/main.go +++ b/examples/demo-geoip/main.go @@ -17,7 +17,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") app.Plugins( web.WithHTTPDebug(), web.WithHTTP(), diff --git a/examples/demo-oauth/main.go b/examples/demo-oauth/main.go index 803c619..f4a7127 100644 --- a/examples/demo-oauth/main.go +++ b/examples/demo-oauth/main.go @@ -14,7 +14,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") app.Plugins( web.WithHTTP(), auth.WithOAuth(), diff --git a/examples/demo-unix/main.go b/examples/demo-unix/main.go index 0a6ab6b..030ed18 100644 --- a/examples/demo-unix/main.go +++ b/examples/demo-unix/main.go @@ -16,7 +16,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") app.Plugins( unix.WithServer(), unix.WithClient(), diff --git a/examples/demo-ws-cli/main.go b/examples/demo-ws-cli/main.go index 8e21d84..4179b61 100644 --- a/examples/demo-ws-cli/main.go +++ b/examples/demo-ws-cli/main.go @@ -17,7 +17,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") app.Plugins( web.WithWebsocketClient(), ) diff --git a/examples/demo-ws/main.go b/examples/demo-ws/main.go index cd18aae..e08553e 100644 --- a/examples/demo-ws/main.go +++ b/examples/demo-ws/main.go @@ -17,7 +17,6 @@ import ( func main() { app := goppy.New() - app.WithConfig("./config.yaml") app.Plugins( web.WithHTTP(), web.WithWebsocketServer(), diff --git a/go.mod b/go.mod index c1273ed..f96e88f 100644 --- a/go.mod +++ b/go.mod @@ -6,26 +6,27 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/mailru/easyjson v0.7.7 github.com/oschwald/geoip2-golang v1.9.0 - github.com/osspkg/go-sdk v1.3.2 + github.com/osspkg/go-sdk v1.3.6 github.com/osspkg/go-static v1.3.2 github.com/stretchr/testify v1.8.4 gopkg.in/yaml.v3 v3.0.1 ) require ( - cloud.google.com/go/compute/metadata v0.2.0 // indirect + cloud.google.com/go/compute v1.20.1 // indirect + cloud.google.com/go/compute/metadata v0.2.3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-sql-driver/mysql v1.7.1 // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/lib/pq v1.10.9 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/oschwald/maxminddb-golang v1.11.0 // indirect - github.com/osspkg/go-algorithms v1.2.4 // indirect + github.com/osspkg/go-algorithms v1.2.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/net v0.11.0 // indirect - golang.org/x/oauth2 v0.9.0 // indirect - golang.org/x/sys v0.9.0 // indirect + golang.org/x/net v0.12.0 // indirect + golang.org/x/oauth2 v0.10.0 // indirect + golang.org/x/sys v0.10.0 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.28.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/go.sum b/go.sum index 9c871de..d75732d 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,17 @@ -cloud.google.com/go/compute/metadata v0.2.0 h1:nBbNSZyDpkNlo3DepaaLKVuO7ClyifSAmNloSCZrHnQ= -cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= +cloud.google.com/go/compute v1.20.1 h1:6aKEtlUiwEpJzM001l0yFkpXmUVXaN8W+fbkb2AZNbg= +cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM= +cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= +cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= @@ -24,10 +26,10 @@ github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrz github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y= github.com/oschwald/maxminddb-golang v1.11.0 h1:aSXMqYR/EPNjGE8epgqwDay+P30hCBZIveY0WZbAWh0= github.com/oschwald/maxminddb-golang v1.11.0/go.mod h1:YmVI+H0zh3ySFR3w+oz8PCfglAFj3PuCmui13+P9zDg= -github.com/osspkg/go-algorithms v1.2.4 h1:e33+nNZbHJA9RmVZMqQ3+uqM6u6B6s+TTtgl742LKS4= -github.com/osspkg/go-algorithms v1.2.4/go.mod h1:Zdclm/CKhDrUD34kIm9PL4VDbiey/jXAHY36nfc0r5Q= -github.com/osspkg/go-sdk v1.3.2 h1:jamGdCLc2vyS+rPD04cVszie/9ekPKv9OTZEHNvMh/s= -github.com/osspkg/go-sdk v1.3.2/go.mod h1:69hC0tZpAwBRjOxEpSJmo/c0b7KBhbG/BdmJ21t+Hv0= +github.com/osspkg/go-algorithms v1.2.6 h1:/eIZ1XlxZ2LRxtbOG4REA5KcR17F5E64+Y2cudJvfRU= +github.com/osspkg/go-algorithms v1.2.6/go.mod h1:Zdclm/CKhDrUD34kIm9PL4VDbiey/jXAHY36nfc0r5Q= +github.com/osspkg/go-sdk v1.3.6 h1:VcB5o3+c1uwkD2B0v4ZxOeaN/SN0QUr3Fktt8S3Le0M= +github.com/osspkg/go-sdk v1.3.6/go.mod h1:/ZzPlvttlMyTLiDomDGJt7sqFz/RmkWXzrK5tifci14= github.com/osspkg/go-static v1.3.2 h1:MST9eSG/gO6b1OjFlxoFfSLhdKRiaUA8lEUhh0KoUOg= github.com/osspkg/go-static v1.3.2/go.mod h1:PI0nuemvgmLuVgn3hIZMRFgQH6d1Iyn70Gs23Gl05qo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -36,13 +38,13 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= -golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= -golang.org/x/oauth2 v0.9.0 h1:BPpt2kU7oMRq3kCHAA1tbSEshXRw1LpG2ztgDwrzuAs= -golang.org/x/oauth2 v0.9.0/go.mod h1:qYgFZaFiu6Wg24azG8bdV52QJXJGbZzIIsRCdVKzbLw= +golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= +golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8= +golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= -golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -51,8 +53,8 @@ google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6 google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= -google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/goppy.go b/goppy.go index cbfb041..2bcfe18 100644 --- a/goppy.go +++ b/goppy.go @@ -21,14 +21,12 @@ type ( _app struct { application app.App commands map[string]interface{} - config string plugins []interface{} configs []interface{} args *console.Args } Goppy interface { - WithConfig(filename string) Plugins(args ...plugins.Plugin) Command(name string, call interface{}) Run() @@ -38,19 +36,16 @@ type ( // New constructor for init Goppy func New() Goppy { return &_app{ - application: app.New(), - commands: make(map[string]interface{}), - plugins: make([]interface{}, 0, 100), - configs: make([]interface{}, 0, 100), - args: console.NewArgs().Parse(os.Args[1:]), + application: app.New().ExitFunc(func(code int) { + os.Exit(code) + }), + commands: make(map[string]interface{}), + plugins: make([]interface{}, 0, 100), + configs: make([]interface{}, 0, 100), + args: console.NewArgs().Parse(os.Args[1:]), } } -// WithConfig set config path for goppy -func (v *_app) WithConfig(filename string) { - v.config = filename -} - // Plugins setting the list of plugins to initialize func (v *_app) Plugins(args ...plugins.Plugin) { for _, arg := range args { @@ -73,25 +68,25 @@ func (v *_app) Command(name string, call interface{}) { // Run launching Goppy with initialization of all dependencies func (v *_app) Run() { - v.config = v.parseConfigFlag(v.config) - console.FatalIfErr(recoveryConfig(v.config, v.configs...), "config recovery") - console.FatalIfErr(validateConfig(v.config, v.configs...), "config validate") + apps := v.application.Modules(v.plugins...) + + config := v.parseConfigFlag() + console.FatalIfErr(recoveryConfig(config, v.configs...), "config recovery") + console.FatalIfErr(validateConfig(config, v.configs...), "config validate") + apps.ConfigFile(config, v.configs...) + + pid, err := v.parsePIDFileFlag() + console.FatalIfErr(err, "check pid file") + apps.PidFile(pid) if params := v.args.Next(); len(params) > 0 { if cmd, ok := v.commands[params[0]]; ok { - v.application. - ConfigFile(v.config, v.configs...). - Modules(v.plugins...). - Invoke(cmd) + apps.Invoke(cmd) return } console.Fatalf("<%s> command not found", params[0]) } - - v.application. - ConfigFile(v.config, v.configs...). - Modules(v.plugins...). - Run() + apps.Run() } func reflectResolve(arg interface{}, k reflect.Kind, call func(interface{}), comment string) { @@ -104,18 +99,33 @@ func reflectResolve(arg interface{}, k reflect.Kind, call func(interface{}), com call(arg) } -func (v *_app) parseConfigFlag(filename string) string { - if len(filename) == 0 { - filename = "./config.yaml" - } +func (v *_app) parseConfigFlag() string { conf := v.args.Get("config") if conf == nil || len(*conf) == 0 { - conf = &filename + return "" } return *conf } +func (v *_app) parsePIDFileFlag() (string, error) { + pid := v.args.Get("pid") + if pid == nil || len(*pid) == 0 { + return "", nil + } + file, err := os.Create(*pid) + if err != nil { + return "", err + } + if err = file.Close(); err != nil { + return "", err + } + return *pid, nil +} + func validateConfig(filename string, configs ...interface{}) error { + if len(filename) == 0 { + return nil + } _, err := os.Stat(filename) if err == nil { return nil @@ -142,6 +152,9 @@ func validateConfig(filename string, configs ...interface{}) error { } func recoveryConfig(filename string, configs ...interface{}) error { + if len(filename) == 0 { + return nil + } _, err := os.Stat(filename) if err == nil { return nil diff --git a/plugins/geoip/maxmind.go b/plugins/geoip/maxmind.go index c6e51a4..de046b9 100644 --- a/plugins/geoip/maxmind.go +++ b/plugins/geoip/maxmind.go @@ -9,7 +9,7 @@ import ( "fmt" "net" - geoip2 "github.com/oschwald/geoip2-golang" + "github.com/oschwald/geoip2-golang" "github.com/osspkg/goppy/plugins" )