1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 11:20:54 +00:00

Multiple server refactor

This commit is contained in:
Ming Deng 2020-09-09 22:55:18 +08:00
parent bd1cfefec7
commit d455805a0a
9 changed files with 382 additions and 227 deletions

View File

@ -33,11 +33,11 @@ func init() {
} }
// App defines beego application with a new PatternServeMux. // App defines beego application with a new PatternServeMux.
type App web.App type App web.HttpServer
// NewApp returns a new beego application. // NewApp returns a new beego application.
func NewApp() *App { func NewApp() *App {
return (*App)(web.NewApp()) return (*App)(web.NewHttpSever())
} }
// MiddleWare function for http.Handler // MiddleWare function for http.Handler
@ -46,7 +46,7 @@ type MiddleWare web.MiddleWare
// Run beego application. // Run beego application.
func (app *App) Run(mws ...MiddleWare) { func (app *App) Run(mws ...MiddleWare) {
newMws := oldMiddlewareToNew(mws) newMws := oldMiddlewareToNew(mws)
(*web.App)(app).Run(newMws...) (*web.HttpServer)(app).Run("", newMws...)
} }
func oldMiddlewareToNew(mws []MiddleWare) []web.MiddleWare { func oldMiddlewareToNew(mws []MiddleWare) []web.MiddleWare {
@ -58,7 +58,7 @@ func oldMiddlewareToNew(mws []MiddleWare) []web.MiddleWare {
} }
// Router adds a patterned controller handler to BeeApp. // Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router. // it's an alias method of HttpServer.Router.
// usage: // usage:
// simple router // simple router
// beego.Router("/admin", &admin.UserController{}) // beego.Router("/admin", &admin.UserController{})
@ -138,7 +138,7 @@ func RESTRouter(rootpath string, c ControllerInterface) *App {
} }
// AutoRouter adds defined controller handler to BeeApp. // AutoRouter adds defined controller handler to BeeApp.
// it's same to App.AutoRouter. // it's same to HttpServer.AutoRouter.
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page, // if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
// visit the url /main/list to exec List function or /main/page to exec Page function. // visit the url /main/list to exec List function or /main/page to exec Page function.
func AutoRouter(c ControllerInterface) *App { func AutoRouter(c ControllerInterface) *App {
@ -146,7 +146,7 @@ func AutoRouter(c ControllerInterface) *App {
} }
// AutoPrefix adds controller handler to BeeApp with prefix. // AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix. // it's same to HttpServer.AutoRouterWithPrefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page, // if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function. // visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func AutoPrefix(prefix string, c ControllerInterface) *App { func AutoPrefix(prefix string, c ControllerInterface) *App {

View File

@ -17,8 +17,7 @@ package web
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "sync"
"strings"
) )
const ( const (
@ -35,7 +34,7 @@ type M map[string]interface{}
type hookfunc func() error type hookfunc func() error
var ( var (
hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc hooks = make([]hookfunc, 0) // hook function slice to store the hookfunc
) )
// AddAPPStartHook is used to register the hookfunc // AddAPPStartHook is used to register the hookfunc
@ -52,56 +51,39 @@ func AddAPPStartHook(hf ...hookfunc) {
// beego.Run("127.0.0.1:8089") // beego.Run("127.0.0.1:8089")
func Run(params ...string) { func Run(params ...string) {
initBeforeHTTPRun()
if len(params) > 0 && params[0] != "" { if len(params) > 0 && params[0] != "" {
strs := strings.Split(params[0], ":") BeeApp.Run(params[0])
if len(strs) > 0 && strs[0] != "" {
BConfig.Listen.HTTPAddr = strs[0]
}
if len(strs) > 1 && strs[1] != "" {
BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
}
BConfig.Listen.Domains = params
} }
BeeApp.Run("")
BeeApp.Run()
} }
// RunWithMiddleWares Run beego application with middlewares. // RunWithMiddleWares Run beego application with middlewares.
func RunWithMiddleWares(addr string, mws ...MiddleWare) { func RunWithMiddleWares(addr string, mws ...MiddleWare) {
initBeforeHTTPRun() BeeApp.Run(addr, mws...)
strs := strings.Split(addr, ":")
if len(strs) > 0 && strs[0] != "" {
BConfig.Listen.HTTPAddr = strs[0]
BConfig.Listen.Domains = []string{strs[0]}
}
if len(strs) > 1 && strs[1] != "" {
BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
}
BeeApp.Run(mws...)
} }
func initBeforeHTTPRun() { var initHttpOnce sync.Once
//init hooks
AddAPPStartHook(
registerMime,
registerDefaultErrorHandler,
registerSession,
registerTemplate,
registerAdmin,
registerGzip,
registerCommentRouter,
)
for _, hk := range hooks { // TODO move to module init function
if err := hk(); err != nil { func initBeforeHTTPRun() {
panic(err) initHttpOnce.Do(func() {
// init hooks
AddAPPStartHook(
registerMime,
registerDefaultErrorHandler,
registerSession,
registerTemplate,
registerAdmin,
registerGzip,
registerCommentRouter,
)
for _, hk := range hooks {
if err := hk(); err != nil {
panic(err)
}
} }
} })
} }
// TestBeegoInit is for test package init // TestBeegoInit is for test package init

View File

@ -34,6 +34,7 @@ import (
) )
// Config is the main struct for BConfig // Config is the main struct for BConfig
// TODO after supporting multiple servers, remove common config to somewhere else
type Config struct { type Config struct {
AppName string // Application name AppName string // Application name
RunMode string // Running Mode: dev | prod RunMode string // Running Mode: dev | prod
@ -168,15 +169,15 @@ func init() {
} }
} }
func recoverPanic(ctx *context.Context) { func (cfg *Config) defaultRecoverPanic(ctx *context.Context) {
if err := recover(); err != nil { if err := recover(); err != nil {
if err == ErrAbort { if err == ErrAbort {
return return
} }
if !BConfig.RecoverPanic { if !cfg.RecoverPanic {
panic(err) panic(err)
} }
if BConfig.EnableErrorsShow { if cfg.EnableErrorsShow {
if _, ok := ErrorMaps[fmt.Sprint(err)]; ok { if _, ok := ErrorMaps[fmt.Sprint(err)]; ok {
exception(fmt.Sprint(err), ctx) exception(fmt.Sprint(err), ctx)
return return
@ -193,7 +194,7 @@ func recoverPanic(ctx *context.Context) {
logs.Critical(fmt.Sprintf("%s:%d", file, line)) logs.Critical(fmt.Sprintf("%s:%d", file, line))
stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line)) stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
} }
if BConfig.RunMode == DEV && BConfig.EnableErrorsRender { if cfg.RunMode == DEV && cfg.EnableErrorsRender {
showErr(err, ctx, stack) showErr(err, ctx, stack)
} }
if ctx.Output.Status != 0 { if ctx.Output.Status != 0 {
@ -205,18 +206,18 @@ func recoverPanic(ctx *context.Context) {
} }
func newBConfig() *Config { func newBConfig() *Config {
return &Config{ res := &Config{
AppName: "beego", AppName: "beego",
RunMode: PROD, RunMode: PROD,
RouterCaseSensitive: true, RouterCaseSensitive: true,
ServerName: "beegoServer:" + pkg.VERSION, ServerName: "beegoServer:" + pkg.VERSION,
RecoverPanic: true, RecoverPanic: true,
RecoverFunc: recoverPanic,
CopyRequestBody: false, CopyRequestBody: false,
EnableGzip: false, EnableGzip: false,
MaxMemory: 1 << 26, // 64MB MaxMemory: 1 << 26, // 64MB
EnableErrorsShow: true, EnableErrorsShow: true,
EnableErrorsRender: true, EnableErrorsRender: true,
Listen: Listen{ Listen: Listen{
Graceful: false, Graceful: false,
ServerTimeOut: 0, ServerTimeOut: 0,
@ -279,6 +280,9 @@ func newBConfig() *Config {
Outputs: map[string]string{"console": ""}, Outputs: map[string]string{"console": ""},
}, },
} }
res.RecoverFunc = res.defaultRecoverPanic
return res
} }
// now only support ini, next will support json. // now only support ini, next will support json.

View File

@ -389,7 +389,7 @@ func responseError(rw http.ResponseWriter, r *http.Request, errCode int, errCont
// usage: // usage:
// beego.ErrorHandler("404",NotFound) // beego.ErrorHandler("404",NotFound)
// beego.ErrorHandler("500",InternalServerError) // beego.ErrorHandler("500",InternalServerError)
func ErrorHandler(code string, h http.HandlerFunc) *App { func ErrorHandler(code string, h http.HandlerFunc) *HttpServer {
ErrorMaps[code] = &errorInfo{ ErrorMaps[code] = &errorInfo{
errorType: errorTypeHandler, errorType: errorTypeHandler,
handler: h, handler: h,
@ -401,7 +401,7 @@ func ErrorHandler(code string, h http.HandlerFunc) *App {
// ErrorController registers ControllerInterface to each http err code string. // ErrorController registers ControllerInterface to each http err code string.
// usage: // usage:
// beego.ErrorController(&controllers.ErrorController{}) // beego.ErrorController(&controllers.ErrorController{})
func ErrorController(c ControllerInterface) *App { func ErrorController(c ControllerInterface) *HttpServer {
reflectVal := reflect.ValueOf(c) reflectVal := reflect.ValueOf(c)
rt := reflectVal.Type() rt := reflectVal.Type()
ct := reflect.Indirect(reflectVal).Type() ct := reflect.Indirect(reflectVal).Type()

View File

@ -135,10 +135,18 @@ type ControllerRegister struct {
// the filter created by FilterChain // the filter created by FilterChain
chainRoot *FilterRouter chainRoot *FilterRouter
cfg *Config
} }
// NewControllerRegister returns a new ControllerRegister. // NewControllerRegister returns a new ControllerRegister.
// Usually you should not use this method
// please use NewControllerRegisterWithCfg
func NewControllerRegister() *ControllerRegister { func NewControllerRegister() *ControllerRegister {
return NewControllerRegisterWithCfg(BeeApp.Cfg)
}
func NewControllerRegisterWithCfg(cfg *Config) *ControllerRegister {
res := &ControllerRegister{ res := &ControllerRegister{
routers: make(map[string]*Tree), routers: make(map[string]*Tree),
policies: make(map[string]*Tree), policies: make(map[string]*Tree),
@ -240,7 +248,7 @@ func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInt
} }
func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerInfo) { func (p *ControllerRegister) addToRouter(method, pattern string, r *ControllerInfo) {
if !BConfig.RouterCaseSensitive { if !p.cfg.RouterCaseSensitive {
pattern = strings.ToLower(pattern) pattern = strings.ToLower(pattern)
} }
if t, ok := p.routers[method]; ok { if t, ok := p.routers[method]; ok {
@ -453,7 +461,7 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
// 1. setting the returnOnOutput value (false allows multiple filters to execute) // 1. setting the returnOnOutput value (false allows multiple filters to execute)
// 2. determining whether or not params need to be reset. // 2. determining whether or not params need to be reset.
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) error { func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) error {
opts = append(opts, WithCaseSensitive(BConfig.RouterCaseSensitive)) opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive))
mr := newFilterRouter(pattern, filter, opts...) mr := newFilterRouter(pattern, filter, opts...)
return p.insertFilterRouter(pos, mr) return p.insertFilterRouter(pos, mr)
} }
@ -472,7 +480,7 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter
func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) { func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) {
root := p.chainRoot root := p.chainRoot
filterFunc := chain(root.filterFunc) filterFunc := chain(root.filterFunc)
opts = append(opts, WithCaseSensitive(BConfig.RouterCaseSensitive)) opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive))
p.chainRoot = newFilterRouter(pattern, filterFunc, opts...) p.chainRoot = newFilterRouter(pattern, filterFunc, opts...)
p.chainRoot.next = root p.chainRoot.next = root
@ -669,14 +677,14 @@ func (p *ControllerRegister) serveHttp(ctx *beecontext.Context) {
isRunnable bool isRunnable bool
) )
if BConfig.RecoverFunc != nil { if p.cfg.RecoverFunc != nil {
defer BConfig.RecoverFunc(ctx) defer p.cfg.RecoverFunc(ctx)
} }
ctx.Output.EnableGzip = BConfig.EnableGzip ctx.Output.EnableGzip = p.cfg.EnableGzip
if BConfig.RunMode == DEV { if p.cfg.RunMode == DEV {
ctx.Output.Header("Server", BConfig.ServerName) ctx.Output.Header("Server", p.cfg.ServerName)
} }
urlPath := p.getUrlPath(ctx) urlPath := p.getUrlPath(ctx)
@ -700,20 +708,20 @@ func (p *ControllerRegister) serveHttp(ctx *beecontext.Context) {
} }
if r.Method != http.MethodGet && r.Method != http.MethodHead { if r.Method != http.MethodGet && r.Method != http.MethodHead {
if BConfig.CopyRequestBody && !ctx.Input.IsUpload() { if p.cfg.CopyRequestBody && !ctx.Input.IsUpload() {
// connection will close if the incoming data are larger (RFC 7231, 6.5.11) // connection will close if the incoming data are larger (RFC 7231, 6.5.11)
if r.ContentLength > BConfig.MaxMemory { if r.ContentLength > p.cfg.MaxMemory {
logs.Error(errors.New("payload too large")) logs.Error(errors.New("payload too large"))
exception("413", ctx) exception("413", ctx)
goto Admin goto Admin
} }
ctx.Input.CopyBody(BConfig.MaxMemory) ctx.Input.CopyBody(p.cfg.MaxMemory)
} }
ctx.Input.ParseFormOrMulitForm(BConfig.MaxMemory) ctx.Input.ParseFormOrMulitForm(p.cfg.MaxMemory)
} }
// session init // session init
if BConfig.WebConfig.Session.SessionOn { if p.cfg.WebConfig.Session.SessionOn {
var err error var err error
ctx.Input.CruSession, err = GlobalSessions.SessionStart(rw, r) ctx.Input.CruSession, err = GlobalSessions.SessionStart(rw, r)
if err != nil { if err != nil {
@ -819,7 +827,7 @@ func (p *ControllerRegister) serveHttp(ctx *beecontext.Context) {
execController.Prepare() execController.Prepare()
// if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf // if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
if BConfig.WebConfig.EnableXSRF { if p.cfg.WebConfig.EnableXSRF {
execController.XSRFToken() execController.XSRFToken()
if r.Method == http.MethodPost || r.Method == http.MethodDelete || r.Method == http.MethodPut || if r.Method == http.MethodPost || r.Method == http.MethodDelete || r.Method == http.MethodPut ||
(r.Method == http.MethodPost && (ctx.Input.Query("_method") == http.MethodDelete || ctx.Input.Query("_method") == http.MethodPut)) { (r.Method == http.MethodPost && (ctx.Input.Query("_method") == http.MethodDelete || ctx.Input.Query("_method") == http.MethodPut)) {
@ -864,7 +872,7 @@ func (p *ControllerRegister) serveHttp(ctx *beecontext.Context) {
// render template // render template
if !ctx.ResponseWriter.Started && ctx.Output.Status == 0 { if !ctx.ResponseWriter.Started && ctx.Output.Status == 0 {
if BConfig.WebConfig.AutoRender { if p.cfg.WebConfig.AutoRender {
if err := execController.Render(); err != nil { if err := execController.Render(); err != nil {
logs.Error(err) logs.Error(err)
} }
@ -897,7 +905,7 @@ Admin:
timeDur := time.Since(startTime) timeDur := time.Since(startTime)
ctx.ResponseWriter.Elapsed = timeDur ctx.ResponseWriter.Elapsed = timeDur
if BConfig.Listen.EnableAdmin { if p.cfg.Listen.EnableAdmin {
pattern := "" pattern := ""
if routerInfo != nil { if routerInfo != nil {
pattern = routerInfo.pattern pattern = routerInfo.pattern
@ -912,7 +920,7 @@ Admin:
} }
} }
if BConfig.RunMode == DEV && !BConfig.Log.AccessLogs { if p.cfg.RunMode == DEV && !p.cfg.Log.AccessLogs {
match := map[bool]string{true: "match", false: "nomatch"} match := map[bool]string{true: "match", false: "nomatch"}
devInfo := fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s", devInfo := fmt.Sprintf("|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s",
ctx.Input.IP(), ctx.Input.IP(),
@ -935,7 +943,7 @@ Admin:
func (p *ControllerRegister) getUrlPath(ctx *beecontext.Context) string { func (p *ControllerRegister) getUrlPath(ctx *beecontext.Context) string {
urlPath := ctx.Request.URL.Path urlPath := ctx.Request.URL.Path
if !BConfig.RouterCaseSensitive { if !p.cfg.RouterCaseSensitive {
urlPath = strings.ToLower(urlPath) urlPath = strings.ToLower(urlPath)
} }
return urlPath return urlPath
@ -958,7 +966,7 @@ func (p *ControllerRegister) handleParamResponse(context *beecontext.Context, ex
// FindRouter Find Router info for URL // FindRouter Find Router info for URL
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *ControllerInfo, isFind bool) { func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *ControllerInfo, isFind bool) {
var urlPath = context.Input.URL() var urlPath = context.Input.URL()
if !BConfig.RouterCaseSensitive { if !p.cfg.RouterCaseSensitive {
urlPath = strings.ToLower(urlPath) urlPath = strings.ToLower(urlPath)
} }
httpMethod := context.Input.Method() httpMethod := context.Input.Method()
@ -984,36 +992,5 @@ func toURL(params map[string]string) string {
// LogAccess logging info HTTP Access // LogAccess logging info HTTP Access
func LogAccess(ctx *beecontext.Context, startTime *time.Time, statusCode int) { func LogAccess(ctx *beecontext.Context, startTime *time.Time, statusCode int) {
// Skip logging if AccessLogs config is false BeeApp.LogAccess(ctx, startTime, statusCode)
if !BConfig.Log.AccessLogs {
return
}
// Skip logging static requests unless EnableStaticLogs config is true
if !BConfig.Log.EnableStaticLogs && DefaultAccessLogFilter.Filter(ctx) {
return
}
var (
requestTime time.Time
elapsedTime time.Duration
r = ctx.Request
)
if startTime != nil {
requestTime = *startTime
elapsedTime = time.Since(*startTime)
}
record := &logs.AccessLogRecord{
RemoteAddr: ctx.Input.IP(),
RequestTime: requestTime,
RequestMethod: r.Method,
Request: fmt.Sprintf("%s %s %s", r.Method, r.RequestURI, r.Proto),
ServerProtocol: r.Proto,
Host: r.Host,
Status: statusCode,
ElapsedTime: elapsedTime,
HTTPReferrer: r.Header.Get("Referer"),
HTTPUserAgent: r.Header.Get("User-Agent"),
RemoteUser: r.Header.Get("Remote-User"),
BodyBytesSent: r.ContentLength,
}
logs.AccessLog(record, BConfig.Log.AccessLogsFormat)
} }

View File

@ -381,7 +381,7 @@ func TestRouterHandlerAll(t *testing.T) {
} }
// //
// Benchmarks NewApp: // Benchmarks NewHttpSever:
// //
func beegoFilterFunc(ctx *context.Context) { func beegoFilterFunc(ctx *context.Context) {

View File

@ -24,12 +24,14 @@ import (
"net/http/fcgi" "net/http/fcgi"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
"golang.org/x/crypto/acme/autocert" "golang.org/x/crypto/acme/autocert"
"github.com/astaxie/beego/pkg/infrastructure/logs" "github.com/astaxie/beego/pkg/infrastructure/logs"
beecontext "github.com/astaxie/beego/pkg/server/web/context"
"github.com/astaxie/beego/pkg/infrastructure/utils" "github.com/astaxie/beego/pkg/infrastructure/utils"
"github.com/astaxie/beego/pkg/server/web/grace" "github.com/astaxie/beego/pkg/server/web/grace"
@ -37,24 +39,39 @@ import (
var ( var (
// BeeApp is an application instance // BeeApp is an application instance
BeeApp *App // If you are using single server, you could use this
// But if you need multiple servers, do not use this
BeeApp *HttpServer
) )
func init() { func init() {
// create beego application // create beego application
BeeApp = NewApp() BeeApp = NewHttpSever()
} }
// App defines beego application with a new PatternServeMux. // HttpServer defines beego application with a new PatternServeMux.
type App struct { type HttpServer struct {
Handlers *ControllerRegister Handlers *ControllerRegister
Server *http.Server Server *http.Server
Cfg *Config
} }
// NewApp returns a new beego application. // NewHttpSever returns a new beego application.
func NewApp() *App { // this method will use the BConfig as the configure to create HttpServer
cr := NewControllerRegister() // Be careful that when you update BConfig, the server's Cfg will not be updated
app := &App{Handlers: cr, Server: &http.Server{}} func NewHttpSever() *HttpServer {
return NewHttpServerWithCfg(*BConfig)
}
// NewHttpServerWithCfg will create an sever with specific cfg
func NewHttpServerWithCfg(cfg Config) *HttpServer {
cfgPtr := &cfg
cr := NewControllerRegisterWithCfg(cfgPtr)
app := &HttpServer{
Handlers: cr,
Server: &http.Server{},
Cfg: cfgPtr,
}
return app return app
} }
@ -62,11 +79,16 @@ func NewApp() *App {
type MiddleWare func(http.Handler) http.Handler type MiddleWare func(http.Handler) http.Handler
// Run beego application. // Run beego application.
func (app *App) Run(mws ...MiddleWare) { func (app *HttpServer) Run(addr string, mws ...MiddleWare) {
addr := BConfig.Listen.HTTPAddr
if BConfig.Listen.HTTPPort != 0 { initBeforeHTTPRun()
addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPAddr, BConfig.Listen.HTTPPort)
app.initAddr(addr)
addr = app.Cfg.Listen.HTTPAddr
if app.Cfg.Listen.HTTPPort != 0 {
addr = fmt.Sprintf("%s:%d", app.Cfg.Listen.HTTPAddr, app.Cfg.Listen.HTTPPort)
} }
var ( var (
@ -76,8 +98,8 @@ func (app *App) Run(mws ...MiddleWare) {
) )
// run cgi server // run cgi server
if BConfig.Listen.EnableFcgi { if app.Cfg.Listen.EnableFcgi {
if BConfig.Listen.EnableStdIo { if app.Cfg.Listen.EnableStdIo {
if err = fcgi.Serve(nil, app.Handlers); err == nil { // standard I/O if err = fcgi.Serve(nil, app.Handlers); err == nil { // standard I/O
logs.Info("Use FCGI via standard I/O") logs.Info("Use FCGI via standard I/O")
} else { } else {
@ -85,7 +107,7 @@ func (app *App) Run(mws ...MiddleWare) {
} }
return return
} }
if BConfig.Listen.HTTPPort == 0 { if app.Cfg.Listen.HTTPPort == 0 {
// remove the Socket file before start // remove the Socket file before start
if utils.FileExists(addr) { if utils.FileExists(addr) {
os.Remove(addr) os.Remove(addr)
@ -110,40 +132,42 @@ func (app *App) Run(mws ...MiddleWare) {
} }
app.Server.Handler = mws[i](app.Server.Handler) app.Server.Handler = mws[i](app.Server.Handler)
} }
app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second app.Server.ReadTimeout = time.Duration(app.Cfg.Listen.ServerTimeOut) * time.Second
app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second app.Server.WriteTimeout = time.Duration(app.Cfg.Listen.ServerTimeOut) * time.Second
app.Server.ErrorLog = logs.GetLogger("HTTP") app.Server.ErrorLog = logs.GetLogger("HTTP")
// run graceful mode // run graceful mode
if BConfig.Listen.Graceful { if app.Cfg.Listen.Graceful {
httpsAddr := BConfig.Listen.HTTPSAddr httpsAddr := app.Cfg.Listen.HTTPSAddr
app.Server.Addr = httpsAddr app.Server.Addr = httpsAddr
if BConfig.Listen.EnableHTTPS || BConfig.Listen.EnableMutualHTTPS { if app.Cfg.Listen.EnableHTTPS || app.Cfg.Listen.EnableMutualHTTPS {
go func() { go func() {
time.Sleep(1000 * time.Microsecond) time.Sleep(1000 * time.Microsecond)
if BConfig.Listen.HTTPSPort != 0 { if app.Cfg.Listen.HTTPSPort != 0 {
httpsAddr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort) httpsAddr = fmt.Sprintf("%s:%d", app.Cfg.Listen.HTTPSAddr, app.Cfg.Listen.HTTPSPort)
app.Server.Addr = httpsAddr app.Server.Addr = httpsAddr
} }
server := grace.NewServer(httpsAddr, app.Server.Handler) server := grace.NewServer(httpsAddr, app.Server.Handler)
server.Server.ReadTimeout = app.Server.ReadTimeout server.Server.ReadTimeout = app.Server.ReadTimeout
server.Server.WriteTimeout = app.Server.WriteTimeout server.Server.WriteTimeout = app.Server.WriteTimeout
if BConfig.Listen.EnableMutualHTTPS { if app.Cfg.Listen.EnableMutualHTTPS {
if err := server.ListenAndServeMutualTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile, BConfig.Listen.TrustCaFile); err != nil { if err := server.ListenAndServeMutualTLS(app.Cfg.Listen.HTTPSCertFile,
app.Cfg.Listen.HTTPSKeyFile,
app.Cfg.Listen.TrustCaFile); err != nil {
logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid())) logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
time.Sleep(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
} }
} else { } else {
if BConfig.Listen.AutoTLS { if app.Cfg.Listen.AutoTLS {
m := autocert.Manager{ m := autocert.Manager{
Prompt: autocert.AcceptTOS, Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(BConfig.Listen.Domains...), HostPolicy: autocert.HostWhitelist(app.Cfg.Listen.Domains...),
Cache: autocert.DirCache(BConfig.Listen.TLSCacheDir), Cache: autocert.DirCache(app.Cfg.Listen.TLSCacheDir),
} }
app.Server.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate} app.Server.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile = "", "" app.Cfg.Listen.HTTPSCertFile, app.Cfg.Listen.HTTPSKeyFile = "", ""
} }
if err := server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil { if err := server.ListenAndServeTLS(app.Cfg.Listen.HTTPSCertFile, app.Cfg.Listen.HTTPSKeyFile); err != nil {
logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid())) logs.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
time.Sleep(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
} }
@ -151,12 +175,12 @@ func (app *App) Run(mws ...MiddleWare) {
endRunning <- true endRunning <- true
}() }()
} }
if BConfig.Listen.EnableHTTP { if app.Cfg.Listen.EnableHTTP {
go func() { go func() {
server := grace.NewServer(addr, app.Server.Handler) server := grace.NewServer(addr, app.Server.Handler)
server.Server.ReadTimeout = app.Server.ReadTimeout server.Server.ReadTimeout = app.Server.ReadTimeout
server.Server.WriteTimeout = app.Server.WriteTimeout server.Server.WriteTimeout = app.Server.WriteTimeout
if BConfig.Listen.ListenTCP4 { if app.Cfg.Listen.ListenTCP4 {
server.Network = "tcp4" server.Network = "tcp4"
} }
if err := server.ListenAndServe(); err != nil { if err := server.ListenAndServe(); err != nil {
@ -171,27 +195,27 @@ func (app *App) Run(mws ...MiddleWare) {
} }
// run normal mode // run normal mode
if BConfig.Listen.EnableHTTPS || BConfig.Listen.EnableMutualHTTPS { if app.Cfg.Listen.EnableHTTPS || app.Cfg.Listen.EnableMutualHTTPS {
go func() { go func() {
time.Sleep(1000 * time.Microsecond) time.Sleep(1000 * time.Microsecond)
if BConfig.Listen.HTTPSPort != 0 { if app.Cfg.Listen.HTTPSPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort) app.Server.Addr = fmt.Sprintf("%s:%d", app.Cfg.Listen.HTTPSAddr, app.Cfg.Listen.HTTPSPort)
} else if BConfig.Listen.EnableHTTP { } else if app.Cfg.Listen.EnableHTTP {
logs.Info("Start https server error, conflict with http. Please reset https port") logs.Info("Start https server error, conflict with http. Please reset https port")
return return
} }
logs.Info("https server Running on https://%s", app.Server.Addr) logs.Info("https server Running on https://%s", app.Server.Addr)
if BConfig.Listen.AutoTLS { if app.Cfg.Listen.AutoTLS {
m := autocert.Manager{ m := autocert.Manager{
Prompt: autocert.AcceptTOS, Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(BConfig.Listen.Domains...), HostPolicy: autocert.HostWhitelist(app.Cfg.Listen.Domains...),
Cache: autocert.DirCache(BConfig.Listen.TLSCacheDir), Cache: autocert.DirCache(app.Cfg.Listen.TLSCacheDir),
} }
app.Server.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate} app.Server.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile = "", "" app.Cfg.Listen.HTTPSCertFile, app.Cfg.Listen.HTTPSKeyFile = "", ""
} else if BConfig.Listen.EnableMutualHTTPS { } else if app.Cfg.Listen.EnableMutualHTTPS {
pool := x509.NewCertPool() pool := x509.NewCertPool()
data, err := ioutil.ReadFile(BConfig.Listen.TrustCaFile) data, err := ioutil.ReadFile(app.Cfg.Listen.TrustCaFile)
if err != nil { if err != nil {
logs.Info("MutualHTTPS should provide TrustCaFile") logs.Info("MutualHTTPS should provide TrustCaFile")
return return
@ -199,10 +223,10 @@ func (app *App) Run(mws ...MiddleWare) {
pool.AppendCertsFromPEM(data) pool.AppendCertsFromPEM(data)
app.Server.TLSConfig = &tls.Config{ app.Server.TLSConfig = &tls.Config{
ClientCAs: pool, ClientCAs: pool,
ClientAuth: tls.ClientAuthType(BConfig.Listen.ClientAuth), ClientAuth: tls.ClientAuthType(app.Cfg.Listen.ClientAuth),
} }
} }
if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil { if err := app.Server.ListenAndServeTLS(app.Cfg.Listen.HTTPSCertFile, app.Cfg.Listen.HTTPSKeyFile); err != nil {
logs.Critical("ListenAndServeTLS: ", err) logs.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
endRunning <- true endRunning <- true
@ -210,11 +234,11 @@ func (app *App) Run(mws ...MiddleWare) {
}() }()
} }
if BConfig.Listen.EnableHTTP { if app.Cfg.Listen.EnableHTTP {
go func() { go func() {
app.Server.Addr = addr app.Server.Addr = addr
logs.Info("http server Running on http://%s", app.Server.Addr) logs.Info("http server Running on http://%s", app.Server.Addr)
if BConfig.Listen.ListenTCP4 { if app.Cfg.Listen.ListenTCP4 {
ln, err := net.Listen("tcp4", app.Server.Addr) ln, err := net.Listen("tcp4", app.Server.Addr)
if err != nil { if err != nil {
logs.Critical("ListenAndServe: ", err) logs.Critical("ListenAndServe: ", err)
@ -240,8 +264,17 @@ func (app *App) Run(mws ...MiddleWare) {
<-endRunning <-endRunning
} }
func (app *HttpServer) Start() {
}
// Router see HttpServer.Router
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *HttpServer {
return BeeApp.Router(rootpath, c, mappingMethods...)
}
// Router adds a patterned controller handler to BeeApp. // Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router. // it's an alias method of HttpServer.Router.
// usage: // usage:
// simple router // simple router
// beego.Router("/admin", &admin.UserController{}) // beego.Router("/admin", &admin.UserController{})
@ -256,9 +289,14 @@ func (app *App) Run(mws ...MiddleWare) {
// beego.Router("/api/create",&RestController{},"post:CreateFood") // beego.Router("/api/create",&RestController{},"post:CreateFood")
// beego.Router("/api/update",&RestController{},"put:UpdateFood") // beego.Router("/api/update",&RestController{},"put:UpdateFood")
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood") // beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App { func (app *HttpServer) Router(rootPath string, c ControllerInterface, mappingMethods ...string) *HttpServer {
BeeApp.Handlers.Add(rootpath, c, mappingMethods...) app.Handlers.Add(rootPath, c, mappingMethods...)
return BeeApp return app
}
// UnregisterFixedRoute see HttpServer.UnregisterFixedRoute
func UnregisterFixedRoute(fixedRoute string, method string) *HttpServer {
return BeeApp.UnregisterFixedRoute(fixedRoute, method)
} }
// UnregisterFixedRoute unregisters the route with the specified fixedRoute. It is particularly useful // UnregisterFixedRoute unregisters the route with the specified fixedRoute. It is particularly useful
@ -270,31 +308,31 @@ func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *A
// Usage (replace "GET" with "*" for all methods): // Usage (replace "GET" with "*" for all methods):
// beego.UnregisterFixedRoute("/yourpreviouspath", "GET") // beego.UnregisterFixedRoute("/yourpreviouspath", "GET")
// beego.Router("/yourpreviouspath", yourControllerAddress, "get:GetNewPage") // beego.Router("/yourpreviouspath", yourControllerAddress, "get:GetNewPage")
func UnregisterFixedRoute(fixedRoute string, method string) *App { func (app *HttpServer) UnregisterFixedRoute(fixedRoute string, method string) *HttpServer {
subPaths := splitPath(fixedRoute) subPaths := splitPath(fixedRoute)
if method == "" || method == "*" { if method == "" || method == "*" {
for m := range HTTPMETHOD { for m := range HTTPMETHOD {
if _, ok := BeeApp.Handlers.routers[m]; !ok { if _, ok := app.Handlers.routers[m]; !ok {
continue continue
} }
if BeeApp.Handlers.routers[m].prefix == strings.Trim(fixedRoute, "/ ") { if app.Handlers.routers[m].prefix == strings.Trim(fixedRoute, "/ ") {
findAndRemoveSingleTree(BeeApp.Handlers.routers[m]) findAndRemoveSingleTree(app.Handlers.routers[m])
continue continue
} }
findAndRemoveTree(subPaths, BeeApp.Handlers.routers[m], m) findAndRemoveTree(subPaths, app.Handlers.routers[m], m)
} }
return BeeApp return app
} }
// Single HTTP method // Single HTTP method
um := strings.ToUpper(method) um := strings.ToUpper(method)
if _, ok := BeeApp.Handlers.routers[um]; ok { if _, ok := app.Handlers.routers[um]; ok {
if BeeApp.Handlers.routers[um].prefix == strings.Trim(fixedRoute, "/ ") { if app.Handlers.routers[um].prefix == strings.Trim(fixedRoute, "/ ") {
findAndRemoveSingleTree(BeeApp.Handlers.routers[um]) findAndRemoveSingleTree(app.Handlers.routers[um])
return BeeApp return app
} }
findAndRemoveTree(subPaths, BeeApp.Handlers.routers[um], um) findAndRemoveTree(subPaths, app.Handlers.routers[um], um)
} }
return BeeApp return app
} }
func findAndRemoveTree(paths []string, entryPointTree *Tree, method string) { func findAndRemoveTree(paths []string, entryPointTree *Tree, method string) {
@ -339,6 +377,11 @@ func findAndRemoveSingleTree(entryPointTree *Tree) {
} }
} }
// Include see HttpServer.Include
func Include(cList ...ControllerInterface) *HttpServer {
return BeeApp.Include(cList...)
}
// Include will generate router file in the router/xxx.go from the controller's comments // Include will generate router file in the router/xxx.go from the controller's comments
// usage: // usage:
// beego.Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{}) // beego.Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
@ -366,36 +409,56 @@ func findAndRemoveSingleTree(entryPointTree *Tree) {
// the comments @router url methodlist // the comments @router url methodlist
// url support all the function Router's pattern // url support all the function Router's pattern
// methodlist [get post head put delete options *] // methodlist [get post head put delete options *]
func Include(cList ...ControllerInterface) *App { func (app *HttpServer) Include(cList ...ControllerInterface) *HttpServer {
BeeApp.Handlers.Include(cList...) app.Handlers.Include(cList...)
return BeeApp return app
}
// RESTRouter see HttpServer.RESTRouter
func RESTRouter(rootpath string, c ControllerInterface) *HttpServer {
return BeeApp.RESTRouter(rootpath, c)
} }
// RESTRouter adds a restful controller handler to BeeApp. // RESTRouter adds a restful controller handler to BeeApp.
// its' controller implements beego.ControllerInterface and // its' controller implements beego.ControllerInterface and
// defines a param "pattern/:objectId" to visit each resource. // defines a param "pattern/:objectId" to visit each resource.
func RESTRouter(rootpath string, c ControllerInterface) *App { func (app *HttpServer) RESTRouter(rootpath string, c ControllerInterface) *HttpServer {
Router(rootpath, c) app.Router(rootpath, c)
Router(path.Join(rootpath, ":objectId"), c) app.Router(path.Join(rootpath, ":objectId"), c)
return BeeApp return app
}
// AutoRouter see HttpServer.AutoRouter
func AutoRouter(c ControllerInterface) *HttpServer {
return BeeApp.AutoRouter(c)
} }
// AutoRouter adds defined controller handler to BeeApp. // AutoRouter adds defined controller handler to BeeApp.
// it's same to App.AutoRouter. // it's same to HttpServer.AutoRouter.
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page, // if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
// visit the url /main/list to exec List function or /main/page to exec Page function. // visit the url /main/list to exec List function or /main/page to exec Page function.
func AutoRouter(c ControllerInterface) *App { func (app *HttpServer) AutoRouter(c ControllerInterface) *HttpServer {
BeeApp.Handlers.AddAuto(c) app.Handlers.AddAuto(c)
return BeeApp return app
}
// AutoPrefix see HttpServer.AutoPrefix
func AutoPrefix(prefix string, c ControllerInterface) *HttpServer {
return BeeApp.AutoPrefix(prefix, c)
} }
// AutoPrefix adds controller handler to BeeApp with prefix. // AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix. // it's same to HttpServer.AutoRouterWithPrefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page, // if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function. // visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func AutoPrefix(prefix string, c ControllerInterface) *App { func (app *HttpServer) AutoPrefix(prefix string, c ControllerInterface) *HttpServer {
BeeApp.Handlers.AddAutoPrefix(prefix, c) app.Handlers.AddAutoPrefix(prefix, c)
return BeeApp return app
}
// Get see HttpServer.Get
func Get(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Get(rootpath, f)
} }
// Get used to register router for Get method // Get used to register router for Get method
@ -403,9 +466,14 @@ func AutoPrefix(prefix string, c ControllerInterface) *App {
// beego.Get("/", func(ctx *context.Context){ // beego.Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Get(rootpath string, f FilterFunc) *App { func (app *HttpServer) Get(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Get(rootpath, f) app.Handlers.Get(rootpath, f)
return BeeApp return app
}
// Post see HttpServer.Post
func Post(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Post(rootpath, f)
} }
// Post used to register router for Post method // Post used to register router for Post method
@ -413,9 +481,14 @@ func Get(rootpath string, f FilterFunc) *App {
// beego.Post("/api", func(ctx *context.Context){ // beego.Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Post(rootpath string, f FilterFunc) *App { func (app *HttpServer) Post(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Post(rootpath, f) app.Handlers.Post(rootpath, f)
return BeeApp return app
}
// Delete see HttpServer.Delete
func Delete(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Delete(rootpath, f)
} }
// Delete used to register router for Delete method // Delete used to register router for Delete method
@ -423,9 +496,14 @@ func Post(rootpath string, f FilterFunc) *App {
// beego.Delete("/api", func(ctx *context.Context){ // beego.Delete("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Delete(rootpath string, f FilterFunc) *App { func (app *HttpServer) Delete(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Delete(rootpath, f) app.Handlers.Delete(rootpath, f)
return BeeApp return app
}
// Put see HttpServer.Put
func Put(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Put(rootpath, f)
} }
// Put used to register router for Put method // Put used to register router for Put method
@ -433,9 +511,14 @@ func Delete(rootpath string, f FilterFunc) *App {
// beego.Put("/api", func(ctx *context.Context){ // beego.Put("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Put(rootpath string, f FilterFunc) *App { func (app *HttpServer) Put(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Put(rootpath, f) app.Handlers.Put(rootpath, f)
return BeeApp return app
}
// Head see HttpServer.Head
func Head(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Head(rootpath, f)
} }
// Head used to register router for Head method // Head used to register router for Head method
@ -443,8 +526,14 @@ func Put(rootpath string, f FilterFunc) *App {
// beego.Head("/api", func(ctx *context.Context){ // beego.Head("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Head(rootpath string, f FilterFunc) *App { func (app *HttpServer) Head(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Head(rootpath, f) app.Handlers.Head(rootpath, f)
return app
}
// Options see HttpServer.Options
func Options(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Options(rootpath, f)
return BeeApp return BeeApp
} }
@ -453,9 +542,14 @@ func Head(rootpath string, f FilterFunc) *App {
// beego.Options("/api", func(ctx *context.Context){ // beego.Options("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Options(rootpath string, f FilterFunc) *App { func (app *HttpServer) Options(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Options(rootpath, f) app.Handlers.Options(rootpath, f)
return BeeApp return app
}
// Patch see HttpServer.Patch
func Patch(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Patch(rootpath, f)
} }
// Patch used to register router for Patch method // Patch used to register router for Patch method
@ -463,9 +557,14 @@ func Options(rootpath string, f FilterFunc) *App {
// beego.Patch("/api", func(ctx *context.Context){ // beego.Patch("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Patch(rootpath string, f FilterFunc) *App { func (app *HttpServer) Patch(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Patch(rootpath, f) app.Handlers.Patch(rootpath, f)
return BeeApp return app
}
// Any see HttpServer.Any
func Any(rootpath string, f FilterFunc) *HttpServer {
return BeeApp.Any(rootpath, f)
} }
// Any used to register router for all methods // Any used to register router for all methods
@ -473,9 +572,14 @@ func Patch(rootpath string, f FilterFunc) *App {
// beego.Any("/api", func(ctx *context.Context){ // beego.Any("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world") // ctx.Output.Body("hello world")
// }) // })
func Any(rootpath string, f FilterFunc) *App { func (app *HttpServer) Any(rootpath string, f FilterFunc) *HttpServer {
BeeApp.Handlers.Any(rootpath, f) app.Handlers.Any(rootpath, f)
return BeeApp return app
}
// Handler see HttpServer.Handler
func Handler(rootpath string, h http.Handler, options ...interface{}) *HttpServer {
return BeeApp.Handler(rootpath, h, options...)
} }
// Handler used to register a Handler router // Handler used to register a Handler router
@ -483,24 +587,81 @@ func Any(rootpath string, f FilterFunc) *App {
// beego.Handler("/api", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) { // beego.Handler("/api", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) // fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
// })) // }))
func Handler(rootpath string, h http.Handler, options ...interface{}) *App { func (app *HttpServer) Handler(rootpath string, h http.Handler, options ...interface{}) *HttpServer {
BeeApp.Handlers.Handler(rootpath, h, options...) app.Handlers.Handler(rootpath, h, options...)
return BeeApp return app
}
// InserFilter see HttpServer.InsertFilter
func InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) *HttpServer {
return BeeApp.InsertFilter(pattern, pos, filter, opts...)
} }
// InsertFilter adds a FilterFunc with pattern condition and action constant. // InsertFilter adds a FilterFunc with pattern condition and action constant.
// The pos means action constant including // The pos means action constant including
// beego.BeforeStatic, beego.BeforeRouter, beego.BeforeExec, beego.AfterExec and beego.FinishRouter. // beego.BeforeStatic, beego.BeforeRouter, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute) // The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
func InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) *App { func (app *HttpServer) InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) *HttpServer {
BeeApp.Handlers.InsertFilter(pattern, pos, filter, opts...) app.Handlers.InsertFilter(pattern, pos, filter, opts...)
return BeeApp return app
}
// InsertFilterChain see HttpServer.InsertFilterChain
func InsertFilterChain(pattern string, filterChain FilterChain, opts ...FilterOpt) *HttpServer {
return BeeApp.InsertFilterChain(pattern, filterChain, opts...)
} }
// InsertFilterChain adds a FilterFunc built by filterChain. // InsertFilterChain adds a FilterFunc built by filterChain.
// This filter will be executed before all filters. // This filter will be executed before all filters.
// the filter's behavior is like stack // the filter's behavior like stack's behavior
func InsertFilterChain(pattern string, filterChain FilterChain, opts ...FilterOpt) *App { // and the last filter is serving the http request
BeeApp.Handlers.InsertFilterChain(pattern, filterChain, opts...) func (app *HttpServer) InsertFilterChain(pattern string, filterChain FilterChain, opts ...FilterOpt) *HttpServer {
return BeeApp app.Handlers.InsertFilterChain(pattern, filterChain, opts...)
return app
}
func (app *HttpServer) initAddr(addr string) {
strs := strings.Split(addr, ":")
if len(strs) > 0 && strs[0] != "" {
app.Cfg.Listen.HTTPAddr = strs[0]
app.Cfg.Listen.Domains = []string{strs[0]}
}
if len(strs) > 1 && strs[1] != "" {
app.Cfg.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
}
}
func (app *HttpServer) LogAccess(ctx *beecontext.Context, startTime *time.Time, statusCode int) {
// Skip logging if AccessLogs config is false
if !app.Cfg.Log.AccessLogs {
return
}
// Skip logging static requests unless EnableStaticLogs config is true
if !app.Cfg.Log.EnableStaticLogs && DefaultAccessLogFilter.Filter(ctx) {
return
}
var (
requestTime time.Time
elapsedTime time.Duration
r = ctx.Request
)
if startTime != nil {
requestTime = *startTime
elapsedTime = time.Since(*startTime)
}
record := &logs.AccessLogRecord{
RemoteAddr: ctx.Input.IP(),
RequestTime: requestTime,
RequestMethod: r.Method,
Request: fmt.Sprintf("%s %s %s", r.Method, r.RequestURI, r.Proto),
ServerProtocol: r.Proto,
Host: r.Host,
Status: statusCode,
ElapsedTime: elapsedTime,
HTTPReferrer: r.Header.Get("Referer"),
HTTPUserAgent: r.Header.Get("User-Agent"),
RemoteUser: r.Header.Get("Remote-User"),
BodyBytesSent: r.ContentLength,
}
logs.AccessLog(record, app.Cfg.Log.AccessLogsFormat)
} }

View File

@ -0,0 +1,31 @@
// Copyright 2020
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
package web
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewHttpServerWithCfg(t *testing.T) {
// we should make sure that update server's config won't change
BConfig.AppName = "Before"
svr := NewHttpServerWithCfg(*BConfig)
svr.Cfg.AppName = "hello"
assert.NotEqual(t, "hello", BConfig.AppName)
assert.Equal(t, "Before", BConfig.AppName)
}

View File

@ -368,14 +368,14 @@ func SetTemplateFSFunc(fnt templateFSFunc) {
} }
// SetViewsPath sets view directory path in beego application. // SetViewsPath sets view directory path in beego application.
func SetViewsPath(path string) *App { func SetViewsPath(path string) *HttpServer {
BConfig.WebConfig.ViewsPath = path BConfig.WebConfig.ViewsPath = path
return BeeApp return BeeApp
} }
// SetStaticPath sets static directory path and proper url pattern in beego application. // SetStaticPath sets static directory path and proper url pattern in beego application.
// if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public". // if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
func SetStaticPath(url string, path string) *App { func SetStaticPath(url string, path string) *HttpServer {
if !strings.HasPrefix(url, "/") { if !strings.HasPrefix(url, "/") {
url = "/" + url url = "/" + url
} }
@ -387,7 +387,7 @@ func SetStaticPath(url string, path string) *App {
} }
// DelStaticPath removes the static folder setting in this url pattern in beego application. // DelStaticPath removes the static folder setting in this url pattern in beego application.
func DelStaticPath(url string) *App { func DelStaticPath(url string) *HttpServer {
if !strings.HasPrefix(url, "/") { if !strings.HasPrefix(url, "/") {
url = "/" + url url = "/" + url
} }
@ -399,7 +399,7 @@ func DelStaticPath(url string) *App {
} }
// AddTemplateEngine add a new templatePreProcessor which support extension // AddTemplateEngine add a new templatePreProcessor which support extension
func AddTemplateEngine(extension string, fn templatePreProcessor) *App { func AddTemplateEngine(extension string, fn templatePreProcessor) *HttpServer {
AddTemplateExt(extension) AddTemplateExt(extension)
beeTemplateEngines[extension] = fn beeTemplateEngines[extension] = fn
return BeeApp return BeeApp