diff --git a/dotweb.go b/dotweb.go index 6aff7c0..a5e7d13 100644 --- a/dotweb.go +++ b/dotweb.go @@ -19,6 +19,7 @@ import ( "github.com/devfeel/dotweb/logger" "github.com/devfeel/dotweb/servers" "github.com/devfeel/dotweb/session" + "reflect" "sync" ) @@ -35,6 +36,7 @@ type ( AppContext *core.ItemContext middlewareMap map[string]MiddlewareFunc middlewareMutex *sync.RWMutex + StartMode string } // ExceptionHandle 支持自定义异常处理代码能力 @@ -52,6 +54,9 @@ const ( DefaultHTTPPort = 8080 //DefaultHTTPPort default http port; fixed for #70 UPDATE default http port 80 to 8080 RunMode_Development = "development" RunMode_Production = "production" + + StartMode_New = "New" + StartMode_Classic = "Classic" ) //New create and return DotApp instance @@ -64,8 +69,12 @@ func New() *DotWeb { Config: config.NewConfig(), middlewareMap: make(map[string]MiddlewareFunc), middlewareMutex: new(sync.RWMutex), + StartMode: StartMode_New, } app.HttpServer.setDotApp(app) + //add default httphandler with middlewares + //fixed for issue #100 + app.Use(&xMiddleware{}) //init logger logger.InitLog() @@ -78,6 +87,7 @@ func New() *DotWeb { // 3.print logo func Classic() *DotWeb { app := New() + app.StartMode = StartMode_Classic app.SetEnabledLog(true) app.UseRequestLog() @@ -264,7 +274,9 @@ func (app *DotWeb) ListenAndServe(addr string) error { app.initBindMiddleware() - app.initInnerRouter() + if app.StartMode == StartMode_Classic { + app.UseDotwebRouter() + } if app.HttpServer.ServerConfig().EnabledTLS { err := app.HttpServer.ListenAndServeTLS(addr, app.HttpServer.ServerConfig().TLSCertFile, app.HttpServer.ServerConfig().TLSKeyFile) @@ -389,23 +401,22 @@ func (app *DotWeb) initRegisterConfigGroup() { // init bind app's middleware to router node func (app *DotWeb) initBindMiddleware() { - //add default httphandler with middlewares - app.Use(&xMiddleware{}) - router := app.HttpServer.Router().(*router) for path, node := range router.allNodeMap { - logger.Logger().Debug("DotWeb initBindMiddleware "+path+" "+fmt.Sprint(node), LogTarget_HttpServer) node.appMiddlewares = app.Middlewares for _, m := range node.appMiddlewares { if m.HasExclude() && m.ExistsExcludeRouter(node.fullPath) { + logger.Logger().Debug("DotWeb initBindMiddleware "+path+" "+reflect.TypeOf(m).String()+" exclude", LogTarget_HttpServer) node.hasExcludeMiddleware = true + } else { + logger.Logger().Debug("DotWeb initBindMiddleware "+path+" "+reflect.TypeOf(m).String()+" match", LogTarget_HttpServer) } } } } // init inner routers -func (app *DotWeb) initInnerRouter() { +func (app *DotWeb) UseDotwebRouter() { //默认支持pprof信息查看 gInner := app.HttpServer.Group("/dotweb") gInner.GET("/debug/pprof/:key", initPProf) diff --git a/example/middleware/main.go b/example/middleware/main.go index 72b3d55..eee3e2a 100644 --- a/example/middleware/main.go +++ b/example/middleware/main.go @@ -19,19 +19,17 @@ func main() { //开启development模式 app.SetDevelopmentMode() - //设置gzip开关 - //app.SetEnabledGzip(true) + exAccessFmtLog := NewAccessFmtLog("appex") + exAccessFmtLog.Exclude("/index") + exAccessFmtLog.Exclude("/v1/machines/queryIP/:IP") + app.Use(exAccessFmtLog) - //设置路由 - InitRoute(app.HttpServer) - - //InitModule(app) - - //app.UseRequestLog() + app.ExcludeUse(NewAccessFmtLog("appex1"), "/") app.Use( NewAccessFmtLog("app"), ) - app.ExcludeUse(NewAccessFmtLog("appex"), "/", "/") + //设置路由 + InitRoute(app.HttpServer) //启动 监控服务 app.SetPProfConfig(true, 8081) @@ -50,13 +48,16 @@ func main() { func Index(ctx dotweb.Context) error { ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8") //fmt.Println(time.Now(), "Index Handler") - err := ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares())) + err := ctx.WriteString("index => ", ctx.Request().Url()) fmt.Println(ctx.RouterNode().GroupMiddlewares()) return err } func InitRoute(server *dotweb.HttpServer) { server.Router().GET("/", Index) + server.Router().GET("/index", Index) + server.Router().GET("/v1/machines/queryIP/:IP", Index) + server.Router().GET("/v1/machines/queryIP2", Index) server.Router().GET("/use", Index).Use(NewAccessFmtLog("Router-use")) g := server.Group("/group").Use(NewAccessFmtLog("group")).Use(NewSimpleAuth("admin")) diff --git a/framework/file/file.go b/framework/file/file.go index 80b21ca..3d704eb 100644 --- a/framework/file/file.go +++ b/framework/file/file.go @@ -16,20 +16,6 @@ func GetCurrentDirectory() string { return strings.Replace(dir, "\\", "/", -1) } -//get filename extensions -func GetFileExt(fileName string) string { - if fileName == "" { - return "" - } else { - index := strings.LastIndex(fileName, ".") - if index < 0 { - return "" - } else { - return string(fileName[index:]) - } - } -} - //check filename is exist func Exist(filename string) bool { _, err := os.Stat(filename) diff --git a/framework/file/file_test.go b/framework/file/file_test.go index 110e59a..194c9aa 100644 --- a/framework/file/file_test.go +++ b/framework/file/file_test.go @@ -1,6 +1,7 @@ package file import ( + "path/filepath" "testing" ) @@ -13,7 +14,7 @@ func Test_GetCurrentDirectory_1(t *testing.T) { func Test_GetFileExt_1(t *testing.T) { fn := "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/download/vagrant_1.9.2.dmg" - fileExt := GetFileExt(fn) + fileExt := filepath.Ext(fn) if len(fileExt) < 1 { t.Error("fileExt null!") } else { @@ -23,7 +24,7 @@ func Test_GetFileExt_1(t *testing.T) { func Test_GetFileExt_2(t *testing.T) { fn := "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/download/vagrant_1" - fileExt := GetFileExt(fn) + fileExt := filepath.Ext(fn) if len(fileExt) < 1 { t.Error("fileExt null!") } else { diff --git a/uploadfile.go b/uploadfile.go index 2ca38f8..29c4e0a 100644 --- a/uploadfile.go +++ b/uploadfile.go @@ -2,10 +2,10 @@ package dotweb import ( "errors" - files "github.com/devfeel/dotweb/framework/file" "io" "mime/multipart" "os" + "path/filepath" ) type UploadFile struct { @@ -21,7 +21,7 @@ func NewUploadFile(file multipart.File, header *multipart.FileHeader) *UploadFil File: file, Header: header, fileName: header.Filename, - fileExt: files.GetFileExt(header.Filename), + fileExt: filepath.Ext(header.Filename), //update for issue #99 } } diff --git a/version.MD b/version.MD index 06e16b5..4baa3eb 100644 --- a/version.MD +++ b/version.MD @@ -1,5 +1,13 @@ ## dotweb版本记录: +#### Version 1.4.3 +* 调整dotweb内部路由注册逻辑,New模式默认不开启,Classic模式默认开启,可通过app.UseDotwebRouter手动开启 +* 修复 issue #100, 解决特定场景下Exclude不生效问题 +* Use filepath.Ext to replace file.GetFileExt, update for issue #99 +* 移除 framework/file.GetFileExt 函数 +* 同步更新example代码 +* 2018-01-07 22:00 + #### Version 1.4.2 * Context新增QueryInt\QueryInt64接口,用于简化获取Int类型的Get参数,如果参数未传入或不是合法整形,返回0 * Context接口调整:除Write外,其他WriteXXX接口,返回值从(int, error)调整为error