kos/instance.go

84 lines
1.5 KiB
Go

package kos
import (
"git.nspix.com/golang/kos/entry/cli"
"git.nspix.com/golang/kos/entry/http"
_ "git.nspix.com/golang/kos/pkg/request"
_ "git.nspix.com/golang/kos/util/arrays"
_ "git.nspix.com/golang/kos/util/bs"
_ "git.nspix.com/golang/kos/util/fetch"
_ "git.nspix.com/golang/kos/util/humanize"
_ "git.nspix.com/golang/kos/util/random"
_ "git.nspix.com/golang/kos/util/reflection"
_ "git.nspix.com/golang/kos/util/sys"
"sync"
)
var (
once sync.Once
app Application
)
func initialization(cbs ...Option) {
once.Do(func() {
app = New(cbs...)
})
}
func Init(cbs ...Option) Application {
initialization(cbs...)
return app
}
func Name() string {
initialization()
return app.Info().Name
}
func ShortName() string {
initialization()
if entry, ok := app.(*application); ok {
return entry.opts.ShortName()
}
return app.Info().Name
}
func Version() string {
initialization()
return app.Info().Version
}
func Debug(args ...any) bool {
initialization()
if entry, ok := app.(*application); ok {
if len(args) <= 0 {
return entry.opts.EnableDebug
}
if b, ok := args[0].(bool); ok {
entry.opts.EnableDebug = b
}
return entry.opts.EnableDebug
}
return false
}
func Node() *Info {
initialization()
return app.Info()
}
func Http() *http.Server {
initialization()
return app.Http()
}
func Command() *cli.Server {
initialization()
return app.Command()
}
func Handle(method string, cb HandleFunc) {
initialization()
app.Handle(method, cb)
}