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.

74 line
2.2KB

  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. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  49. func Use(module func(http.Handler) http.Handler) {
  50. Default.Use(module)
  51. }
  52. func Run() {
  53. Default.Use(Logger)
  54. Default.Run()
  55. }
  56. func init() {
  57. var root = fs.Getcd(2)
  58. env.Set(internal.ROOT, root)
  59. env.Load(filepath.Join(root, ".env"))
  60. }