mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 09:51:28 +00:00
Merge branch 'develop' of https://github.com/astaxie/beego into develop
This commit is contained in:
commit
1bf52e8922
4
admin.go
4
admin.go
@ -90,8 +90,8 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
|||||||
switch command {
|
switch command {
|
||||||
case "conf":
|
case "conf":
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
m["AppConfigPath"] = AppConfigPath
|
m["AppConfigPath"] = appConfigPath
|
||||||
m["AppConfigProvider"] = AppConfigProvider
|
m["AppConfigProvider"] = appConfigProvider
|
||||||
m["BConfig.AppName"] = BConfig.AppName
|
m["BConfig.AppName"] = BConfig.AppName
|
||||||
m["BConfig.RunMode"] = BConfig.RunMode
|
m["BConfig.RunMode"] = BConfig.RunMode
|
||||||
m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
|
m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
|
||||||
|
18
beego.go
18
beego.go
@ -15,7 +15,6 @@
|
|||||||
package beego
|
package beego
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -68,21 +67,6 @@ func Run(params ...string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initBeforeHTTPRun() {
|
func initBeforeHTTPRun() {
|
||||||
// if AppConfigPath is setted or conf/app.conf exist
|
|
||||||
err := ParseConfig()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
//init log
|
|
||||||
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)
|
|
||||||
|
|
||||||
//init hooks
|
//init hooks
|
||||||
AddAPPStartHook(registerMime)
|
AddAPPStartHook(registerMime)
|
||||||
AddAPPStartHook(registerDefaultErrorHandler)
|
AddAPPStartHook(registerDefaultErrorHandler)
|
||||||
@ -101,7 +85,7 @@ func initBeforeHTTPRun() {
|
|||||||
// TestBeegoInit is for test package init
|
// TestBeegoInit is for test package init
|
||||||
func TestBeegoInit(ap string) {
|
func TestBeegoInit(ap string) {
|
||||||
os.Setenv("BEEGO_RUNMODE", "test")
|
os.Setenv("BEEGO_RUNMODE", "test")
|
||||||
AppConfigPath = filepath.Join(ap, "conf", "app.conf")
|
appConfigPath = filepath.Join(ap, "conf", "app.conf")
|
||||||
os.Chdir(ap)
|
os.Chdir(ap)
|
||||||
initBeforeHTTPRun()
|
initBeforeHTTPRun()
|
||||||
}
|
}
|
||||||
|
89
config.go
89
config.go
@ -19,6 +19,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/astaxie/beego/config"
|
"github.com/astaxie/beego/config"
|
||||||
"github.com/astaxie/beego/session"
|
"github.com/astaxie/beego/session"
|
||||||
@ -105,16 +106,16 @@ var (
|
|||||||
AppConfig *beegoAppConfig
|
AppConfig *beegoAppConfig
|
||||||
// AppPath is the absolute path to the app
|
// AppPath is the absolute path to the app
|
||||||
AppPath string
|
AppPath string
|
||||||
// AppConfigPath is the path to the config files
|
|
||||||
AppConfigPath string
|
|
||||||
// AppConfigProvider is the provider for the config, default is ini
|
|
||||||
AppConfigProvider = "ini"
|
|
||||||
// TemplateCache stores template caching
|
// TemplateCache stores template caching
|
||||||
TemplateCache map[string]*template.Template
|
TemplateCache map[string]*template.Template
|
||||||
// GlobalSessions is the instance for the session manager
|
// GlobalSessions is the instance for the session manager
|
||||||
GlobalSessions *session.Manager
|
GlobalSessions *session.Manager
|
||||||
|
|
||||||
workPath string
|
workPath string
|
||||||
|
// appConfigPath is the path to the config files
|
||||||
|
appConfigPath string
|
||||||
|
// appConfigProvider is the provider for the config, default is ini
|
||||||
|
appConfigProvider = "ini"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -122,6 +123,10 @@ func init() {
|
|||||||
workPath, _ = os.Getwd()
|
workPath, _ = os.Getwd()
|
||||||
workPath, _ = filepath.Abs(workPath)
|
workPath, _ = filepath.Abs(workPath)
|
||||||
|
|
||||||
|
if workPath != AppPath {
|
||||||
|
os.Chdir(AppPath)
|
||||||
|
}
|
||||||
|
|
||||||
BConfig = &Config{
|
BConfig = &Config{
|
||||||
AppName: "beego",
|
AppName: "beego",
|
||||||
RunMode: DEV,
|
RunMode: DEV,
|
||||||
@ -181,26 +186,19 @@ func init() {
|
|||||||
Outputs: map[string]string{"console": ""},
|
Outputs: map[string]string{"console": ""},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
ParseConfig()
|
|
||||||
|
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
||||||
|
if !utils.FileExists(appConfigPath) {
|
||||||
|
AppConfig = &beegoAppConfig{config.NewFakeConfig()}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
parseConfig(appConfigPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseConfig parsed default config file.
|
|
||||||
// now only support ini, next will support json.
|
// now only support ini, next will support json.
|
||||||
func ParseConfig() (err error) {
|
func parseConfig(appConfigPath string) (err error) {
|
||||||
if AppConfigPath == "" {
|
AppConfig, err = newAppConfig(appConfigProvider, appConfigPath)
|
||||||
// initialize default configurations
|
|
||||||
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
|
||||||
if !utils.FileExists(AppConfigPath) {
|
|
||||||
AppConfig = &beegoAppConfig{config.NewFakeConfig()}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if workPath != AppPath {
|
|
||||||
os.Chdir(AppPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -254,6 +252,8 @@ func ParseConfig() (err error) {
|
|||||||
BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime)
|
BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime)
|
||||||
BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie)
|
BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie)
|
||||||
BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain)
|
BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain)
|
||||||
|
BConfig.Log.AccessLogs = AppConfig.DefaultBool("LogAccessLogs", BConfig.Log.AccessLogs)
|
||||||
|
BConfig.Log.FileLineNum = AppConfig.DefaultBool("LogFileLineNum", BConfig.Log.FileLineNum)
|
||||||
|
|
||||||
if sd := AppConfig.String("StaticDir"); sd != "" {
|
if sd := AppConfig.String("StaticDir"); sd != "" {
|
||||||
for k := range BConfig.WebConfig.StaticDir {
|
for k := range BConfig.WebConfig.StaticDir {
|
||||||
@ -286,15 +286,58 @@ func ParseConfig() (err error) {
|
|||||||
BConfig.WebConfig.StaticExtensionsToGzip = fileExts
|
BConfig.WebConfig.StaticExtensionsToGzip = fileExts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadAppConfig allow developer to apply a config file
|
||||||
|
func LoadAppConfig(adapterName, configPath string) error {
|
||||||
|
absConfigPath, err := filepath.Abs(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !utils.FileExists(absConfigPath) {
|
||||||
|
return fmt.Errorf("the target config file: %s don't exist!", configPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if absConfigPath == appConfigPath {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
appConfigPath = absConfigPath
|
||||||
|
appConfigProvider = adapterName
|
||||||
|
|
||||||
|
return parseConfig(appConfigPath)
|
||||||
|
}
|
||||||
|
|
||||||
type beegoAppConfig struct {
|
type beegoAppConfig struct {
|
||||||
innerConfig config.Configer
|
innerConfig config.Configer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, error) {
|
func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, error) {
|
||||||
ac, err := config.NewConfig(AppConfigProvider, AppConfigPath)
|
ac, err := config.NewConfig(appConfigProvider, appConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -287,6 +287,13 @@ func (input *BeegoInput) Params() map[string]string {
|
|||||||
|
|
||||||
// SetParam will set the param with key and value
|
// SetParam will set the param with key and value
|
||||||
func (input *BeegoInput) SetParam(key, val string) {
|
func (input *BeegoInput) SetParam(key, val string) {
|
||||||
|
// check if already exists
|
||||||
|
for i, v := range input.pnames {
|
||||||
|
if v == key && i <= len(input.pvalues) {
|
||||||
|
input.pvalues[i] = val
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
input.pvalues = append(input.pvalues, val)
|
input.pvalues = append(input.pvalues, val)
|
||||||
input.pnames = append(input.pnames, key)
|
input.pnames = append(input.pnames, key)
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -117,3 +118,56 @@ func TestSubDomain(t *testing.T) {
|
|||||||
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
|
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParams(t *testing.T) {
|
||||||
|
inp := NewInput()
|
||||||
|
|
||||||
|
inp.SetParam("p1", "val1_ver1")
|
||||||
|
inp.SetParam("p2", "val2_ver1")
|
||||||
|
inp.SetParam("p3", "val3_ver1")
|
||||||
|
if l := inp.ParamsLen(); l != 3 {
|
||||||
|
t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
if val := inp.Param("p1"); val != "val1_ver1" {
|
||||||
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver1")
|
||||||
|
}
|
||||||
|
if val := inp.Param("p3"); val != "val3_ver1" {
|
||||||
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val3_ver1")
|
||||||
|
}
|
||||||
|
vals := inp.Params()
|
||||||
|
expected := map[string]string{
|
||||||
|
"p1": "val1_ver1",
|
||||||
|
"p2": "val2_ver1",
|
||||||
|
"p3": "val3_ver1",
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(vals, expected) {
|
||||||
|
t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
// overwriting existing params
|
||||||
|
inp.SetParam("p1", "val1_ver2")
|
||||||
|
inp.SetParam("p2", "val2_ver2")
|
||||||
|
expected = map[string]string{
|
||||||
|
"p1": "val1_ver2",
|
||||||
|
"p2": "val2_ver2",
|
||||||
|
"p3": "val3_ver1",
|
||||||
|
}
|
||||||
|
vals = inp.Params()
|
||||||
|
if !reflect.DeepEqual(vals, expected) {
|
||||||
|
t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l := inp.ParamsLen(); l != 3 {
|
||||||
|
t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
if val := inp.Param("p1"); val != "val1_ver2" {
|
||||||
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
|
||||||
|
}
|
||||||
|
|
||||||
|
if val := inp.Param("p2"); val != "val2_ver2" {
|
||||||
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -48,7 +48,8 @@ var colors = []brush{
|
|||||||
// consoleWriter implements LoggerInterface and writes messages to terminal.
|
// consoleWriter implements LoggerInterface and writes messages to terminal.
|
||||||
type consoleWriter struct {
|
type consoleWriter struct {
|
||||||
lg *log.Logger
|
lg *log.Logger
|
||||||
Level int `json:"level"`
|
Level int `json:"level"`
|
||||||
|
Color bool `json:"color"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConsole create ConsoleWriter returning as LoggerInterface.
|
// NewConsole create ConsoleWriter returning as LoggerInterface.
|
||||||
@ -56,6 +57,7 @@ func NewConsole() Logger {
|
|||||||
cw := &consoleWriter{
|
cw := &consoleWriter{
|
||||||
lg: log.New(os.Stdout, "", 0),
|
lg: log.New(os.Stdout, "", 0),
|
||||||
Level: LevelDebug,
|
Level: LevelDebug,
|
||||||
|
Color: true,
|
||||||
}
|
}
|
||||||
return cw
|
return cw
|
||||||
}
|
}
|
||||||
@ -75,7 +77,7 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
msg = formatLogTime(when) + msg
|
msg = formatLogTime(when) + msg
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" || !c.Color {
|
||||||
c.lg.Println(msg)
|
c.lg.Println(msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -42,3 +42,10 @@ func TestConsole(t *testing.T) {
|
|||||||
log2.SetLogger("console", `{"level":3}`)
|
log2.SetLogger("console", `{"level":3}`)
|
||||||
testConsoleCalls(log2)
|
testConsoleCalls(log2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test console without color
|
||||||
|
func TestConsoleNoColor(t *testing.T) {
|
||||||
|
log := NewLogger(100)
|
||||||
|
log.SetLogger("console", `{"color":false}`)
|
||||||
|
testConsoleCalls(log)
|
||||||
|
}
|
||||||
|
@ -365,6 +365,7 @@ func (bl *BeeLogger) Close() {
|
|||||||
l.Flush()
|
l.Flush()
|
||||||
l.Destroy()
|
l.Destroy()
|
||||||
}
|
}
|
||||||
|
bl.outputs = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatLogTime(when time.Time) string {
|
func formatLogTime(when time.Time) string {
|
||||||
|
@ -8,9 +8,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
const licenseFile = "./LICENSE"
|
var currentWorkDir, _ = os.Getwd()
|
||||||
|
var licenseFile = filepath.Join(currentWorkDir, "LICENSE")
|
||||||
|
|
||||||
func testOpenFile(encoding string, content []byte, t *testing.T) {
|
func testOpenFile(encoding string, content []byte, t *testing.T) {
|
||||||
fi, _ := os.Stat(licenseFile)
|
fi, _ := os.Stat(licenseFile)
|
||||||
|
Loading…
Reference in New Issue
Block a user