Skip to content

Commit 34f6a50

Browse files
authored
Merge pull request devfeel#101 from devfeel/develop
Version 1.4.3 - BUG修复,优化代码
2 parents 129df76 + 9348cd3 commit 34f6a50

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

dotweb.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/devfeel/dotweb/logger"
2020
"github.com/devfeel/dotweb/servers"
2121
"github.com/devfeel/dotweb/session"
22+
"reflect"
2223
"sync"
2324
)
2425

@@ -35,6 +36,7 @@ type (
3536
AppContext *core.ItemContext
3637
middlewareMap map[string]MiddlewareFunc
3738
middlewareMutex *sync.RWMutex
39+
StartMode string
3840
}
3941

4042
// ExceptionHandle 支持自定义异常处理代码能力
@@ -52,6 +54,9 @@ const (
5254
DefaultHTTPPort = 8080 //DefaultHTTPPort default http port; fixed for #70 UPDATE default http port 80 to 8080
5355
RunMode_Development = "development"
5456
RunMode_Production = "production"
57+
58+
StartMode_New = "New"
59+
StartMode_Classic = "Classic"
5560
)
5661

5762
//New create and return DotApp instance
@@ -64,8 +69,12 @@ func New() *DotWeb {
6469
Config: config.NewConfig(),
6570
middlewareMap: make(map[string]MiddlewareFunc),
6671
middlewareMutex: new(sync.RWMutex),
72+
StartMode: StartMode_New,
6773
}
6874
app.HttpServer.setDotApp(app)
75+
//add default httphandler with middlewares
76+
//fixed for issue #100
77+
app.Use(&xMiddleware{})
6978

