mirror of
https://github.com/astaxie/beego.git
synced 2025-06-12 08:00:40 +00:00
remove config API's context parameter
This commit is contained in:
@ -15,7 +15,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
context2 "context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -301,11 +300,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(nil, "RunMode"); runMode != "" && err == nil {
|
||||
} else if runMode, err := ac.String("RunMode"); runMode != "" && err == nil {
|
||||
BConfig.RunMode = runMode
|
||||
}
|
||||
|
||||
if sd, err := ac.String(nil, "StaticDir"); sd != "" && err == nil {
|
||||
if sd, err := ac.String("StaticDir"); sd != "" && err == nil {
|
||||
BConfig.WebConfig.StaticDir = map[string]string{}
|
||||
sds := strings.Fields(sd)
|
||||
for _, v := range sds {
|
||||
@ -317,7 +316,7 @@ func assignConfig(ac config.Configer) error {
|
||||
}
|
||||
}
|
||||
|
||||
if sgz, err := ac.String(nil, "StaticExtensionsToGzip"); sgz != "" && err == nil {
|
||||
if sgz, err := ac.String("StaticExtensionsToGzip"); sgz != "" && err == nil {
|
||||
extensions := strings.Split(sgz, ",")
|
||||
fileExts := []string{}
|
||||
for _, ext := range extensions {
|
||||
@ -335,15 +334,15 @@ func assignConfig(ac config.Configer) error {
|
||||
}
|
||||
}
|
||||
|
||||
if sfs, err := ac.Int(nil, "StaticCacheFileSize"); err == nil {
|
||||
if sfs, err := ac.Int("StaticCacheFileSize"); err == nil {
|
||||
BConfig.WebConfig.StaticCacheFileSize = sfs
|
||||
}
|
||||
|
||||
if sfn, err := ac.Int(nil, "StaticCacheFileNum"); err == nil {
|
||||
if sfn, err := ac.Int("StaticCacheFileNum"); err == nil {
|
||||
BConfig.WebConfig.StaticCacheFileNum = sfn
|
||||
}
|
||||
|
||||
if lo, err := ac.String(nil, "LogOutputs"); lo != "" && err == nil {
|
||||
if lo, err := ac.String("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
|
||||
@ -390,11 +389,11 @@ func assignSingleConfig(p interface{}, ac config.Configer) {
|
||||
name := pt.Field(i).Name
|
||||
switch pf.Kind() {
|
||||
case reflect.String:
|
||||
pf.SetString(ac.DefaultString(nil, name, pf.String()))
|
||||
pf.SetString(ac.DefaultString(name, pf.String()))
|
||||
case reflect.Int, reflect.Int64:
|
||||
pf.SetInt(ac.DefaultInt64(nil, name, pf.Int()))
|
||||
pf.SetInt(ac.DefaultInt64(name, pf.Int()))
|
||||
case reflect.Bool:
|
||||
pf.SetBool(ac.DefaultBool(nil, name, pf.Bool()))
|
||||
pf.SetBool(ac.DefaultBool(name, pf.Bool()))
|
||||
case reflect.Struct:
|
||||
default:
|
||||
// do nothing here
|
||||
@ -433,105 +432,105 @@ func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, err
|
||||
return &beegoAppConfig{innerConfig: ac}, nil
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
}
|
||||
return 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 {
|
||||
func (b *beegoAppConfig) String(key string) (string, error) {
|
||||
if v, err := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" && err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.String(nil, key)
|
||||
return b.innerConfig.String(key)
|
||||
}
|
||||
|
||||
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 {
|
||||
func (b *beegoAppConfig) Strings(key string) ([]string, error) {
|
||||
if v, err := b.innerConfig.Strings(BConfig.RunMode + "::" + key); len(v) > 0 && err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Strings(nil, key)
|
||||
return b.innerConfig.Strings(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Int(ctx context2.Context, key string) (int, error) {
|
||||
if v, err := b.innerConfig.Int(nil, BConfig.RunMode+"::"+key); err == nil {
|
||||
func (b *beegoAppConfig) Int(key string) (int, error) {
|
||||
if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Int(nil, key)
|
||||
return b.innerConfig.Int(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Int64(ctx context2.Context, key string) (int64, error) {
|
||||
if v, err := b.innerConfig.Int64(nil, BConfig.RunMode+"::"+key); err == nil {
|
||||
func (b *beegoAppConfig) Int64(key string) (int64, error) {
|
||||
if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Int64(nil, key)
|
||||
return b.innerConfig.Int64(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Bool(ctx context2.Context, key string) (bool, error) {
|
||||
if v, err := b.innerConfig.Bool(nil, BConfig.RunMode+"::"+key); err == nil {
|
||||
func (b *beegoAppConfig) Bool(key string) (bool, error) {
|
||||
if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Bool(nil, key)
|
||||
return b.innerConfig.Bool(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Float(ctx context2.Context, key string) (float64, error) {
|
||||
if v, err := b.innerConfig.Float(nil, BConfig.RunMode+"::"+key); err == nil {
|
||||
func (b *beegoAppConfig) Float(key string) (float64, error) {
|
||||
if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Float(nil, key)
|
||||
return b.innerConfig.Float(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultString(ctx context2.Context, key string, defaultVal string) string {
|
||||
if v, err := b.String(nil, key); v != "" && err == nil {
|
||||
func (b *beegoAppConfig) DefaultString(key string, defaultVal string) string {
|
||||
if v, err := b.String(key); v != "" && err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultStrings(ctx context2.Context, key string, defaultVal []string) []string {
|
||||
if v, err := b.Strings(ctx, key); len(v) != 0 && err == nil {
|
||||
func (b *beegoAppConfig) DefaultStrings(key string, defaultVal []string) []string {
|
||||
if v, err := b.Strings(key); len(v) != 0 && err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultInt(ctx context2.Context, key string, defaultVal int) int {
|
||||
if v, err := b.Int(ctx, key); err == nil {
|
||||
func (b *beegoAppConfig) DefaultInt(key string, defaultVal int) int {
|
||||
if v, err := b.Int(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultInt64(ctx context2.Context, key string, defaultVal int64) int64 {
|
||||
if v, err := b.Int64(ctx, key); err == nil {
|
||||
func (b *beegoAppConfig) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
if v, err := b.Int64(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultBool(ctx context2.Context, key string, defaultVal bool) bool {
|
||||
if v, err := b.Bool(ctx, key); err == nil {
|
||||
func (b *beegoAppConfig) DefaultBool(key string, defaultVal bool) bool {
|
||||
if v, err := b.Bool(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultFloat(ctx context2.Context, key string, defaultVal float64) float64 {
|
||||
if v, err := b.Float(ctx, key); err == nil {
|
||||
func (b *beegoAppConfig) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
if v, err := b.Float(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DIY(ctx context2.Context, key string) (interface{}, error) {
|
||||
return b.innerConfig.DIY(nil, key)
|
||||
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
|
||||
return b.innerConfig.DIY(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) GetSection(ctx context2.Context, section string) (map[string]string, error) {
|
||||
return b.innerConfig.GetSection(nil, section)
|
||||
func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
|
||||
return b.innerConfig.GetSection(section)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) SaveConfigFile(ctx context2.Context, filename string) error {
|
||||
return b.innerConfig.SaveConfigFile(nil, filename)
|
||||
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
|
||||
return b.innerConfig.SaveConfigFile(filename)
|
||||
}
|
||||
|
@ -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(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")
|
||||
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")
|
||||
assignConfig(ac)
|
||||
|
||||
t.Logf("%#v", BConfig)
|
||||
|
@ -1,7 +1,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
context2 "context"
|
||||
"encoding/json"
|
||||
"mime"
|
||||
"net/http"
|
||||
@ -48,7 +47,7 @@ func registerDefaultErrorHandler() error {
|
||||
func registerSession() error {
|
||||
if BConfig.WebConfig.Session.SessionOn {
|
||||
var err error
|
||||
sessionConfig, err := AppConfig.String(nil, "sessionConfig")
|
||||
sessionConfig, err := AppConfig.String("sessionConfig")
|
||||
conf := new(session.ManagerConfig)
|
||||
if sessionConfig == "" || err != nil {
|
||||
conf.CookieName = BConfig.WebConfig.Session.SessionName
|
||||
@ -89,9 +88,9 @@ func registerTemplate() error {
|
||||
func registerGzip() error {
|
||||
if BConfig.EnableGzip {
|
||||
context.InitGzip(
|
||||
AppConfig.DefaultInt(context2.Background(), "gzipMinLength", -1),
|
||||
AppConfig.DefaultInt(context2.Background(), "gzipCompressLevel", -1),
|
||||
AppConfig.DefaultStrings(context2.Background(), "includedMethods", []string{"GET"}),
|
||||
AppConfig.DefaultInt("gzipMinLength", -1),
|
||||
AppConfig.DefaultInt("gzipCompressLevel", -1),
|
||||
AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
|
@ -15,7 +15,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -222,7 +221,7 @@ func buildMethodParams(funcParams []*ast.Field, pc *parsedComment) []*param.Meth
|
||||
func buildMethodParam(fparam *ast.Field, name string, pc *parsedComment) *param.MethodParam {
|
||||
options := []param.MethodParamOption{}
|
||||
if cparam, ok := pc.params[name]; ok {
|
||||
//Build param from comment info
|
||||
// Build param from comment info
|
||||
name = cparam.name
|
||||
if cparam.required {
|
||||
options = append(options, param.IsRequired)
|
||||
@ -359,10 +358,10 @@ filterLoop:
|
||||
methods := matches[2]
|
||||
if methods == "" {
|
||||
pc.methods = []string{"get"}
|
||||
//pc.hasGet = true
|
||||
// pc.hasGet = true
|
||||
} else {
|
||||
pc.methods = strings.Split(methods, ",")
|
||||
//pc.hasGet = strings.Contains(methods, "get")
|
||||
// pc.hasGet = strings.Contains(methods, "get")
|
||||
}
|
||||
pcs = append(pcs, pc)
|
||||
} else {
|
||||
@ -517,7 +516,7 @@ func genRouterCode(pkgRealpath string) {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
routersDir := AppConfig.DefaultString(context.Background(), "routersdir", "routers")
|
||||
routersDir := AppConfig.DefaultString("routersdir", "routers")
|
||||
content := strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1)
|
||||
content = strings.Replace(content, "{{.routersDir}}", routersDir, -1)
|
||||
content = strings.Replace(content, "{{.globalimport}}", globalimport, -1)
|
||||
@ -586,7 +585,7 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
|
||||
func getRouterDir(pkgRealpath string) string {
|
||||
dir := filepath.Dir(pkgRealpath)
|
||||
for {
|
||||
routersDir := AppConfig.DefaultString(context.Background(), "routersdir", "routers")
|
||||
routersDir := AppConfig.DefaultString("routersdir", "routers")
|
||||
d := filepath.Join(dir, routersDir)
|
||||
if utils.FileExists(d) {
|
||||
return d
|
||||
|
@ -15,7 +15,6 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
@ -161,17 +160,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(context.Background(), key)
|
||||
value, err = AppConfig.String(key)
|
||||
case "Bool":
|
||||
value, err = AppConfig.Bool(context.Background(), key)
|
||||
value, err = AppConfig.Bool(key)
|
||||
case "Int":
|
||||
value, err = AppConfig.Int(context.Background(), key)
|
||||
value, err = AppConfig.Int(key)
|
||||
case "Int64":
|
||||
value, err = AppConfig.Int64(context.Background(), key)
|
||||
value, err = AppConfig.Int64(key)
|
||||
case "Float":
|
||||
value, err = AppConfig.Float(context.Background(), key)
|
||||
value, err = AppConfig.Float(key)
|
||||
case "DIY":
|
||||
value, err = AppConfig.DIY(context.Background(), key)
|
||||
value, err = AppConfig.DIY(key)
|
||||
default:
|
||||
err = errors.New("config keys must be of type String, Bool, Int, Int64, Float, or DIY")
|
||||
}
|
||||
|
Reference in New Issue
Block a user