update: readme.md; mv flag to env
This commit is contained in:
parent
a9039115d8
commit
3018bfb836
25
env.go
Normal file
25
env.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
24
flag.go
24
flag.go
@ -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()
|
|
||||||
}
|
|
2
go.mod
2
go.mod
@ -23,6 +23,8 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
filippo.io/edwards25519 v1.1.0 // indirect
|
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/cespare/xxhash/v2 v2.1.2 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
6
go.sum
6
go.sum
@ -2,11 +2,16 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
|||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
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 h1:fbRS8YZObcp1KV1KGj8pDpIj1WrI0W8pwU9Ny/2fJys=
|
||||||
gitea.com/loveuer/gredis v1.0.0/go.mod h1:TQlubgDiyNTRXqASd/XIUrqPBLj9NZRR2DmV3V2ZyMY=
|
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 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
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/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.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 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 h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
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=
|
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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
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/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.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.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
28
readme.md
28
readme.md
@ -13,6 +13,18 @@ app.Run(ctx)
|
|||||||
|
|
||||||
> simple example
|
> simple example
|
||||||
```go
|
```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 {
|
type Record struct {
|
||||||
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
Id uint64 `json:"id" gorm:"primaryKey;column:id"`
|
||||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||||
@ -20,14 +32,19 @@ type Record struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := upp.New()
|
app := upp.New(upp.Config{Debug: false})
|
||||||
|
|
||||||
app.With(upp.InitDB("sqlite://data.db", &Record{}))
|
app.With(upp.InitDB("sqlite://data.db", &Record{}))
|
||||||
app.With(upp.InitApi(api.New()))
|
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 {
|
app.GET("/hello/:name", func(c *api.Ctx) error {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
c.UseLogger().Info("[hello] got name = %s", name)
|
c.UseLogger().Debug("[hello] got name = %s", name)
|
||||||
record := &Record{Name: name}
|
record := &Record{Name: name}
|
||||||
err := c.UseDB().Create(record).Error
|
err := c.UseDB().Create(record).Error
|
||||||
return c.JSON(map[string]any{"record": record, "err": err})
|
return c.JSON(map[string]any{"record": record, "err": err})
|
||||||
@ -36,8 +53,11 @@ func main() {
|
|||||||
app.RunSignal()
|
app.RunSignal()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
> run with flags
|
> run with env
|
||||||
```sh
|
```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 .
|
||||||
```
|
```
|
2
run.go
2
run.go
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (u *upp) StartAPI(ctx context.Context) {
|
func (u *upp) StartAPI(ctx context.Context) {
|
||||||
address := _flag.listen.http
|
address := env.ListenHttp
|
||||||
if address == "" {
|
if address == "" {
|
||||||
address = u.api.config.Address
|
address = u.api.config.Address
|
||||||
}
|
}
|
||||||
|
10
upp.go
10
upp.go
@ -42,13 +42,13 @@ func New(configs ...Config) *upp {
|
|||||||
config = configs[0]
|
config = configs[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Debug || _flag.debug {
|
|
||||||
log.SetLogLevel(log.LogLevelDebug)
|
|
||||||
}
|
|
||||||
|
|
||||||
app := &upp{
|
app := &upp{
|
||||||
logger: upp_logger_pool,
|
logger: upp_logger_pool,
|
||||||
debug: config.Debug,
|
}
|
||||||
|
|
||||||
|
if config.Debug || env.Debug {
|
||||||
|
log.SetLogLevel(log.LogLevelDebug)
|
||||||
|
app.debug = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
Loading…
x
Reference in New Issue
Block a user