kos/instance.go

84 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package kos
import (
2024-11-12 17:47:28 +08:00
"git.nobla.cn/golang/kos/entry/cli"
"git.nobla.cn/golang/kos/entry/http"
_ "git.nobla.cn/golang/kos/pkg/request"
_ "git.nobla.cn/golang/kos/util/arrays"
_ "git.nobla.cn/golang/kos/util/bs"
_ "git.nobla.cn/golang/kos/util/fetch"
_ "git.nobla.cn/golang/kos/util/humanize"
_ "git.nobla.cn/golang/kos/util/random"
_ "git.nobla.cn/golang/kos/util/reflection"
_ "git.nobla.cn/golang/kos/util/sys"
2023-04-23 17:57:36 +08:00
"sync"
)
var (
once sync.Once
2024-04-29 14:58:03 +08:00
app Application
2023-04-23 17:57:36 +08:00
)
2024-04-29 10:52:19 +08:00
func initialization(cbs ...Option) {
2023-04-23 17:57:36 +08:00
once.Do(func() {
2024-04-29 14:58:03 +08:00
app = New(cbs...)
2023-04-23 17:57:36 +08:00
})
}
2024-04-29 10:52:19 +08:00
func Init(cbs ...Option) Application {
initialization(cbs...)
2024-04-29 14:58:03 +08:00
return app
2023-04-23 17:57:36 +08:00
}
2023-10-23 15:44:44 +08:00
func Name() string {
2024-04-29 10:52:19 +08:00
initialization()
2024-04-29 14:58:03 +08:00
return app.Info().Name
2023-10-23 15:44:44 +08:00
}
2024-04-29 10:52:19 +08:00
func ShortName() string {
initialization()
2024-04-29 14:58:03 +08:00
if entry, ok := app.(*application); ok {
return entry.opts.ShortName()
}
return app.Info().Name
2024-04-29 10:52:19 +08:00
}
2023-10-23 15:44:44 +08:00
func Version() string {
2024-04-29 10:52:19 +08:00
initialization()
2024-04-29 14:58:03 +08:00
return app.Info().Version
2023-10-23 15:44:44 +08:00
}
2024-04-29 10:52:19 +08:00
func Debug(args ...any) bool {
initialization()
2024-04-29 14:58:03 +08:00
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
2024-04-29 10:52:19 +08:00
}
2024-04-29 14:58:03 +08:00
return false
2023-10-23 15:43:46 +08:00
}
2024-04-29 10:52:19 +08:00
func Node() *Info {
initialization()
2024-04-29 14:58:03 +08:00
return app.Info()
2023-04-23 17:57:36 +08:00
}
2024-04-29 10:52:19 +08:00
func Http() *http.Server {
initialization()
2024-04-29 14:58:03 +08:00
return app.Http()
2024-04-29 10:52:19 +08:00
}
func Command() *cli.Server {
initialization()
2024-04-29 14:58:03 +08:00
return app.Command()
2023-10-23 15:47:37 +08:00
}
2023-04-23 17:57:36 +08:00
func Handle(method string, cb HandleFunc) {
2024-04-29 10:52:19 +08:00
initialization()
2024-04-29 14:58:03 +08:00
app.Handle(method, cb)
2023-04-23 17:57:36 +08:00
}