From 3018bfb836f093360136f39fd78aa9f40866a861 Mon Sep 17 00:00:00 2001 From: loveuer Date: Wed, 1 Jan 2025 22:14:45 -0800 Subject: [PATCH] update: readme.md; mv flag to env --- env.go | 25 +++++++++++++++++++++++++ flag.go | 24 ------------------------ go.mod | 2 ++ go.sum | 6 ++++++ readme.md | 28 ++++++++++++++++++++++++---- run.go | 2 +- upp.go | 10 +++++----- 7 files changed, 63 insertions(+), 34 deletions(-) create mode 100644 env.go delete mode 100644 flag.go diff --git a/env.go b/env.go new file mode 100644 index 0000000..7d91145 --- /dev/null +++ b/env.go @@ -0,0 +1,25 @@ +package upp + +import ( + "time" + + "github.com/alexflint/go-arg" + "github.com/loveuer/upp/pkg/tool" +) + +type _env struct { + Debug bool `arg:"env:DEBUG"` + ListenHttp string `arg:"env:LISTEN_HTTP"` +} + +var env = &_env{} + +func init() { + time.Local = time.FixedZone("CST", 8*3600) + + arg.MustParse(env) + + if env.Debug { + tool.TablePrinter(env) + } +} diff --git a/flag.go b/flag.go deleted file mode 100644 index 073b1e8..0000000 --- a/flag.go +++ /dev/null @@ -1,24 +0,0 @@ -package upp - -import ( - "flag" - "time" -) - -type __flag struct { - debug bool - listen struct { - http string - } -} - -var _flag = &__flag{} - -func init() { - time.Local = time.FixedZone("CST", 8*3600) - - flag.BoolVar(&_flag.debug, "debug", false, "debug mode") - flag.StringVar(&_flag.listen.http, "listen.http", "localhost:8080", "") - - flag.Parse() -} diff --git a/go.mod b/go.mod index ed88d1d..1e73aff 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,8 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect + github.com/alexflint/go-arg v1.5.1 // indirect + github.com/alexflint/go-scalar v1.2.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect diff --git a/go.sum b/go.sum index 37ed91f..1e480e3 100644 --- a/go.sum +++ b/go.sum @@ -2,11 +2,16 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= gitea.com/loveuer/gredis v1.0.0 h1:fbRS8YZObcp1KV1KGj8pDpIj1WrI0W8pwU9Ny/2fJys= gitea.com/loveuer/gredis v1.0.0/go.mod h1:TQlubgDiyNTRXqASd/XIUrqPBLj9NZRR2DmV3V2ZyMY= +github.com/alexflint/go-arg v1.5.1 h1:nBuWUCpuRy0snAG+uIJ6N0UvYxpxA0/ghA/AaHxlT8Y= +github.com/alexflint/go-arg v1.5.1/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8= +github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw= +github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= @@ -80,6 +85,7 @@ github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3k github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= diff --git a/readme.md b/readme.md index b95e015..22bbc17 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,18 @@ app.Run(ctx) > simple example ```go +var config = struct { + ES string +}{} + +func init() { + flag.StringVar(&config.ES, "es", "http://es.dev:9200", "") + + flag.Parse() + + tool.TablePrinter(config) +} + type Record struct { Id uint64 `json:"id" gorm:"primaryKey;column:id"` CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"` @@ -20,14 +32,19 @@ type Record struct { } func main() { - app := upp.New() + app := upp.New(upp.Config{Debug: false}) app.With(upp.InitDB("sqlite://data.db", &Record{})) app.With(upp.InitApi(api.New())) + app.With(upp.InitFn(func(u interfaces.Upp) { + u.UseLogger().Debug("[init] create init record") + u.UseDB().Create(&Record{Name: "init"}) + })) + app.GET("/hello/:name", func(c *api.Ctx) error { name := c.Param("name") - c.UseLogger().Info("[hello] got name = %s", name) + c.UseLogger().Debug("[hello] got name = %s", name) record := &Record{Name: name} err := c.UseDB().Create(record).Error return c.JSON(map[string]any{"record": record, "err": err}) @@ -36,8 +53,11 @@ func main() { app.RunSignal() } + ``` -> run with flags +> run with env ```sh -go run . --debug --listen.http '0.0.0.0:8080' +DEBUG=true go run . + +DEBUG=true LISTEN_HTTP=0.0.0.0:8080 go run . ``` \ No newline at end of file diff --git a/run.go b/run.go index c563e5f..0752620 100644 --- a/run.go +++ b/run.go @@ -10,7 +10,7 @@ import ( ) func (u *upp) StartAPI(ctx context.Context) { - address := _flag.listen.http + address := env.ListenHttp if address == "" { address = u.api.config.Address } diff --git a/upp.go b/upp.go index 8742868..97b661c 100644 --- a/upp.go +++ b/upp.go @@ -42,13 +42,13 @@ func New(configs ...Config) *upp { config = configs[0] } - if config.Debug || _flag.debug { - log.SetLogLevel(log.LogLevelDebug) - } - app := &upp{ logger: upp_logger_pool, - debug: config.Debug, + } + + if config.Debug || env.Debug { + log.SetLogLevel(log.LogLevelDebug) + app.debug = true } return app