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.

80 lines
2.4KB

  1. package rex
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "github.com/goanywhere/env"
  6. "github.com/goanywhere/fs"
  7. "github.com/goanywhere/rex/internal"
  8. . "github.com/goanywhere/rex/middleware"
  9. )
  10. var (
  11. Default = New()
  12. )
  13. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  14. // it also fetch the full function name of the handler (with package) to name the route.
  15. func Get(pattern string, handler interface{}) {
  16. Default.Get(pattern, handler)
  17. }
  18. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  19. // it also fetch the full function name of the handler (with package) to name the route.
  20. func Head(pattern string, handler interface{}) {
  21. Default.Head(pattern, handler)
  22. }
  23. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  24. // it also fetch the full function name of the handler (with package) to name the route.
  25. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  26. func Options(pattern string, handler interface{}) {
  27. Default.Options(pattern, handler)
  28. }
  29. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  30. // it also fetch the full function name of the handler (with package) to name the route.
  31. func Post(pattern string, handler interface{}) {
  32. Default.Post(pattern, handler)
  33. }
  34. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  35. // it also fetch the full function name of the handler (with package) to name the route.
  36. func Put(pattern string, handler interface{}) {
  37. Default.Put(pattern, handler)
  38. }
  39. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  40. // it also fetch the full function name of the handler (with package) to name the route.
  41. func Delete(pattern string, handler interface{}) {
  42. Default.Delete(pattern, handler)
  43. }
  44. // Group creates a new application group under the given path.
  45. func Group(path string) *Server {
  46. return Default.Group(path)
  47. }
  48. // FileServer registers a handler to serve HTTP (GET|HEAD) requests
  49. // with the contents of file system under the given directory.
  50. func FileServer(prefix, dir string) {
  51. Default.FileServer(prefix, dir)
  52. }
  53. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  54. func Use(module func(http.Handler) http.Handler) {
  55. Default.Use(module)
  56. }
  57. func Run() {
  58. Default.Use(Logger)
  59. Default.Run()
  60. }
  61. func init() {
  62. var root = fs.Getcd(2)
  63. env.Set(internal.ROOT, root)
  64. env.Load(filepath.Join(root, ".env"))
  65. }