router.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package util
  2. import (
  3. "net/http"
  4. "runtime"
  5. "git.chuangxin1.com/cx/util/httprouter"
  6. )
  7. // Router router
  8. type Router = httprouter.Router
  9. // Handle is a function that can be registered to a route to handle HTTP
  10. // requests. Like http.HandlerFunc.
  11. type Handle func(http.ResponseWriter, *http.Request)
  12. // NewRouter new http router with default route
  13. func NewRouter() *Router {
  14. router := httprouter.New()
  15. router.NotFound = http.HandlerFunc(notFoundHandler)
  16. router.PanicHandler = panicHandler
  17. router.MethodNotAllowed = http.HandlerFunc(notAllowedHandler)
  18. router.GET("/api/ver", defaultHandler)
  19. //router.GET("/api/health", defaultHandler)
  20. //router.Handler("GET", "/metrics", promhttp.Handler())
  21. return router
  22. }
  23. func defaultHandler(w http.ResponseWriter, r *http.Request) {
  24. ver := map[string]string{
  25. "version": LibVersion,
  26. "name": LibName}
  27. WriteJSON(w, RowReplyData(ver))
  28. }
  29. func panicHandler(w http.ResponseWriter, r *http.Request, err interface{}) {
  30. e := err.(runtime.Error)
  31. WriteJSON(w, ErrReplyData(ErrException, e.Error()))
  32. }
  33. func notFoundHandler(w http.ResponseWriter, req *http.Request) {
  34. WriteJSON(w, ErrReplyData(ErrNotFound, `NotFound`))
  35. }
  36. func notAllowedHandler(w http.ResponseWriter, req *http.Request) {
  37. WriteJSON(w, ErrReplyData(ErrNotAllowed, `Method Not Allowed`))
  38. }
  39. // AddHandler add handler to router
  40. func AddHandler(router *httprouter.Router, method, route string, handler http.Handler) {
  41. router.Handler(method, route, handler)
  42. }
  43. // AddGetHandle add handle to router
  44. func AddGetHandle(router *httprouter.Router, route string, handle Handle) {
  45. router.GET(route, httprouter.Handle(handle))
  46. }
  47. // AddPostHandle add handle to router
  48. func AddPostHandle(router *httprouter.Router, route string, handle Handle) {
  49. router.POST(route, httprouter.Handle(handle))
  50. }
  51. // AddPutHandle add handle to router
  52. func AddPutHandle(router *httprouter.Router, route string, handle Handle) {
  53. router.PUT(route, httprouter.Handle(handle))
  54. }
  55. // AddHeadHandle add handle to router
  56. func AddHeadHandle(router *httprouter.Router, route string, handle Handle) {
  57. router.HEAD(route, httprouter.Handle(handle))
  58. }
  59. // AddDeleteHandle add handle to router
  60. func AddDeleteHandle(router *httprouter.Router, route string, handle Handle) {
  61. router.DELETE(route, httprouter.Handle(handle))
  62. }
  63. // AddPatchHandle add handle to router
  64. func AddPatchHandle(router *httprouter.Router, route string, handle Handle) {
  65. router.PATCH(route, httprouter.Handle(handle))
  66. }
  67. // AddOptionsHandle add handle to router
  68. func AddOptionsHandle(router *httprouter.Router, route string, handle Handle) {
  69. router.OPTIONS(route, httprouter.Handle(handle))
  70. }