appx

package
v0.2.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 16 Imported by: 0

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 BatchRun

func BatchRun(runners ...func())

func BatchRunSync

func BatchRunSync(runners ...func())

Types

type AppCtx

type AppCtx struct {
	*cobra.Command
	// contains filtered or unexported fields
}

func NewAppContext

func NewAppContext(options ...Option) *AppCtx
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

func (*AppCtx) Conf

func (app *AppCtx) Conf(ctx context.Context, configurations ...any)

func (*AppCtx) MainRoot

func (app *AppCtx) MainRoot() string

func (*AppCtx) Name

func (app *AppCtx) Name() string

func (*AppCtx) Version

func (app *AppCtx) Version() string

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())

func (*AppOption) PreRun

func (o *AppOption) PreRun()

type Meta

type Meta struct {
	Name     string  `json:"name"`
	Feature  string  `json:"feature"`
	Version  string  `json:"version"`
	CommitID string  `json:"commit"`
	Date     string  `json:"date"`
	Runtime  Runtime `json:"runtime"`
}

func (*Meta) Overwrite

func (m *Meta) Overwrite(meta Meta)

func (*Meta) Print

func (m *Meta) Print()

func (*Meta) String

func (m *Meta) String() string

type Option

type Option func(*AppCtx)

func WithBatchRunner

func WithBatchRunner(runners ...func()) Option

func WithBuildMeta

func WithBuildMeta(meta Meta) Option

func WithMainExecutor

func WithMainExecutor(main func() error) Option

func WithMainRoot

func WithMainRoot(root string) Option

func WithPreRunner

func WithPreRunner(runners ...func()) Option

type Runtime

type Runtime string
const (
	RUNTIME_PROD    Runtime = "PROD"
	RUNTIME_STAGING Runtime = "STAGING"
	RUNTIME_DEV     Runtime = "DEV"
)

func GetRuntime

func GetRuntime() Runtime

func (Runtime) String

func (r Runtime) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL