mirror of
https://github.com/astaxie/beego.git
synced 2025-06-12 09:40:41 +00:00
Adapter: web module
This commit is contained in:
@ -199,7 +199,7 @@ func (app *App) Run(mws ...MiddleWare) {
|
||||
pool.AppendCertsFromPEM(data)
|
||||
app.Server.TLSConfig = &tls.Config{
|
||||
ClientCAs: pool,
|
||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
||||
ClientAuth: tls.ClientAuthType(BConfig.Listen.ClientAuth),
|
||||
}
|
||||
}
|
||||
if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
|
||||
|
@ -16,6 +16,7 @@ package web
|
||||
|
||||
import (
|
||||
context2 "context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -72,6 +73,7 @@ type Listen struct {
|
||||
AdminPort int
|
||||
EnableFcgi bool
|
||||
EnableStdIo bool // EnableStdIo works with EnableFcgi Use FCGI via standard I/O
|
||||
ClientAuth int
|
||||
}
|
||||
|
||||
// WebConfig holds web related config
|
||||
@ -234,6 +236,7 @@ func newBConfig() *Config {
|
||||
AdminPort: 8088,
|
||||
EnableFcgi: false,
|
||||
EnableStdIo: false,
|
||||
ClientAuth: int(tls.RequireAndVerifyClientCert),
|
||||
},
|
||||
WebConfig: WebConfig{
|
||||
AutoRender: true,
|
||||
|
@ -43,24 +43,26 @@ type FilterRouter struct {
|
||||
// params is for:
|
||||
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
|
||||
// 2. determining whether or not params need to be reset.
|
||||
func newFilterRouter(pattern string, routerCaseSensitive bool, filter FilterFunc, params ...bool) *FilterRouter {
|
||||
func newFilterRouter(pattern string, filter FilterFunc, opts ...FilterOpt) *FilterRouter {
|
||||
mr := &FilterRouter{
|
||||
tree: NewTree(),
|
||||
pattern: pattern,
|
||||
filterFunc: filter,
|
||||
returnOnOutput: true,
|
||||
}
|
||||
if !routerCaseSensitive {
|
||||
|
||||
fos := &filterOpts{}
|
||||
|
||||
for _, o := range opts {
|
||||
o(fos)
|
||||
}
|
||||
|
||||
if !fos.routerCaseSensitive {
|
||||
mr.pattern = strings.ToLower(pattern)
|
||||
}
|
||||
|
||||
paramsLen := len(params)
|
||||
if paramsLen > 0 {
|
||||
mr.returnOnOutput = params[0]
|
||||
}
|
||||
if paramsLen > 1 {
|
||||
mr.resetParams = params[1]
|
||||
}
|
||||
mr.returnOnOutput = fos.returnOnOutput
|
||||
mr.resetParams = fos.resetParams
|
||||
mr.tree.AddRouter(pattern, true)
|
||||
return mr
|
||||
}
|
||||
@ -103,3 +105,29 @@ func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type filterOpts struct {
|
||||
returnOnOutput bool
|
||||
resetParams bool
|
||||
routerCaseSensitive bool
|
||||
}
|
||||
|
||||
type FilterOpt func(opts *filterOpts)
|
||||
|
||||
func WithReturnOnOutput(ret bool) FilterOpt {
|
||||
return func(opts *filterOpts) {
|
||||
opts.returnOnOutput = ret
|
||||
}
|
||||
}
|
||||
|
||||
func WithResetParams(reset bool) FilterOpt {
|
||||
return func(opts *filterOpts) {
|
||||
opts.resetParams = reset
|
||||
}
|
||||
}
|
||||
|
||||
func WithCaseSensitive(sensitive bool) FilterOpt {
|
||||
return func(opts *filterOpts) {
|
||||
opts.routerCaseSensitive = sensitive
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ func NewControllerRegister() *ControllerRegister {
|
||||
},
|
||||
},
|
||||
}
|
||||
res.chainRoot = newFilterRouter("/*", false, res.serveHttp)
|
||||
res.chainRoot = newFilterRouter("/*", res.serveHttp, WithCaseSensitive(false))
|
||||
return res
|
||||
}
|
||||
|
||||
@ -262,7 +262,7 @@ func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
||||
if comm, ok := GlobalControllerRouter[key]; ok {
|
||||
for _, a := range comm {
|
||||
for _, f := range a.Filters {
|
||||
p.InsertFilter(f.Pattern, f.Pos, f.Filter, f.ReturnOnOutput, f.ResetParams)
|
||||
p.InsertFilter(f.Pattern, f.Pos, f.Filter, WithReturnOnOutput(f.ReturnOnOutput), WithResetParams(f.ResetParams))
|
||||
}
|
||||
|
||||
p.addWithMethodParams(a.Router, c, a.MethodParams, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
|
||||
@ -452,8 +452,9 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface)
|
||||
// params is for:
|
||||
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
|
||||
// 2. determining whether or not params need to be reset.
|
||||
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) error {
|
||||
mr := newFilterRouter(pattern, BConfig.RouterCaseSensitive, filter, params...)
|
||||
func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, opts ...FilterOpt) error {
|
||||
opts = append(opts, WithCaseSensitive(BConfig.RouterCaseSensitive))
|
||||
mr := newFilterRouter(pattern, filter, opts...)
|
||||
return p.insertFilterRouter(pos, mr)
|
||||
}
|
||||
|
||||
@ -468,10 +469,11 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter
|
||||
// // do something
|
||||
// }
|
||||
// }
|
||||
func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, params ...bool) {
|
||||
func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) {
|
||||
root := p.chainRoot
|
||||
filterFunc := chain(root.filterFunc)
|
||||
p.chainRoot = newFilterRouter(pattern, BConfig.RouterCaseSensitive, filterFunc, params...)
|
||||
opts = append(opts, WithCaseSensitive(BConfig.RouterCaseSensitive))
|
||||
p.chainRoot = newFilterRouter(pattern, filterFunc, opts...)
|
||||
p.chainRoot.next = root
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user