Skip to content

Commit

Permalink
Feature/download router (#15)
Browse files Browse the repository at this point in the history
* Add Download file helper in response and a express.Router instance based app.use()

* update readme
  • Loading branch information
DronRathore authored Jun 16, 2017
1 parent 85afadf commit fc5eac7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion express.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
42 changes: 33 additions & 9 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

0 comments on commit fc5eac7

Please sign in to comment.