From fdb6e3d25eb43a973c1d11a00e8ed0a1e6ed9686 Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Fri, 5 Jan 2018 17:31:30 +0800 Subject: [PATCH 1/4] update example/middleware --- example/middleware/main.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/example/middleware/main.go b/example/middleware/main.go index 72b3d55..56af48b 100644 --- a/example/middleware/main.go +++ b/example/middleware/main.go @@ -22,8 +22,6 @@ func main() { //设置gzip开关 //app.SetEnabledGzip(true) - //设置路由 - InitRoute(app.HttpServer) //InitModule(app) @@ -31,7 +29,15 @@ func main() { app.Use( NewAccessFmtLog("app"), ) - app.ExcludeUse(NewAccessFmtLog("appex"), "/", "/") + exAccessFmtLog := NewAccessFmtLog("appex") + exAccessFmtLog.Exclude("/index") + exAccessFmtLog.Exclude("/v1/machines/queryIP/:IP") + app.Use(exAccessFmtLog) + + app.ExcludeUse(NewAccessFmtLog("appex1"), "/") + + //设置路由 + InitRoute(app.HttpServer) //启动 监控服务 app.SetPProfConfig(true, 8081) @@ -50,13 +56,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")) From 8948a8881ea00353ddf74fce25f098dfebe9d59a Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Sun, 7 Jan 2018 22:00:56 +0800 Subject: [PATCH 2/4] =?UTF-8?q?####=20Version=201.4.3=20*=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4dotweb=E5=86=85=E9=83=A8=E8=B7=AF=E7=94=B1=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E9=80=BB=E8=BE=91=EF=BC=8CNew=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=8D=E5=BC=80=E5=90=AF=EF=BC=8CClassic?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AF=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E9=80=9A=E8=BF=87app.UseDotwebRouter=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E5=BC=80=E5=90=AF=20*=20=E4=BF=AE=E5=A4=8D=20issue=20?= =?UTF-8?q?#100,=20=E8=A7=A3=E5=86=B3=E7=89=B9=E5=AE=9A=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=B8=8BExclude=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20*=20Use=20filepath.Ext=20to=20replace=20file.GetFileExt,=20u?= =?UTF-8?q?pdate=20for=20issue=20#99=20*=20=E5=90=8C=E6=AD=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0example=E4=BB=A3=E7=A0=81=20*=202018-01-07=2022:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dotweb.go | 23 +++++++++++++++++------ example/middleware/main.go | 14 +++----------- uploadfile.go | 3 ++- version.MD | 7 +++++++ 4 files changed, 29 insertions(+), 18 deletions(-) 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 56af48b..eee3e2a 100644 --- a/example/middleware/main.go +++ b/example/middleware/main.go @@ -19,23 +19,15 @@ func main() { //开启development模式 app.SetDevelopmentMode() - //设置gzip开关 - //app.SetEnabledGzip(true) - - - //InitModule(app) - - //app.UseRequestLog() - app.Use( - NewAccessFmtLog("app"), - ) exAccessFmtLog := NewAccessFmtLog("appex") exAccessFmtLog.Exclude("/index") exAccessFmtLog.Exclude("/v1/machines/queryIP/:IP") app.Use(exAccessFmtLog) app.ExcludeUse(NewAccessFmtLog("appex1"), "/") - + app.Use( + NewAccessFmtLog("app"), + ) //设置路由 InitRoute(app.HttpServer) diff --git a/uploadfile.go b/uploadfile.go index 2ca38f8..c7b6e6c 100644 --- a/uploadfile.go +++ b/uploadfile.go @@ -6,6 +6,7 @@ import ( "io" "mime/multipart" "os" + "path/filepath" ) type UploadFile struct { @@ -21,7 +22,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..fcc09e4 100644 --- a/version.MD +++ b/version.MD @@ -1,5 +1,12 @@ ## dotweb版本记录: +#### Version 1.4.3 +* 调整dotweb内部路由注册逻辑,New模式默认不开启,Classic模式默认开启,可通过app.UseDotwebRouter手动开启 +* 修复 issue #100, 解决特定场景下Exclude不生效问题 +* Use filepath.Ext to replace file.GetFileExt, update for issue #99 +* 同步更新example代码 +* 2018-01-07 22:00 + #### Version 1.4.2 * Context新增QueryInt\QueryInt64接口,用于简化获取Int类型的Get参数,如果参数未传入或不是合法整形,返回0 * Context接口调整:除Write外,其他WriteXXX接口,返回值从(int, error)调整为error From 7828e6cf6c6664491c05a341f3b76ce2354d9639 Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Sun, 7 Jan 2018 22:01:49 +0800 Subject: [PATCH 3/4] =?UTF-8?q?####=20Version=201.4.3=20*=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4dotweb=E5=86=85=E9=83=A8=E8=B7=AF=E7=94=B1=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E9=80=BB=E8=BE=91=EF=BC=8CNew=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=8D=E5=BC=80=E5=90=AF=EF=BC=8CClassic?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AF=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E9=80=9A=E8=BF=87app.UseDotwebRouter=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E5=BC=80=E5=90=AF=20*=20=E4=BF=AE=E5=A4=8D=20issue=20?= =?UTF-8?q?#100,=20=E8=A7=A3=E5=86=B3=E7=89=B9=E5=AE=9A=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=B8=8BExclude=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20*=20Use=20filepath.Ext=20to=20replace=20file.GetFileExt,=20u?= =?UTF-8?q?pdate=20for=20issue=20#99=20*=20=E5=90=8C=E6=AD=A5=E6=9B=B4?= =?UTF-8?q?=E6=96=B0example=E4=BB=A3=E7=A0=81=20*=202018-01-07=2022:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uploadfile.go | 1 - 1 file changed, 1 deletion(-) diff --git a/uploadfile.go b/uploadfile.go index c7b6e6c..29c4e0a 100644 --- a/uploadfile.go +++ b/uploadfile.go @@ -2,7 +2,6 @@ package dotweb import ( "errors" - files "github.com/devfeel/dotweb/framework/file" "io" "mime/multipart" "os" From 9348cd373482cdd9e061259999a57717e3f3206a Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Sun, 7 Jan 2018 22:05:40 +0800 Subject: [PATCH 4/4] =?UTF-8?q?####=20Version=201.4.3=20*=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4dotweb=E5=86=85=E9=83=A8=E8=B7=AF=E7=94=B1=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E9=80=BB=E8=BE=91=EF=BC=8CNew=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=8D=E5=BC=80=E5=90=AF=EF=BC=8CClassic?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AF=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E9=80=9A=E8=BF=87app.UseDotwebRouter=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E5=BC=80=E5=90=AF=20*=20=E4=BF=AE=E5=A4=8D=20issue=20?= =?UTF-8?q?#100,=20=E8=A7=A3=E5=86=B3=E7=89=B9=E5=AE=9A=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=B8=8BExclude=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20*=20Use=20filepath.Ext=20to=20replace=20file.GetFileExt,=20u?= =?UTF-8?q?pdate=20for=20issue=20#99=20*=20=E7=A7=BB=E9=99=A4=20framework/?= =?UTF-8?q?file.GetFileExt=20=E5=87=BD=E6=95=B0=20*=20=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0example=E4=BB=A3=E7=A0=81=20*=202018-01-07=20?= =?UTF-8?q?22:00?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/file/file.go | 14 -------------- framework/file/file_test.go | 5 +++-- version.MD | 1 + 3 files changed, 4 insertions(+), 16 deletions(-) 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/version.MD b/version.MD index fcc09e4..4baa3eb 100644 --- a/version.MD +++ b/version.MD @@ -4,6 +4,7 @@ * 调整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