diff --git a/README.md b/README.md index 49da8d0..e399341 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,29 @@ func main (){ app.Start("8080") } ``` + +__Note__: You can also adhoc an ```express.Router()``` instance too much like it is done in expressjs +```go +package main +import ( + express "github.com/DronRathore/goexpress" + request "github.com/DronRathore/goexpress/request" + response "github.com/DronRathore/goexpress/response" +) +var LibRoutes = func (){ + // create a new Router instance which works in similar way as app.Get/Post etc + var LibRouter = express.Router() + LibRouter.Get("/lib/:api_version", func(req *request.Request, res *response.Response, next func()){ + res.Json(req.Params["api_version"]) + }) + return *LibRoutes +}() // immediate invocation +func main(){ + var app = express.Express() + app.Use(LibRoutes) // attaches the Library Routes + app.Start("8080") +} +``` ## Middleware You can write custom middlewares, wrappers in the similar fashion. Middlewares can be used to add websocket upgradation lib, session handling lib, static assets server handler ```go @@ -86,6 +109,15 @@ func main (){ app.Start("8080") } ``` +__Note__: You can now also send an auto downloadable file too using ```res.Download``` api +```go +/* + @params: + path: Full path to the file in local machine + filename: The name to be sent to the client +*/ +res.Download(path string, filename string) +``` ## Post Body ```go diff --git a/express.go b/express.go index c7b4bcf..064d3e5 100644 --- a/express.go +++ b/express.go @@ -121,11 +121,18 @@ func (e *express) Delete(url string, middleware router.Middleware) *express{ } // Extension to provide Router.Use functionality -func (e *express) Use(middleware router.Middleware) *express{ +func (e *express) Use(middleware interface{}) *express{ e.router.Use(middleware) return e } +// Returns a new instance of express Router +func Router() *router.Router { + var route *router.Router = &router.Router{} + route.Init() + return route; +} + // Sets global app properties that can be accessed under express struct func (e *express) SetProp(key string, value interface{}) *express{ e.properties[key] = value diff --git a/response/response.go b/response/response.go index affc8b6..23f33d4 100644 --- a/response/response.go +++ b/response/response.go @@ -222,6 +222,17 @@ func (res *Response) SendFile(url string, noCache bool) bool { <-channel return true } +// Send a download file to the client +func (res *Response) Download(path string, file_name string) bool { + if res.Header.CanSendHeader() == true { + res.Header.Set("Content-Disposition", "attachment; filename=\"" + file_name+ "\"") + return res.SendFile(path, false) + } else { + log.Print("Cannot Send header after being flushed") + res.End() + return false + } +} // Ends a response and drops the connection with client func (res *Response) End(){ diff --git a/router/router.go b/router/router.go index 9b87e9b..85de0c1 100644 --- a/router/router.go +++ b/router/router.go @@ -71,18 +71,42 @@ func (r* Router) Delete(url string, middleware Middleware) *Router{ r.addHandler("delete", false, compileRegex(url), middleware) return r } - -func (r* Router) Use(middleware Middleware) *Router{ - var regex = compileRegex("(.*)") - // A middleware is for all type of routes - r.addHandler("get", true, regex, middleware) - r.addHandler("post", true, regex, middleware) - r.addHandler("put", true, regex, middleware) - r.addHandler("patch", true, regex, middleware) - r.addHandler("delete", true, regex, middleware) +// Router.Use can take a function or a new express.Router() instance as argument +func (r* Router) Use(middleware interface{}) *Router{ + router, ok := middleware.(Router) + if ok { + r.useRouter(router) + } else { + mware, ok := middleware.(func(request *request.Request, response *response.Response, next func())) + if ok { + var regex = compileRegex("(.*)") + // A middleware is for all type of routes + r.addHandler("get", true, regex, mware) + r.addHandler("post", true, regex, mware) + r.addHandler("put", true, regex, mware) + r.addHandler("patch", true, regex, mware) + r.addHandler("delete", true, regex, mware) + } else { + panic("express.Router.Use can only take a function or a Router instance") + } + } return r } +func (r* Router) useRouter(router Router) *Router { + routes := router.getRoutes() + for route_type, list := range routes { + if r.routes[route_type] == nil { + r.routes[route_type] = []*Route{} + } + r.routes[route_type] = append(r.routes[route_type], list...) + } + return r; +} + +func (r* Router) getRoutes() map[string][]*Route { + return r.routes +} // Finds the suitable router for given url and method // It returns the middleware if found and a cursor index of array func (r* Router) FindNext(index int, method string, url string, request *request.Request) (Middleware, int, bool){