2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-12-03 05:15:32 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"os"
|
2013-12-19 04:22:30 +00:00
|
|
|
"path/filepath"
|
2013-12-03 05:15:32 +00:00
|
|
|
"strings"
|
2016-01-26 16:10:21 +00:00
|
|
|
"fmt"
|
2013-12-03 11:26:51 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/config"
|
|
|
|
"github.com/astaxie/beego/session"
|
2014-04-01 10:08:00 +00:00
|
|
|
"github.com/astaxie/beego/utils"
|
2013-12-03 05:15:32 +00:00
|
|
|
)
|
|
|
|
|
2016-01-17 16:22:11 +00:00
|
|
|
// Config is the main struct for BConfig
|
2016-01-17 16:18:21 +00:00
|
|
|
type Config struct {
|
2015-12-09 15:35:04 +00:00
|
|
|
AppName string //Application name
|
|
|
|
RunMode string //Running Mode: dev | prod
|
2015-09-07 15:19:42 +00:00
|
|
|
RouterCaseSensitive bool
|
2015-12-09 15:35:04 +00:00
|
|
|
ServerName string
|
|
|
|
RecoverPanic bool
|
|
|
|
CopyRequestBody bool
|
|
|
|
EnableGzip bool
|
|
|
|
MaxMemory int64
|
|
|
|
EnableErrorsShow bool
|
|
|
|
Listen Listen
|
|
|
|
WebConfig WebConfig
|
|
|
|
Log LogConfig
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 16:18:21 +00:00
|
|
|
// Listen holds for http and https related config
|
2015-12-09 15:35:04 +00:00
|
|
|
type Listen struct {
|
|
|
|
Graceful bool // Graceful means use graceful module to start the server
|
|
|
|
ServerTimeOut int64
|
|
|
|
ListenTCP4 bool
|
2016-01-12 13:55:02 +00:00
|
|
|
EnableHTTP bool
|
2015-12-09 15:35:04 +00:00
|
|
|
HTTPAddr string
|
|
|
|
HTTPPort int
|
2016-01-12 13:55:02 +00:00
|
|
|
EnableHTTPS bool
|
2015-12-09 15:35:04 +00:00
|
|
|
HTTPSAddr string
|
|
|
|
HTTPSPort int
|
|
|
|
HTTPSCertFile string
|
|
|
|
HTTPSKeyFile string
|
2016-01-12 13:55:02 +00:00
|
|
|
EnableAdmin bool
|
2015-12-09 15:35:04 +00:00
|
|
|
AdminAddr string
|
|
|
|
AdminPort int
|
|
|
|
EnableFcgi bool
|
|
|
|
EnableStdIo bool // EnableStdIo works with EnableFcgi Use FCGI via standard I/O
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 16:18:21 +00:00
|
|
|
// WebConfig holds web related config
|
2015-12-09 15:35:04 +00:00
|
|
|
type WebConfig struct {
|
|
|
|
AutoRender bool
|
|
|
|
EnableDocs bool
|
|
|
|
FlashName string
|
2016-01-15 06:07:37 +00:00
|
|
|
FlashSeparator string
|
2015-12-09 15:35:04 +00:00
|
|
|
DirectoryIndex bool
|
|
|
|
StaticDir map[string]string
|
|
|
|
StaticExtensionsToGzip []string
|
|
|
|
TemplateLeft string
|
|
|
|
TemplateRight string
|
|
|
|
ViewsPath string
|
|
|
|
EnableXSRF bool
|
2016-01-15 06:07:37 +00:00
|
|
|
XSRFKey string
|
2015-12-09 15:35:04 +00:00
|
|
|
XSRFExpire int
|
|
|
|
Session SessionConfig
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 16:18:21 +00:00
|
|
|
// SessionConfig holds session related config
|
2015-12-09 15:35:04 +00:00
|
|
|
type SessionConfig struct {
|
|
|
|
SessionOn bool
|
|
|
|
SessionProvider string
|
|
|
|
SessionName string
|
|
|
|
SessionGCMaxLifetime int64
|
|
|
|
SessionProviderConfig string
|
|
|
|
SessionCookieLifeTime int
|
|
|
|
SessionAutoSetCookie bool
|
|
|
|
SessionDomain string
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 16:18:21 +00:00
|
|
|
// LogConfig holds Log related config
|
2015-12-09 15:35:04 +00:00
|
|
|
type LogConfig struct {
|
|
|
|
AccessLogs bool
|
|
|
|
FileLineNum bool
|
2016-01-07 17:40:19 +00:00
|
|
|
Outputs map[string]string // Store Adaptor : config
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 15:35:04 +00:00
|
|
|
var (
|
|
|
|
// BConfig is the default config for Application
|
2016-01-17 16:18:21 +00:00
|
|
|
BConfig *Config
|
2015-12-09 15:35:04 +00:00
|
|
|
// AppConfig is the instance of Config, store the config information from file
|
|
|
|
AppConfig *beegoAppConfig
|
2016-01-23 16:18:16 +00:00
|
|
|
// AppPath is the absolute path to the app
|
|
|
|
AppPath string
|
2015-12-09 15:35:04 +00:00
|
|
|
// TemplateCache stores template caching
|
|
|
|
TemplateCache map[string]*template.Template
|
|
|
|
// GlobalSessions is the instance for the session manager
|
|
|
|
GlobalSessions *session.Manager
|
2016-01-26 16:10:21 +00:00
|
|
|
|
2016-01-25 15:00:09 +00:00
|
|
|
workPath string
|
2016-01-27 08:43:01 +00:00
|
|
|
// appConfigPath is the path to the config files
|
|
|
|
appConfigPath string
|
|
|
|
// appConfigProvider is the provider for the config, default is ini
|
|
|
|
appConfigProvider = "ini"
|
2015-12-09 15:35:04 +00:00
|
|
|
)
|
2014-10-01 14:10:33 +00:00
|
|
|
|
2015-12-09 15:35:04 +00:00
|
|
|
func init() {
|
2016-01-25 15:00:09 +00:00
|
|
|
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
workPath, _ = os.Getwd()
|
|
|
|
workPath, _ = filepath.Abs(workPath)
|
|
|
|
|
2016-01-27 04:13:26 +00:00
|
|
|
if workPath != AppPath {
|
|
|
|
os.Chdir(AppPath)
|
|
|
|
}
|
|
|
|
|
2016-01-17 16:18:21 +00:00
|
|
|
BConfig = &Config{
|
2015-12-09 15:35:04 +00:00
|
|
|
AppName: "beego",
|
2015-12-29 18:32:37 +00:00
|
|
|
RunMode: DEV,
|
2015-12-09 15:35:04 +00:00
|
|
|
RouterCaseSensitive: true,
|
|
|
|
ServerName: "beegoServer:" + VERSION,
|
|
|
|
RecoverPanic: true,
|
|
|
|
CopyRequestBody: false,
|
|
|
|
EnableGzip: false,
|
|
|
|
MaxMemory: 1 << 26, //64MB
|
|
|
|
EnableErrorsShow: true,
|
|
|
|
Listen: Listen{
|
|
|
|
Graceful: false,
|
|
|
|
ServerTimeOut: 0,
|
|
|
|
ListenTCP4: false,
|
2016-01-12 13:55:02 +00:00
|
|
|
EnableHTTP: true,
|
2015-12-09 15:35:04 +00:00
|
|
|
HTTPAddr: "",
|
|
|
|
HTTPPort: 8080,
|
2016-01-12 13:55:02 +00:00
|
|
|
EnableHTTPS: false,
|
2015-12-09 15:35:04 +00:00
|
|
|
HTTPSAddr: "",
|
|
|
|
HTTPSPort: 10443,
|
|
|
|
HTTPSCertFile: "",
|
|
|
|
HTTPSKeyFile: "",
|
2016-01-12 13:55:02 +00:00
|
|
|
EnableAdmin: false,
|
2015-12-09 15:35:04 +00:00
|
|
|
AdminAddr: "",
|
|
|
|
AdminPort: 8088,
|
|
|
|
EnableFcgi: false,
|
|
|
|
EnableStdIo: false,
|
|
|
|
},
|
|
|
|
WebConfig: WebConfig{
|
|
|
|
AutoRender: true,
|
|
|
|
EnableDocs: false,
|
|
|
|
FlashName: "BEEGO_FLASH",
|
2016-01-15 06:07:37 +00:00
|
|
|
FlashSeparator: "BEEGOFLASH",
|
2015-12-09 15:35:04 +00:00
|
|
|
DirectoryIndex: false,
|
|
|
|
StaticDir: map[string]string{"/static": "static"},
|
|
|
|
StaticExtensionsToGzip: []string{".css", ".js"},
|
|
|
|
TemplateLeft: "{{",
|
|
|
|
TemplateRight: "}}",
|
|
|
|
ViewsPath: "views",
|
|
|
|
EnableXSRF: false,
|
2016-01-15 06:07:37 +00:00
|
|
|
XSRFKey: "beegoxsrf",
|
2015-12-09 15:35:04 +00:00
|
|
|
XSRFExpire: 0,
|
|
|
|
Session: SessionConfig{
|
|
|
|
SessionOn: false,
|
|
|
|
SessionProvider: "memory",
|
|
|
|
SessionName: "beegosessionID",
|
|
|
|
SessionGCMaxLifetime: 3600,
|
|
|
|
SessionProviderConfig: "",
|
|
|
|
SessionCookieLifeTime: 0, //set cookie default is the brower life
|
|
|
|
SessionAutoSetCookie: true,
|
|
|
|
SessionDomain: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Log: LogConfig{
|
|
|
|
AccessLogs: false,
|
|
|
|
FileLineNum: true,
|
2016-01-07 17:40:19 +00:00
|
|
|
Outputs: map[string]string{"console": ""},
|
2015-12-09 15:35:04 +00:00
|
|
|
},
|
2015-04-05 15:21:13 +00:00
|
|
|
}
|
2016-01-26 16:10:21 +00:00
|
|
|
|
2016-01-27 08:43:01 +00:00
|
|
|
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
|
|
|
if !utils.FileExists(appConfigPath) {
|
2016-01-26 16:10:21 +00:00
|
|
|
AppConfig = &beegoAppConfig{config.NewFakeConfig()}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-27 08:43:01 +00:00
|
|
|
parseConfig(appConfigPath)
|
2016-01-26 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 15:35:04 +00:00
|
|
|
// now only support ini, next will support json.
|
2016-01-26 16:10:21 +00:00
|
|
|
func parseConfig(appConfigPath string) (err error) {
|
2016-01-27 08:43:01 +00:00
|
|
|
AppConfig, err = newAppConfig(appConfigProvider, appConfigPath)
|
2014-10-24 11:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-01 14:10:33 +00:00
|
|
|
// set the runmode first
|
2015-12-17 12:05:00 +00:00
|
|
|
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.RunMode = envRunMode
|
2014-10-08 19:00:07 +00:00
|
|
|
} else if runmode := AppConfig.String("RunMode"); runmode != "" {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.RunMode = runmode
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
2013-12-03 05:15:32 +00:00
|
|
|
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.AppName = AppConfig.DefaultString("AppName", BConfig.AppName)
|
|
|
|
BConfig.RecoverPanic = AppConfig.DefaultBool("RecoverPanic", BConfig.RecoverPanic)
|
|
|
|
BConfig.RouterCaseSensitive = AppConfig.DefaultBool("RouterCaseSensitive", BConfig.RouterCaseSensitive)
|
2016-01-12 13:55:02 +00:00
|
|
|
BConfig.ServerName = AppConfig.DefaultString("ServerName", BConfig.ServerName)
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.EnableGzip = AppConfig.DefaultBool("EnableGzip", BConfig.EnableGzip)
|
|
|
|
BConfig.EnableErrorsShow = AppConfig.DefaultBool("EnableErrorsShow", BConfig.EnableErrorsShow)
|
|
|
|
BConfig.CopyRequestBody = AppConfig.DefaultBool("CopyRequestBody", BConfig.CopyRequestBody)
|
|
|
|
BConfig.MaxMemory = AppConfig.DefaultInt64("MaxMemory", BConfig.MaxMemory)
|
|
|
|
BConfig.Listen.Graceful = AppConfig.DefaultBool("Graceful", BConfig.Listen.Graceful)
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.Listen.HTTPAddr = AppConfig.String("HTTPAddr")
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.Listen.HTTPPort = AppConfig.DefaultInt("HTTPPort", BConfig.Listen.HTTPPort)
|
|
|
|
BConfig.Listen.ListenTCP4 = AppConfig.DefaultBool("ListenTCP4", BConfig.Listen.ListenTCP4)
|
2016-01-12 13:55:02 +00:00
|
|
|
BConfig.Listen.EnableHTTP = AppConfig.DefaultBool("EnableHTTP", BConfig.Listen.EnableHTTP)
|
|
|
|
BConfig.Listen.EnableHTTPS = AppConfig.DefaultBool("EnableHTTPS", BConfig.Listen.EnableHTTPS)
|
2016-01-07 15:35:01 +00:00
|
|
|
BConfig.Listen.HTTPSAddr = AppConfig.DefaultString("HTTPSAddr", BConfig.Listen.HTTPSAddr)
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.Listen.HTTPSPort = AppConfig.DefaultInt("HTTPSPort", BConfig.Listen.HTTPSPort)
|
2016-01-12 13:55:02 +00:00
|
|
|
BConfig.Listen.HTTPSCertFile = AppConfig.DefaultString("HTTPSCertFile", BConfig.Listen.HTTPSCertFile)
|
|
|
|
BConfig.Listen.HTTPSKeyFile = AppConfig.DefaultString("HTTPSKeyFile", BConfig.Listen.HTTPSKeyFile)
|
|
|
|
BConfig.Listen.EnableAdmin = AppConfig.DefaultBool("EnableAdmin", BConfig.Listen.EnableAdmin)
|
|
|
|
BConfig.Listen.AdminAddr = AppConfig.DefaultString("AdminAddr", BConfig.Listen.AdminAddr)
|
|
|
|
BConfig.Listen.AdminPort = AppConfig.DefaultInt("AdminPort", BConfig.Listen.AdminPort)
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.Listen.EnableFcgi = AppConfig.DefaultBool("EnableFcgi", BConfig.Listen.EnableFcgi)
|
2016-01-07 17:40:19 +00:00
|
|
|
BConfig.Listen.EnableStdIo = AppConfig.DefaultBool("EnableStdIo", BConfig.Listen.EnableStdIo)
|
2016-01-12 13:55:02 +00:00
|
|
|
BConfig.Listen.ServerTimeOut = AppConfig.DefaultInt64("ServerTimeOut", BConfig.Listen.ServerTimeOut)
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.WebConfig.AutoRender = AppConfig.DefaultBool("AutoRender", BConfig.WebConfig.AutoRender)
|
|
|
|
BConfig.WebConfig.ViewsPath = AppConfig.DefaultString("ViewsPath", BConfig.WebConfig.ViewsPath)
|
|
|
|
BConfig.WebConfig.DirectoryIndex = AppConfig.DefaultBool("DirectoryIndex", BConfig.WebConfig.DirectoryIndex)
|
2015-12-09 16:12:52 +00:00
|
|
|
BConfig.WebConfig.FlashName = AppConfig.DefaultString("FlashName", BConfig.WebConfig.FlashName)
|
2016-01-15 06:07:37 +00:00
|
|
|
BConfig.WebConfig.FlashSeparator = AppConfig.DefaultString("FlashSeparator", BConfig.WebConfig.FlashSeparator)
|
2015-12-09 16:12:52 +00:00
|
|
|
BConfig.WebConfig.EnableDocs = AppConfig.DefaultBool("EnableDocs", BConfig.WebConfig.EnableDocs)
|
2016-01-15 06:07:37 +00:00
|
|
|
BConfig.WebConfig.XSRFKey = AppConfig.DefaultString("XSRFKEY", BConfig.WebConfig.XSRFKey)
|
2015-12-09 16:11:24 +00:00
|
|
|
BConfig.WebConfig.EnableXSRF = AppConfig.DefaultBool("EnableXSRF", BConfig.WebConfig.EnableXSRF)
|
|
|
|
BConfig.WebConfig.XSRFExpire = AppConfig.DefaultInt("XSRFExpire", BConfig.WebConfig.XSRFExpire)
|
|
|
|
BConfig.WebConfig.TemplateLeft = AppConfig.DefaultString("TemplateLeft", BConfig.WebConfig.TemplateLeft)
|
|
|
|
BConfig.WebConfig.TemplateRight = AppConfig.DefaultString("TemplateRight", BConfig.WebConfig.TemplateRight)
|
|
|
|
BConfig.WebConfig.Session.SessionOn = AppConfig.DefaultBool("SessionOn", BConfig.WebConfig.Session.SessionOn)
|
|
|
|
BConfig.WebConfig.Session.SessionProvider = AppConfig.DefaultString("SessionProvider", BConfig.WebConfig.Session.SessionProvider)
|
|
|
|
BConfig.WebConfig.Session.SessionName = AppConfig.DefaultString("SessionName", BConfig.WebConfig.Session.SessionName)
|
|
|
|
BConfig.WebConfig.Session.SessionProviderConfig = AppConfig.DefaultString("SessionProviderConfig", BConfig.WebConfig.Session.SessionProviderConfig)
|
|
|
|
BConfig.WebConfig.Session.SessionGCMaxLifetime = AppConfig.DefaultInt64("SessionGCMaxLifetime", BConfig.WebConfig.Session.SessionGCMaxLifetime)
|
|
|
|
BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime)
|
2016-01-07 15:55:55 +00:00
|
|
|
BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie)
|
|
|
|
BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain)
|
2016-01-26 16:10:21 +00:00
|
|
|
BConfig.Log.AccessLogs = AppConfig.DefaultBool("LogAccessLogs", BConfig.Log.AccessLogs)
|
|
|
|
BConfig.Log.FileLineNum = AppConfig.DefaultBool("LogFileLineNum", BConfig.Log.FileLineNum)
|
2015-12-09 16:11:24 +00:00
|
|
|
|
2014-10-01 14:10:33 +00:00
|
|
|
if sd := AppConfig.String("StaticDir"); sd != "" {
|
2015-12-09 15:35:04 +00:00
|
|
|
for k := range BConfig.WebConfig.StaticDir {
|
|
|
|
delete(BConfig.WebConfig.StaticDir, k)
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
|
|
|
sds := strings.Fields(sd)
|
|
|
|
for _, v := range sds {
|
|
|
|
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
|
2014-10-01 14:10:33 +00:00
|
|
|
} else {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0]
|
2013-12-03 05:15:32 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
2013-12-18 15:23:00 +00:00
|
|
|
|
2014-10-01 14:10:33 +00:00
|
|
|
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
|
|
|
|
extensions := strings.Split(sgz, ",")
|
2015-09-22 05:27:35 +00:00
|
|
|
fileExts := []string{}
|
2015-09-22 03:59:48 +00:00
|
|
|
for _, ext := range extensions {
|
|
|
|
ext = strings.TrimSpace(ext)
|
|
|
|
if ext == "" {
|
|
|
|
continue
|
2013-12-14 18:34:27 +00:00
|
|
|
}
|
2015-09-22 03:59:48 +00:00
|
|
|
if !strings.HasPrefix(ext, ".") {
|
|
|
|
ext = "." + ext
|
|
|
|
}
|
2015-09-22 05:27:35 +00:00
|
|
|
fileExts = append(fileExts, ext)
|
|
|
|
}
|
|
|
|
if len(fileExts) > 0 {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.WebConfig.StaticExtensionsToGzip = fileExts
|
2013-12-14 18:34:27 +00:00
|
|
|
}
|
2014-10-01 14:10:33 +00:00
|
|
|
}
|
2016-01-26 16:10:21 +00:00
|
|
|
|
|
|
|
if lo := AppConfig.String("LogOutputs"); lo != "" {
|
|
|
|
los := strings.Split(lo, ";")
|
|
|
|
for _, v := range los {
|
|
|
|
if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 {
|
|
|
|
BConfig.Log.Outputs[logType2Config[0]] = logType2Config[1]
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//init log
|
|
|
|
BeeLogger.Close()
|
|
|
|
for adaptor, config := range BConfig.Log.Outputs {
|
|
|
|
err = BeeLogger.SetLogger(adaptor, config)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SetLogFuncCall(BConfig.Log.FileLineNum)
|
|
|
|
|
2014-10-01 14:10:33 +00:00
|
|
|
return nil
|
2014-06-11 04:00:50 +00:00
|
|
|
}
|
2015-12-09 15:35:04 +00:00
|
|
|
|
2016-01-26 16:10:21 +00:00
|
|
|
// LoadAppConfig allow developer to apply a config file
|
2016-01-27 08:43:01 +00:00
|
|
|
func LoadAppConfig(adapterName, configPath string) error {
|
2016-01-26 16:10:21 +00:00
|
|
|
absConfigPath, err := filepath.Abs(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-26 16:17:56 +00:00
|
|
|
|
|
|
|
if !utils.FileExists(absConfigPath) {
|
2016-01-27 05:30:34 +00:00
|
|
|
return fmt.Errorf("the target config file: %s don't exist!", configPath)
|
2016-01-26 16:17:56 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 08:43:01 +00:00
|
|
|
if absConfigPath == appConfigPath {
|
2016-01-26 16:10:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 08:43:01 +00:00
|
|
|
appConfigPath = absConfigPath
|
|
|
|
appConfigProvider = adapterName
|
2016-01-26 16:10:21 +00:00
|
|
|
|
2016-01-27 08:43:01 +00:00
|
|
|
return parseConfig(appConfigPath)
|
2016-01-26 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 15:35:04 +00:00
|
|
|
type beegoAppConfig struct {
|
|
|
|
innerConfig config.Configer
|
|
|
|
}
|
|
|
|
|
2016-01-27 08:43:01 +00:00
|
|
|
func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, error) {
|
|
|
|
ac, err := config.NewConfig(appConfigProvider, appConfigPath)
|
2015-12-09 15:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return &beegoAppConfig{ac}, nil
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) Set(key, val string) error {
|
2015-12-17 12:05:00 +00:00
|
|
|
if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil {
|
2015-12-09 15:35:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.innerConfig.Set(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) String(key string) string {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" {
|
|
|
|
return v
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return b.innerConfig.String(key)
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) Strings(key string) []string {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v := b.innerConfig.Strings(BConfig.RunMode + "::" + key); v[0] != "" {
|
2015-12-21 08:05:26 +00:00
|
|
|
return v
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return b.innerConfig.Strings(key)
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) Int(key string) (int, error) {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
|
|
|
|
return v, nil
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return b.innerConfig.Int(key)
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) Int64(key string) (int64, error) {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
|
|
|
|
return v, nil
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return b.innerConfig.Int64(key)
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) Bool(key string) (bool, error) {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
|
|
|
|
return v, nil
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return b.innerConfig.Bool(key)
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) Float(key string) (float64, error) {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
|
|
|
|
return v, nil
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
2015-12-17 12:05:00 +00:00
|
|
|
return b.innerConfig.Float(key)
|
2015-12-09 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DefaultString(key string, defaultval string) string {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v := b.String(key); v != "" {
|
2015-12-09 15:35:04 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DefaultStrings(key string, defaultval []string) []string {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v := b.Strings(key); len(v) != 0 {
|
2015-12-09 15:35:04 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DefaultInt(key string, defaultval int) int {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.Int(key); err == nil {
|
2015-12-09 15:35:04 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DefaultInt64(key string, defaultval int64) int64 {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.Int64(key); err == nil {
|
2015-12-09 15:35:04 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DefaultBool(key string, defaultval bool) bool {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.Bool(key); err == nil {
|
2015-12-09 15:35:04 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DefaultFloat(key string, defaultval float64) float64 {
|
2015-12-17 12:05:00 +00:00
|
|
|
if v, err := b.Float(key); err == nil {
|
2015-12-09 15:35:04 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
|
|
|
|
return b.innerConfig.DIY(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
|
|
|
|
return b.innerConfig.GetSection(section)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
|
|
|
|
return b.innerConfig.SaveConfigFile(filename)
|
|
|
|
}
|