commit 5efa5d877eecb50b569dc81edf73fb8b924eeae8 Author: bryanqiu Date: Wed Jun 7 09:44:56 2023 +0800 initial version diff --git a/app.go b/app.go new file mode 100644 index 0000000..378ed8f --- /dev/null +++ b/app.go @@ -0,0 +1,85 @@ +package app + +import ( + "errors" + + "qoobing.com/gomod/cache" + "qoobing.com/gomod/log" +) + +var ( + appCacher cache.Cacher[App] = nil + ErrAppNotExist = errors.New("app is not exist") + ErrAppidInvalid = errors.New("app is invalid") +) + +type App struct { + AppId string `json:"appid"` + AppName string `json:"appname"` + AppType string `json:"apptype"` + AppTitle string `json:"apptitle"` + AppSecureKey string `json:"appseckey"` + AppSpecialConfig map[string]interface{} `json:"appconfigs"` +} + +func (a *App) Config(name string) interface{} { + if confstr, ok := a.AppSpecialConfig[name]; !ok { + return nil + } else { + return confstr + } +} + +func (a *App) ConfigInt(name string, defaultvalue int) int { + if c := a.Config(name); c != nil { + return c.(int) + } + return defaultvalue +} + +func (a *App) ConfigBool(name string, defaultvalue bool) bool { + if c := a.Config(name); c != nil { + return c.(bool) + } + return defaultvalue +} + +func (a *App) ConfigString(name string, defaultvalue string) string { + if c := a.Config(name); c != nil { + return c.(string) + } + return defaultvalue +} + +type ( + Options = cache.Config + AppStorager = cache.Getter[App] +) + +// Init initial the app storager. +// +// Example for app initializer: +// +// Example for app config user: +func Init(storager AppStorager, options Options) { + appCacher = cache.NewCache[App](storager, options) +} + +// GetAppByAppId get app config by appid. +func GetAppByAppId(appid string) (*App, error) { + // Step 1. check appid checksum + // TODO: check appid checksum + if len(appid) < 8 { + return nil, ErrAppidInvalid + } + + // Step 2. get from cache + var app, err = appCacher.GetFromCache(appid) + if err != nil { + return nil, ErrAppidInvalid + } + + // Step 3. return + log.PrintPretty("success cached/return app:", app) + return app, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1f6aa40 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module qoobing.com/gomod/app + +go 1.19