initial version

This commit is contained in:
bryanqiu 2023-06-07 09:44:56 +08:00
commit 5efa5d877e
2 changed files with 88 additions and 0 deletions

85
app.go Normal file
View File

@ -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
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module qoobing.com/gomod/app
go 1.19