A simple router written in Go for APIs and servers of Eodomius projects
go get github.com/eodomius/routerYou can create a new router with the New function :
var router = router.New()You can handle a request with the supported methods :
-
First parameter is the path of the request
-
Second parameter is the function to call when the request is received
- First parameter is the responce
- Second parameter is the request
- Third parameter is a Result structure
router.Get("/test/{id}", func(w http.ResponseWriter, r *http.Request, result *Result){
w.Write([]byte("Get : Test ID : " + result.Params["id"]))
})
router.Post("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
w.Write([]byte("Post : Test"))
})
router.Patch("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
w.Write([]byte("Patch : Test"))
})
router.Put("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
w.Write([]byte("Put : Test"))
})
router.Delete("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
w.Write([]byte("Delete : Test"))
})You can add a middleware to the router with the Use function :
- This function takes a function as parameter :
- First parameter is the responce
- Second parameter is the request
router.Use(func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("Middleware"))
})Get: handle GET methodHead: handle HEAD methodConnect: handle CONNECT methodTrace: handle TRACE methodOptions: handle OPTIONS methodPost: handle POST methodPatch: handle PATCH methodPut: handle PUT methodDelete: handle DELETE method
Result is a structure that contains parameters.
type Result struct {
Params map[string]string // [paramName]paramValue
}