Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

90 lines
2.7KB

  1. package rex
  2. import (
  3. "flag"
  4. "net/http"
  5. "path/filepath"
  6. "runtime"
  7. "github.com/goanywhere/rex/internal"
  8. mw "github.com/goanywhere/rex/middleware"
  9. "github.com/goanywhere/x/env"
  10. "github.com/goanywhere/x/fs"
  11. )
  12. var (
  13. DefaultMux = New()
  14. config = &options{
  15. debug: true,
  16. port: 5000,
  17. maxprocs: runtime.NumCPU(),
  18. }
  19. )
  20. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  21. // it also fetch the full function name of the handler (with package) to name the route.
  22. func Get(pattern string, handler interface{}) {
  23. DefaultMux.Get(pattern, handler)
  24. }
  25. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  26. // it also fetch the full function name of the handler (with package) to name the route.
  27. func Head(pattern string, handler interface{}) {
  28. DefaultMux.Head(pattern, handler)
  29. }
  30. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  31. // it also fetch the full function name of the handler (with package) to name the route.
  32. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  33. func Options(pattern string, handler interface{}) {
  34. DefaultMux.Options(pattern, handler)
  35. }
  36. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  37. // it also fetch the full function name of the handler (with package) to name the route.
  38. func Post(pattern string, handler interface{}) {
  39. DefaultMux.Post(pattern, handler)
  40. }
  41. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  42. // it also fetch the full function name of the handler (with package) to name the route.
  43. func Put(pattern string, handler interface{}) {
  44. DefaultMux.Put(pattern, handler)
  45. }
  46. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  47. // it also fetch the full function name of the handler (with package) to name the route.
  48. func Delete(pattern string, handler interface{}) {
  49. DefaultMux.Delete(pattern, handler)
  50. }
  51. // Group creates a new application group under the given path.
  52. func Group(path string) *Router {
  53. return DefaultMux.Group(path)
  54. }
  55. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  56. func Use(module func(http.Handler) http.Handler) {
  57. DefaultMux.Use(module)
  58. }
  59. func Run() {
  60. DefaultMux.Use(mw.Logger)
  61. DefaultMux.Run()
  62. }
  63. func init() {
  64. // setup project root
  65. var root = fs.Getcd(2)
  66. env.Set(internal.ROOT, root)
  67. env.Load(filepath.Join(root, ".env"))
  68. // cmd arguments
  69. flag.BoolVar(&config.debug, "debug", config.debug, "flag to toggle debug mode")
  70. flag.IntVar(&config.port, "port", config.port, "port to run the application server")
  71. flag.IntVar(&config.maxprocs, "maxprocs", config.maxprocs, "maximum cpu processes to run the server")
  72. flag.Parse()
  73. }