Skip to content

Commit

Permalink
Merge pull request devfeel#101 from devfeel/develop
Browse files Browse the repository at this point in the history
Version 1.4.3 - BUG修复,优化代码
  • Loading branch information
devfeel authored Jan 7, 2018
2 parents 129df76 + 9348cd3 commit 34f6a50
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
23 changes: 17 additions & 6 deletions dotweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/devfeel/dotweb/logger"
"github.com/devfeel/dotweb/servers"
"github.com/devfeel/dotweb/session"
"reflect"
"sync"
)

Expand All @@ -35,6 +36,7 @@ type (
AppContext *core.ItemContext
middlewareMap map[string]MiddlewareFunc
middlewareMutex *sync.RWMutex
StartMode string
}

// ExceptionHandle 支持自定义异常处理代码能力
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -78,6 +87,7 @@ func New() *DotWeb {
// 3.print logo
func Classic() *DotWeb {
app := New()
app.StartMode = StartMode_Classic

app.SetEnabledLog(true)
app.UseRequestLog()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 11 additions & 10 deletions example/middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"))
Expand Down
14 changes: 0 additions & 14 deletions framework/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions framework/file/file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"path/filepath"
"testing"
)

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

func Test_GetFileExt_1(t *testing.T) {
fn := "/download/vagrant_1.9.2.dmg"
fileExt := GetFileExt(fn)
fileExt := filepath.Ext(fn)
if len(fileExt) < 1 {
t.Error("fileExt null!")
} else {
Expand All @@ -23,7 +24,7 @@ func Test_GetFileExt_1(t *testing.T) {

func Test_GetFileExt_2(t *testing.T) {
fn := "/download/vagrant_1"
fileExt := GetFileExt(fn)
fileExt := filepath.Ext(fn)
if len(fileExt) < 1 {
t.Error("fileExt null!")
} else {
Expand Down
4 changes: 2 additions & 2 deletions uploadfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package dotweb

import (
"errors"
files "github.com/devfeel/dotweb/framework/file"
"io"
"mime/multipart"
"os"
"path/filepath"
)

type UploadFile struct {
Expand All @@ -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
}
}

Expand Down
8 changes: 8 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 34f6a50

Please sign in to comment.