Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package wrouter
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "git.aiterp.net/gisle/wrouter/auth"
  8. )
  9. // Route is an interface for a request handler.
  10. type Route interface {
  11. Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool
  12. }
  13. // Router is the main structure of the wrouter package. It routes requests to the appropriate
  14. // Route
  15. type Router struct {
  16. paths map[Route]string
  17. routes []Route
  18. }
  19. // Mount a router to the router, prefixing all the paths of it
  20. func (router *Router) Mount(path string, subRouter *Router) {
  21. for _, route := range subRouter.routes {
  22. router.Route(strings.Replace(path+subRouter.paths[route], "//", "/", 1), route)
  23. }
  24. }
  25. // Route mounts a route interface to a path
  26. func (router *Router) Route(path string, route Route) {
  27. if router.paths == nil {
  28. router.paths = make(map[Route]string, 16)
  29. }
  30. router.paths[route] = path
  31. router.routes = append(router.routes, route)
  32. }
  33. // ServeHTTP serves a HTTP request using the router's routes
  34. func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  35. req.ParseForm()
  36. defer req.Body.Close()
  37. // Allow REST for clients of yore
  38. if req.Header.Get("X-Method") != "" {
  39. req.Method = strings.ToUpper(req.Header.Get("X-Method"))
  40. }
  41. // Resolve session cookies
  42. var user *auth.User
  43. var sess *auth.Session
  44. cookie, err := req.Cookie(auth.SessionCookieName)
  45. if cookie != nil && err == nil {
  46. sess = auth.FindSession(cookie.Value)
  47. if sess != nil {
  48. user, _ = auth.FindUser(sess.UserID)
  49. if user != nil {
  50. user.Session = sess
  51. }
  52. }
  53. }
  54. for index, route := range router.routes {
  55. path := router.paths[route]
  56. if strings.HasPrefix(strings.ToLower(req.URL.Path), path) {
  57. // Just so the handler can replace the path properly in case of case
  58. // insensitive clients getting fancy on it.
  59. path = req.URL.Path[:len(path)]
  60. // Attach a little something for testing
  61. w.Header().Set("X-Route-Path", path)
  62. w.Header().Set("X-Route-Index", fmt.Sprint(index))
  63. if route.Handle(path, w, req, user) {
  64. if user != nil && user.LoggedOut() {
  65. auth.CloseSession(sess.ID)
  66. }
  67. return
  68. }
  69. }
  70. }
  71. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  72. w.WriteHeader(404)
  73. w.Write([]byte("Not Found: " + req.URL.Path))
  74. }
  75. // Resource is a shorthand for creating and adding a new REST resource to the router
  76. func (router *Router) Resource(mount string, list, create ResourceFunc, get, update, delete ResourceIDFunc) {
  77. router.Route(mount, NewResource(list, create, get, update, delete))
  78. }
  79. // Static is a shorthand for creating a static file server on the following path
  80. func (router *Router) Static(mount string, filePath string) {
  81. router.Route(mount, NewStatic(filePath))
  82. }
  83. // Function creates a simple bare-bones handler that just runs a function, prevents
  84. // the need for dummy interfaces
  85. func (router *Router) Function(mount string, function FunctionHandlerFunc) {
  86. router.Route(mount, &functionHandler{function})
  87. }
  88. // Listen creates a http.Server with some sane defaults and pointing to this structure
  89. // for request handling.
  90. func (router *Router) Listen(host string, port int) (*http.Server, error) {
  91. srv := &http.Server{
  92. Addr: fmt.Sprintf("%s:%d", host, port),
  93. ReadTimeout: 5 * time.Second,
  94. WriteTimeout: 10 * time.Second,
  95. IdleTimeout: 120 * time.Second,
  96. Handler: router,
  97. }
  98. return srv, srv.ListenAndServe()
  99. }