Documentation
¶
Overview ¶
Example (Init_failed) ¶
_ = os.Setenv("APP__SomeKey", "")
root := "./testdata2"
app := NewAppContext(
WithBuildMeta(Meta{Name: "app"}),
WithMainRoot(root),
)
defer os.RemoveAll(root)
defer func() {
fmt.Println(recover())
}()
app.Conf(context.Background(), &struct {
SomeKey MustInitFailed
}{})
Output: failed to init [group:APP] [field:SomeKey]: must fail
Index ¶
Examples ¶
Constants ¶
View Source
const RuntimeKey = "GO_RUNTIME"
Variables ¶
View Source
var DefaultMeta = Meta{ Name: "name", Feature: "branch", Version: "version", CommitID: "commit", Date: time.Now().Format("200601021504"), Runtime: GetRuntime(), }
Functions ¶
func BatchRunSync ¶
func BatchRunSync(runners ...func())
Types ¶
type AppCtx ¶
func NewAppContext ¶
Example ¶
root := "./testdata1"
app := NewAppContext(
WithMainRoot(root),
WithBuildMeta(Meta{
Name: "app",
Feature: "main",
Version: "v0.0.1",
CommitID: "efbecda",
Date: "200601021504",
Runtime: RUNTIME_DEV,
}),
WithBatchRunner(
func() {
time.Sleep(time.Second * 1)
fmt.Println("batch runner 1")
},
func() {
time.Sleep(time.Second * 2)
fmt.Println("batch runner 2")
},
),
WithPreRunner(
func() {
time.Sleep(time.Second * 1)
fmt.Println("pre runner 1")
},
func() {
time.Sleep(time.Second * 2)
fmt.Println("pre runner 2")
},
),
WithMainExecutor(
func() error {
time.Sleep(time.Second * 3)
fmt.Println("main entry")
return nil
},
),
)
must.NoError(os.MkdirAll(filepath.Join(app.MainRoot(), "config"), os.ModePerm))
must.NoError(os.WriteFile(filepath.Join(app.MainRoot(), "config/local.yml"), []byte(`
APP__CONFIG1__Endpoint_Address: "postgres://hostname:5432/base"
APP__CONFIG1__WorkerID: "100"
APP__CONFIG2__ClientEndpoint_Address: http://localhost:80/demo
APP__CONFIG2__ServerPort: "8080"
`), os.ModePerm))
defer os.RemoveAll(root)
config1 := &Config1{}
config2 := &Config2{}
app.Conf(context.Background(), config1, config2)
cmd := app.Command
buf := bytes.NewBuffer(nil)
cmd.SetOut(buf)
cmd.SetErr(buf)
{
fmt.Println("exec `app version`")
buf.Reset()
cmd.SetArgs([]string{"version"})
must.NoError(cmd.Execute())
fmt.Println(buf.String())
}
{
fmt.Println("exec `app run`")
buf.Reset()
cmd.SetArgs([]string{"run"})
must.NoError(cmd.Execute())
fmt.Println(buf.String())
}
Output: exec `app version` app:main@v0.0.1#efbecda_200601021504(DEV) exec `app run` app:main@v0.0.1#efbecda_200601021504(DEV) name: app feature: main version: v0.0.1 commit: efbecda date: 200601021504 runtime: DEV APP__CONFIG1__Endpoint_Address=postgres://hostname:5432/base APP__CONFIG1__Endpoint_Auth_DecryptKeyEnv=PASSWORD_DEC_KEY APP__CONFIG1__Endpoint_Auth_Password=-------- APP__CONFIG1__Endpoint_Auth_Username= APP__CONFIG1__Endpoint_Cert_CA= APP__CONFIG1__Endpoint_Cert_Crt= APP__CONFIG1__Endpoint_Cert_Key= APP__CONFIG1__WorkerID=100 APP__CONFIG2__ClientEndpoint_Address=http://localhost:80/demo APP__CONFIG2__ClientEndpoint_Auth_DecryptKeyEnv=PASSWORD_DEC_KEY APP__CONFIG2__ClientEndpoint_Auth_Password=-------- APP__CONFIG2__ClientEndpoint_Auth_Username= APP__CONFIG2__ClientEndpoint_Cert_CA= APP__CONFIG2__ClientEndpoint_Cert_Crt= APP__CONFIG2__ClientEndpoint_Cert_Key= APP__CONFIG2__ServerPort=8080 pre runner 1 pre runner 2 batch runner 1 batch runner 2 main entry
type AppOption ¶
type AppOption struct {
Meta
// PreRunner must run before main
PreRunners []func()
// BatchRunner routines need pre-run before enter main, e.g. modules initializations
BatchRunners []func()
}
func (*AppOption) AppendBatchRunners ¶
func (o *AppOption) AppendBatchRunners(runners ...func())
func (*AppOption) AppendPreRunners ¶
func (o *AppOption) AppendPreRunners(runners ...func())
type Meta ¶
type Option ¶
type Option func(*AppCtx)
func WithBatchRunner ¶
func WithBatchRunner(runners ...func()) Option
func WithBuildMeta ¶
func WithMainExecutor ¶
func WithMainRoot ¶
func WithPreRunner ¶
func WithPreRunner(runners ...func()) Option
Click to show internal directories.
Click to hide internal directories.