https://github.com/yang-zzhong/go-httprouter
go http router
https://github.com/yang-zzhong/go-httprouter
go golang http router
Last synced: 3 months ago
JSON representation
go http router
- Host: GitHub
- URL: https://github.com/yang-zzhong/go-httprouter
- Owner: yang-zzhong
- Created: 2018-03-21T08:37:31.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-17T07:39:39.000Z (over 5 years ago)
- Last Synced: 2023-07-27T22:49:10.484Z (over 2 years ago)
- Topics: go, golang, http, router
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A Go HTTP Router
## Feature
1. Support group and middleware
2. With a static server that support front end route
3. Support restful params
```go
import (
"log"
"logic" // user app provide
"net/http"
hr "github.com/yang-zzhong/go-httprouter"
)
router := hr.NewRouter()
//
// config the try order, here we use the default order
// the router will first match the api, the pathfile based on docroot, the a configed entry file based on docroot
//
router.Tries = []string{hr.API, hr.PATHFILE, hr.ENTRYFILE}
// only match api
router.Tries = []string{hr.API}
// config docroot
router.DocRoot = "/srv/http/test"
// config api
router.Group("/api", []Mw{}, func(router *Router) {
router.OnGet("/users", usersList)
router.OnGet("/users/:user_id", user)
router.OnPost("/users", createUser)
router.Group("", []Mw{new(logic.Auth)}, func(router *Router) {
router.OnPut("/users/:user_id", updateUser)
});
})
router.OnGet("/hello-world", hello)
log.Fatal(http.ListenAndServe(":8080", router))
var userList HttpHandler = func(w *hr.Response, req *hr.Request) {
page, _ := req.FormInt("page")
pageSize, _ := req.FormatInt("page_size")
w.WithString(logic.UserList(page, pageSize).Json())
}
var user HttpHandler = func(w *hr.Response, req *hr.Request) {
w.WithString(logic.User(p.Get("user_id")).Json())
}
var createUser HttpHandler = func(w *hr.Response, req *hr.Request) {
params := map[string]interface{}{
"name": req.FormValue("name"),
"account": req.FormValue("account"),
"extra": req.FormMap("extra"),
}
if err := logic.CreateUser(params); err != nil {
panic(err)
}
w.WithString("创建成功")
}
var createUser HttpHandler = func(w *hr.Response, req *hr.Request) {
params := map[string]interface{}{
"name": req.FormValue("name"),
"account": req.FormValue("account"),
"extra": req.FormMap("extra"),
}
if err := logic.UpdateUser(req.Bag.Get("user_id"), params); err != nil {
panic(err)
}
w.WithString("更新成功")
}
var hello HttpHandler = func(w *hr.Response, _ *hr.Request) {
w.WithString("hello world!!!")
}
```