diff --git a/README.md b/README.md index 4df9d5b..2ef7139 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ func main() { }) //begin server err := app.StartServer(80) - fmt.Println("dotweb.StartServer error => ", err) + fmt.Println("dotweb.StartServer error => ", err) } ``` diff --git a/router.go b/router.go index aac3403..75364d2 100644 --- a/router.go +++ b/router.go @@ -67,6 +67,7 @@ type ( Router interface { ServeHTTP(ctx *HttpContext) ServerFile(path string, fileRoot string) RouterNode + RegisterServerFile(routeMethod string, path string, fileRoot string) RouterNode GET(path string, handle HttpHandle) RouterNode HEAD(path string, handle HttpHandle) RouterNode OPTIONS(path string, handle HttpHandle) RouterNode @@ -426,7 +427,7 @@ func (r *router) WebSocket(path string, handle HttpHandle) { r.RegisterRoute(RouteMethod_WebSocket, path, handle) } -// shortcut for router.Handle(httpmethod, path, handle) +// RegisterRoute register router // support GET\POST\DELETE\PUT\HEAD\PATCH\OPTIONS\HiJack\WebSocket\ANY func (r *router) RegisterRoute(routeMethod string, path string, handle HttpHandle) RouterNode { realPath := r.server.VirtualPath() + path @@ -493,12 +494,18 @@ func (r *router) RegisterRoute(routeMethod string, path string, handle HttpHandl return node } -// ServerFile is a shortcut for router.ServeFiles(path, filepath) -// simple demo:server.ServerFile("/src/*", "/var/www") -// simple demo:server.ServerFile("/src/*filepath", "/var/www") -func (r *router) ServerFile(path string, fileroot string) RouterNode { +// ServerFile register ServerFile router with GET method on http.FileServer +// simple demo:router.ServerFile("/src/*", "/var/www") +// simple demo:router.ServerFile("/src/*filepath", "/var/www") +func (r *router) ServerFile(path string, fileRoot string) RouterNode { + return r.RegisterServerFile(RouteMethod_GET, path, fileRoot) +} + +// RegisterServerFile register ServerFile router with routeMethod method on http.FileServer +// simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*", "/var/www") +// simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*filepath", "/var/www") +func (r *router) RegisterServerFile(routeMethod string, path string, fileRoot string) RouterNode{ realPath := r.server.VirtualPath() + path - routeMethod := RouteMethod_GET node := &Node{} if len(realPath) < 2 { panic("path length must be greater than or equal to 2") @@ -510,13 +517,24 @@ func (r *router) ServerFile(path string, fileroot string) RouterNode { panic("path must end with /*filepath or /* in path '" + realPath + "'") } var root http.FileSystem - root = http.Dir(fileroot) + root = http.Dir(fileRoot) if !r.server.ServerConfig().EnabledListDir { root = &core.HideReaddirFS{root} } fileServer := http.FileServer(root) r.add(routeMethod, realPath, r.wrapFileHandle(fileServer)) node = r.getNode(routeMethod, realPath) + + if r.server.ServerConfig().EnabledAutoHEAD { + if !r.existsRouter(RouteMethod_HEAD, realPath){ + r.add(RouteMethod_HEAD, realPath, r.wrapFileHandle(fileServer)) + } + } + if r.server.ServerConfig().EnabledAutoOPTIONS { + if !r.existsRouter(RouteMethod_OPTIONS, realPath) { + r.add(RouteMethod_OPTIONS, realPath, r.wrapFileHandle(fileServer)) + } + } return node } diff --git a/server.go b/server.go index 420c9af..c94e81b 100644 --- a/server.go +++ b/server.go @@ -328,10 +328,16 @@ func (server *HttpServer) DELETE(path string, handle HttpHandle) RouterNode { return server.Router().DELETE(path, handle) } -// ServerFile is a shortcut for router.ServeFiles(path, filepath) +// ServerFile a shortcut for router.ServeFiles(path, fileRoot) // simple demo:server.ServerFile("/src/*filepath", "/var/www") -func (server *HttpServer) ServerFile(path string, fileroot string) RouterNode { - return server.Router().ServerFile(path, fileroot) +func (server *HttpServer) ServerFile(path string, fileRoot string) RouterNode { + return server.Router().ServerFile(path, fileRoot) +} + +// RegisterServerFile a shortcut for router.RegisterServerFile(routeMethod, path, fileRoot) +// simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*filepath", "/var/www") +func (server *HttpServer) RegisterServerFile(routeMethod string, path string, fileRoot string) RouterNode { + return server.Router().RegisterServerFile(routeMethod, path, fileRoot) } // HiJack is a shortcut for router.HiJack(path, handle) diff --git a/version.MD b/version.MD index 8af77f0..c55448d 100644 --- a/version.MD +++ b/version.MD @@ -1,5 +1,35 @@ ## dotweb版本记录: +#### Version 1.5.9.6 +* New Feature: HttpServer & Router add RegisterServerFile use to register ServerFile router with routeMethod method on http.FileServer +* Update: ServerFile add support for EnabledAutoHEAD\EnabledAutoOPTIONS +* Detail: + - when use ServerFile, default routeMethod is GET + - you can use RegisterServerFile specify other HttpMethod, like "POST" +* Example: +``` golang + server.RegisterServerFile(dotweb.RouteMethod_POST, "/dst/*", "/home/www/") +``` +* How To: + - how to add support "HEAD" method for all requests in your httpserver? + ~~~go + func main() { + app := dotweb.Classic("/home/logs/wwwroot/") + + // if use this, all router will auto add "HEAD" method support + // default is false + app.HttpServer.SetEnabledAutoHEAD(true) + + app.HttpServer.GET("/index", func(ctx dotweb.Context) error{ + return ctx.WriteString("welcome to my first web!") + }) + + err := app.StartServer(80) + fmt.Println("dotweb.StartServer error => ", err) + } + ~~~ +* 2019-01-03 10:00 + #### Version 1.5.9.5 * Fixed Bug: Request.IsAJAX check X-Requested-With Contains XMLHttpRequest * New Feature: Response support http2 Push