Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
})
//begin server
err := app.StartServer(80)
fmt.Println("dotweb.StartServer error => ", err)
fmt.Println("dotweb.StartServer error => ", err)
}

```
Expand Down
32 changes: 25 additions & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand Down
12 changes: 9 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -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
Expand Down