Merge pull request #4276 from flycash/newHttpServer

Change NewHttpServer API
This commit is contained in:
Ming Deng 2020-10-21 21:59:45 +08:00 committed by GitHub
commit ae108ec826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 11 deletions

View File

@ -109,7 +109,7 @@ func registerAdmin() error {
servers: make([]*HttpServer, 0, 2), servers: make([]*HttpServer, 0, 2),
} }
beeAdminApp = &adminApp{ beeAdminApp = &adminApp{
HttpServer: NewHttpServerWithCfg(*BConfig), HttpServer: NewHttpServerWithCfg(BConfig),
} }
// keep in mind that all data should be html escaped to avoid XSS attack // keep in mind that all data should be html escaped to avoid XSS attack
beeAdminApp.Router("/", c, "get:AdminIndex") beeAdminApp.Router("/", c, "get:AdminIndex")

View File

@ -59,19 +59,18 @@ type HttpServer struct {
// NewHttpSever returns a new beego application. // NewHttpSever returns a new beego application.
// this method will use the BConfig as the configure to create HttpServer // this method will use the BConfig as the configure to create HttpServer
// Be careful that when you update BConfig, the server's Cfg will not be updated // Be careful that when you update BConfig, the server's Cfg will be updated too
func NewHttpSever() *HttpServer { func NewHttpSever() *HttpServer {
return NewHttpServerWithCfg(*BConfig) return NewHttpServerWithCfg(BConfig)
} }
// NewHttpServerWithCfg will create an sever with specific cfg // NewHttpServerWithCfg will create an sever with specific cfg
func NewHttpServerWithCfg(cfg Config) *HttpServer { func NewHttpServerWithCfg(cfg *Config) *HttpServer {
cfgPtr := &cfg cr := NewControllerRegisterWithCfg(cfg)
cr := NewControllerRegisterWithCfg(cfgPtr)
app := &HttpServer{ app := &HttpServer{
Handlers: cr, Handlers: cr,
Server: &http.Server{}, Server: &http.Server{},
Cfg: cfgPtr, Cfg: cfg,
} }
return app return app

View File

@ -21,11 +21,10 @@ import (
) )
func TestNewHttpServerWithCfg(t *testing.T) { func TestNewHttpServerWithCfg(t *testing.T) {
// we should make sure that update server's config won't change
BConfig.AppName = "Before" BConfig.AppName = "Before"
svr := NewHttpServerWithCfg(*BConfig) svr := NewHttpServerWithCfg(BConfig)
svr.Cfg.AppName = "hello" svr.Cfg.AppName = "hello"
assert.NotEqual(t, "hello", BConfig.AppName) assert.Equal(t, "hello", BConfig.AppName)
assert.Equal(t, "Before", BConfig.AppName)
} }