1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-12 19:10:40 +00:00

Add contect as first parameter for all config method

This commit is contained in:
Ming Deng
2020-08-29 15:05:18 +00:00
parent e831b97eb8
commit 03bec05714
19 changed files with 351 additions and 356 deletions

View File

@ -15,6 +15,7 @@
package web
import (
context2 "context"
"fmt"
"os"
"path/filepath"
@ -292,11 +293,11 @@ func assignConfig(ac config.Configer) error {
// set the run mode first
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
BConfig.RunMode = envRunMode
} else if runMode, err := ac.String("RunMode"); runMode != "" && err == nil {
} else if runMode, err := ac.String(nil, "RunMode"); runMode != "" && err == nil {
BConfig.RunMode = runMode
}
if sd, err := ac.String("StaticDir"); sd != "" && err == nil {
if sd, err := ac.String(nil, "StaticDir"); sd != "" && err == nil {
BConfig.WebConfig.StaticDir = map[string]string{}
sds := strings.Fields(sd)
for _, v := range sds {
@ -308,7 +309,7 @@ func assignConfig(ac config.Configer) error {
}
}
if sgz, err := ac.String("StaticExtensionsToGzip"); sgz != "" && err == nil {
if sgz, err := ac.String(nil, "StaticExtensionsToGzip"); sgz != "" && err == nil {
extensions := strings.Split(sgz, ",")
fileExts := []string{}
for _, ext := range extensions {
@ -326,15 +327,15 @@ func assignConfig(ac config.Configer) error {
}
}
if sfs, err := ac.Int("StaticCacheFileSize"); err == nil {
if sfs, err := ac.Int(nil, "StaticCacheFileSize"); err == nil {
BConfig.WebConfig.StaticCacheFileSize = sfs
}
if sfn, err := ac.Int("StaticCacheFileNum"); err == nil {
if sfn, err := ac.Int(nil, "StaticCacheFileNum"); err == nil {
BConfig.WebConfig.StaticCacheFileNum = sfn
}
if lo, err := ac.String("LogOutputs"); lo != "" && err == nil {
if lo, err := ac.String(nil, "LogOutputs"); lo != "" && err == nil {
// if lo is not nil or empty
// means user has set his own LogOutputs
// clear the default setting to BConfig.Log.Outputs
@ -381,11 +382,11 @@ func assignSingleConfig(p interface{}, ac config.Configer) {
name := pt.Field(i).Name
switch pf.Kind() {
case reflect.String:
pf.SetString(ac.DefaultString(name, pf.String()))
pf.SetString(ac.DefaultString(nil, name, pf.String()))
case reflect.Int, reflect.Int64:
pf.SetInt(ac.DefaultInt64(name, pf.Int()))
pf.SetInt(ac.DefaultInt64(nil, name, pf.Int()))
case reflect.Bool:
pf.SetBool(ac.DefaultBool(name, pf.Bool()))
pf.SetBool(ac.DefaultBool(nil, name, pf.Bool()))
case reflect.Struct:
default:
// do nothing here
@ -424,105 +425,105 @@ func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, err
return &beegoAppConfig{innerConfig: ac}, nil
}
func (b *beegoAppConfig) Set(key, val string) error {
if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil {
return b.innerConfig.Set(key, val)
func (b *beegoAppConfig) Set(ctx context2.Context, key, val string) error {
if err := b.innerConfig.Set(nil, BConfig.RunMode+"::"+key, val); err != nil {
return b.innerConfig.Set(nil, key, val)
}
return nil
}
func (b *beegoAppConfig) String(key string) (string, error) {
if v, err := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" && err == nil {
func (b *beegoAppConfig) String(ctx context2.Context, key string) (string, error) {
if v, err := b.innerConfig.String(nil, BConfig.RunMode+"::"+key); v != "" && err == nil {
return v, nil
}
return b.innerConfig.String(key)
return b.innerConfig.String(nil, key)
}
func (b *beegoAppConfig) Strings(key string) ([]string, error) {
if v, err := b.innerConfig.Strings(BConfig.RunMode + "::" + key); len(v) > 0 && err == nil {
func (b *beegoAppConfig) Strings(ctx context2.Context, key string) ([]string, error) {
if v, err := b.innerConfig.Strings(nil, BConfig.RunMode+"::"+key); len(v) > 0 && err == nil {
return v, nil
}
return b.innerConfig.Strings(key)
return b.innerConfig.Strings(nil, key)
}
func (b *beegoAppConfig) Int(key string) (int, error) {
if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Int(ctx context2.Context, key string) (int, error) {
if v, err := b.innerConfig.Int(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Int(key)
return b.innerConfig.Int(nil, key)
}
func (b *beegoAppConfig) Int64(key string) (int64, error) {
if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Int64(ctx context2.Context, key string) (int64, error) {
if v, err := b.innerConfig.Int64(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Int64(key)
return b.innerConfig.Int64(nil, key)
}
func (b *beegoAppConfig) Bool(key string) (bool, error) {
if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Bool(ctx context2.Context, key string) (bool, error) {
if v, err := b.innerConfig.Bool(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Bool(key)
return b.innerConfig.Bool(nil, key)
}
func (b *beegoAppConfig) Float(key string) (float64, error) {
if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Float(ctx context2.Context, key string) (float64, error) {
if v, err := b.innerConfig.Float(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Float(key)
return b.innerConfig.Float(nil, key)
}
func (b *beegoAppConfig) DefaultString(key string, defaultVal string) string {
if v, err := b.String(key); v != "" && err == nil {
func (b *beegoAppConfig) DefaultString(ctx context2.Context, key string, defaultVal string) string {
if v, err := b.String(nil, key); v != "" && err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultStrings(key string, defaultVal []string) []string {
if v, err := b.Strings(key); len(v) != 0 && err == nil {
func (b *beegoAppConfig) DefaultStrings(ctx context2.Context, key string, defaultVal []string) []string {
if v, err := b.Strings(ctx, key); len(v) != 0 && err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultInt(key string, defaultVal int) int {
if v, err := b.Int(key); err == nil {
func (b *beegoAppConfig) DefaultInt(ctx context2.Context, key string, defaultVal int) int {
if v, err := b.Int(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultInt64(key string, defaultVal int64) int64 {
if v, err := b.Int64(key); err == nil {
func (b *beegoAppConfig) DefaultInt64(ctx context2.Context, key string, defaultVal int64) int64 {
if v, err := b.Int64(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultBool(key string, defaultVal bool) bool {
if v, err := b.Bool(key); err == nil {
func (b *beegoAppConfig) DefaultBool(ctx context2.Context, key string, defaultVal bool) bool {
if v, err := b.Bool(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultFloat(key string, defaultVal float64) float64 {
if v, err := b.Float(key); err == nil {
func (b *beegoAppConfig) DefaultFloat(ctx context2.Context, key string, defaultVal float64) float64 {
if v, err := b.Float(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
return b.innerConfig.DIY(key)
func (b *beegoAppConfig) DIY(ctx context2.Context, key string) (interface{}, error) {
return b.innerConfig.DIY(nil, key)
}
func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
return b.innerConfig.GetSection(section)
func (b *beegoAppConfig) GetSection(ctx context2.Context, section string) (map[string]string, error) {
return b.innerConfig.GetSection(nil, section)
}
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
return b.innerConfig.SaveConfigFile(filename)
func (b *beegoAppConfig) SaveConfigFile(ctx context2.Context, filename string) error {
return b.innerConfig.SaveConfigFile(nil, filename)
}

View File

@ -111,12 +111,12 @@ func TestAssignConfig_02(t *testing.T) {
func TestAssignConfig_03(t *testing.T) {
jcf := &beeJson.JSONConfig{}
ac, _ := jcf.ParseData([]byte(`{"AppName":"beego"}`))
ac.Set("AppName", "test_app")
ac.Set("RunMode", "online")
ac.Set("StaticDir", "download:down download2:down2")
ac.Set("StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
ac.Set("StaticCacheFileSize", "87456")
ac.Set("StaticCacheFileNum", "1254")
ac.Set(nil, "AppName", "test_app")
ac.Set(nil, "RunMode", "online")
ac.Set(nil, "StaticDir", "download:down download2:down2")
ac.Set(nil, "StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
ac.Set(nil, "StaticCacheFileSize", "87456")
ac.Set(nil, "StaticCacheFileNum", "1254")
assignConfig(ac)
t.Logf("%#v", BConfig)

View File

@ -1,6 +1,7 @@
package web
import (
context2 "context"
"encoding/json"
"mime"
"net/http"
@ -48,7 +49,7 @@ func registerDefaultErrorHandler() error {
func registerSession() error {
if BConfig.WebConfig.Session.SessionOn {
var err error
sessionConfig, err := AppConfig.String("sessionConfig")
sessionConfig, err := AppConfig.String(nil, "sessionConfig")
conf := new(session.ManagerConfig)
if sessionConfig == "" || err != nil {
conf.CookieName = BConfig.WebConfig.Session.SessionName
@ -96,9 +97,9 @@ func registerAdmin() error {
func registerGzip() error {
if BConfig.EnableGzip {
context.InitGzip(
AppConfig.DefaultInt("gzipMinLength", -1),
AppConfig.DefaultInt("gzipCompressLevel", -1),
AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
AppConfig.DefaultInt(context2.Background(), "gzipMinLength", -1),
AppConfig.DefaultInt(context2.Background(), "gzipCompressLevel", -1),
AppConfig.DefaultStrings(context2.Background(), "includedMethods", []string{"GET"}),
)
}
return nil

View File

@ -15,6 +15,7 @@
package web
import (
"context"
"encoding/json"
"errors"
"fmt"
@ -516,7 +517,7 @@ func genRouterCode(pkgRealpath string) {
}
defer f.Close()
routersDir := AppConfig.DefaultString("routersdir", "routers")
routersDir := AppConfig.DefaultString(context.Background(), "routersdir", "routers")
content := strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1)
content = strings.Replace(content, "{{.routersDir}}", routersDir, -1)
content = strings.Replace(content, "{{.globalimport}}", globalimport, -1)
@ -585,7 +586,7 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
func getRouterDir(pkgRealpath string) string {
dir := filepath.Dir(pkgRealpath)
for {
routersDir := AppConfig.DefaultString("routersdir", "routers")
routersDir := AppConfig.DefaultString(context.Background(), "routersdir", "routers")
d := filepath.Join(dir, routersDir)
if utils.FileExists(d) {
return d

View File

@ -15,6 +15,7 @@
package web
import (
"context"
"errors"
"fmt"
"html"
@ -58,11 +59,11 @@ func HTML2str(html string) string {
re := regexp.MustCompile(`\<[\S\s]+?\>`)
html = re.ReplaceAllStringFunc(html, strings.ToLower)
//remove STYLE
// remove STYLE
re = regexp.MustCompile(`\<style[\S\s]+?\</style\>`)
html = re.ReplaceAllString(html, "")
//remove SCRIPT
// remove SCRIPT
re = regexp.MustCompile(`\<script[\S\s]+?\</script\>`)
html = re.ReplaceAllString(html, "")
@ -85,7 +86,7 @@ func DateFormat(t time.Time, layout string) (datestring string) {
var datePatterns = []string{
// year
"Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
"y", "06", //A two digit representation of a year Examples: 99 or 03
"y", "06", // A two digit representation of a year Examples: 99 or 03
// month
"m", "01", // Numeric representation of a month, with leading zeros 01 through 12
@ -160,17 +161,17 @@ func NotNil(a interface{}) (isNil bool) {
func GetConfig(returnType, key string, defaultVal interface{}) (value interface{}, err error) {
switch returnType {
case "String":
value, err = AppConfig.String(key)
value, err = AppConfig.String(context.Background(), key)
case "Bool":
value, err = AppConfig.Bool(key)
value, err = AppConfig.Bool(context.Background(), key)
case "Int":
value, err = AppConfig.Int(key)
value, err = AppConfig.Int(context.Background(), key)
case "Int64":
value, err = AppConfig.Int64(key)
value, err = AppConfig.Int64(context.Background(), key)
case "Float":
value, err = AppConfig.Float(key)
value, err = AppConfig.Float(context.Background(), key)
case "DIY":
value, err = AppConfig.DIY(key)
value, err = AppConfig.DIY(context.Background(), key)
default:
err = errors.New("config keys must be of type String, Bool, Int, Int64, Float, or DIY")
}
@ -201,7 +202,7 @@ func Str2html(raw string) template.HTML {
// Htmlquote returns quoted html string.
func Htmlquote(text string) string {
//HTML编码为实体符号
// HTML编码为实体符号
/*
Encodes `text` for raw use in HTML.
>>> htmlquote("<'&\\">")
@ -220,7 +221,7 @@ func Htmlquote(text string) string {
// Htmlunquote returns unquoted html string.
func Htmlunquote(text string) string {
//实体符号解释为HTML
// 实体符号解释为HTML
/*
Decodes `text` that's HTML quoted.
>>> htmlunquote('&lt;&#39;&amp;&quot;&gt;')