7079
//init logger
7180
logger.InitLog()
@@ -78,6 +87,7 @@ func New() *DotWeb {
7887
// 3.print logo
7988
func Classic() *DotWeb {
8089
app := New()
90+
app.StartMode = StartMode_Classic
8191

8292
app.SetEnabledLog(true)
8393
app.UseRequestLog()
@@ -264,7 +274,9 @@ func (app *DotWeb) ListenAndServe(addr string) error {
264274

265275
app.initBindMiddleware()
266276

267-
app.initInnerRouter()
277+
if app.StartMode == StartMode_Classic {
278+
app.UseDotwebRouter()
279+
}
268280

269281
if app.HttpServer.ServerConfig().EnabledTLS {
270282
err := app.HttpServer.ListenAndServeTLS(addr, app.HttpServer.ServerConfig().TLSCertFile, app.HttpServer.ServerConfig().TLSKeyFile)
@@ -389,23 +401,22 @@ func (app *DotWeb) initRegisterConfigGroup() {
389401

390402
// init bind app's middleware to router node
391403
func (app *DotWeb) initBindMiddleware() {
392-
//add default httphandler with middlewares
393-
app.Use(&xMiddleware{})
394-
395404
router := app.HttpServer.Router().(*router)
396405
for path, node := range router.allNodeMap {
397-
logger.Logger().Debug("DotWeb initBindMiddleware "+path+" "+fmt.Sprint(node), LogTarget_HttpServer)
398406
node.appMiddlewares = app.Middlewares
399407
for _, m := range node.appMiddlewares {
400408
if m.HasExclude() && m.ExistsExcludeRouter(node.fullPath) {
409+
logger.Logger().Debug("DotWeb initBindMiddleware "+path+" "+reflect.TypeOf(m).String()+" exclude", LogTarget_HttpServer)
401410
node.hasExcludeMiddleware = true
411+
} else {
412+
logger.Logger().Debug("DotWeb initBindMiddleware "+path+" "+reflect.TypeOf(m).String()+" match", LogTarget_HttpServer)
402413
}
403414
}
404415
}
405416
}
406417

407418
// init inner routers
408-
func (app *DotWeb) initInnerRouter() {
419+
func (app *DotWeb) UseDotwebRouter() {
409420
//默认支持pprof信息查看
410421
gInner := app.HttpServer.Group("/dotweb")
411422
gInner.GET("/debug/pprof/:key", initPProf)

example/middleware/main.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ func main() {
1919
//开启development模式
2020
app.SetDevelopmentMode()
2121

22-
//设置gzip开关
23-
//app.SetEnabledGzip(true)
22+
exAccessFmtLog := NewAccessFmtLog("appex")
23+
exAccessFmtLog.Exclude("/index")
24+
exAccessFmtLog.Exclude("/v1/machines/queryIP/:IP")
25+
app.Use(exAccessFmtLog)
2426

25-
//设置路由
26-
InitRoute(app.HttpServer)
27-
28-
//InitModule(app)
29-
30-
//app.UseRequestLog()
27+
app.ExcludeUse(NewAccessFmtLog("appex1"), "/")
3128
app.Use(
3229
NewAccessFmtLog("app"),
3330
)
34-
app.ExcludeUse(NewAccessFmtLog("appex"), "/", "/")
31+
//设置路由
32+
InitRoute(app.HttpServer)
3533

3634
//启动 监控服务
3735
app.SetPProfConfig(true, 8081)
@@ -50,13 +48,16 @@ func main() {
5048
func Index(ctx dotweb.Context) error {
5149
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
5250
//fmt.Println(time.Now(), "Index Handler")
53-
err := ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
51+
err := ctx.WriteString("index => ", ctx.Request().Url())
5452
fmt.Println(ctx.RouterNode().GroupMiddlewares())
5553
return err
5654
}
5755

5856
func InitRoute(server *dotweb.HttpServer) {
5957
server.Router().GET("/", Index)
58+
server.Router().GET("/index", Index)
59+
server.Router().GET("/v1/machines/queryIP/:IP", Index)
60+
server.Router().GET("/v1/machines/queryIP2", Index)
6061
server.Router().GET("/use", Index).Use(NewAccessFmtLog("Router-use"))
6162

6263
g := server.Group("/group").Use(NewAccessFmtLog("group")).Use(NewSimpleAuth("admin"))

framework/file/file.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ func GetCurrentDirectory() string {
1616
return strings.Replace(dir, "\\", "/", -1)
1717
}
1818

19-
//get filename extensions
20-
func GetFileExt(fileName string) string {
21-
if fileName == "" {
22-
return ""
23-
} else {
24-
index := strings.LastIndex(fileName, ".")
25-
if index < 0 {
26-
return ""
27-
} else {
28-
return string(fileName[index:])
29-
}
30-
}
31-
}
32-
3319
//check filename is exist
3420
func Exist(filename string) bool {
3521
_, err := os.Stat(filename)

framework/file/file_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package file
22

33
import (
4+
"path/filepath"
45
"testing"
56
)
67

@@ -13,7 +14,7 @@ func Test_GetCurrentDirectory_1(t *testing.T) {
1314

1415
func Test_GetFileExt_1(t *testing.T) {
1516
fn := "/download/vagrant_1.9.2.dmg"
16-
fileExt := GetFileExt(fn)
17+
fileExt := filepath.Ext(fn)
1718
if len(fileExt) < 1 {
1819
t.Error("fileExt null!")
1920
} else {
@@ -23,7 +24,7 @@ func Test_GetFileExt_1(t *testing.T) {
2324

2425
func Test_GetFileExt_2(t *testing.T) {
2526
fn := "/download/vagrant_1"
26-
fileExt := GetFileExt(fn)
27+
fileExt := filepath.Ext(fn)
2728
if len(fileExt) < 1 {
2829
t.Error("fileExt null!")
2930
} else {

uploadfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package dotweb
22

33
import (
44
"errors"
5-
files "github.com/devfeel/dotweb/framework/file"
65
"io"
76
"mime/multipart"
87
"os"
8+
"path/filepath"
99
)
1010

1111
type UploadFile struct {
@@ -21,7 +21,7 @@ func NewUploadFile(file multipart.File, header *multipart.FileHeader) *UploadFil
2121
File: file,
2222
Header: header,
2323
fileName: header.Filename,
24-
fileExt: files.GetFileExt(header.Filename),
24+
fileExt: filepath.Ext(header.Filename), //update for issue #99
2525
}
2626
}
2727

version.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## dotweb版本记录:
22

3+
#### Version 1.4.3
4+
* 调整dotweb内部路由注册逻辑,New模式默认不开启,Classic模式默认开启,可通过app.UseDotwebRouter手动开启
5+
* 修复 issue #100, 解决特定场景下Exclude不生效问题
6+
* Use filepath.Ext to replace file.GetFileExt, update for issue #99
7+
* 移除 framework/file.GetFileExt 函数
8+
* 同步更新example代码
9+
* 2018-01-07 22:00
10+
311
#### Version 1.4.2
412
* Context新增QueryInt\QueryInt64接口,用于简化获取Int类型的Get参数,如果参数未传入或不是合法整形,返回0
513
* Context接口调整:除Write外,其他WriteXXX接口,返回值从(int, error)调整为error

0 commit comments

Comments
 (0)