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.

124 lines
4.2KB

  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package rex
  24. import (
  25. "flag"
  26. "net/http"
  27. "path/filepath"
  28. "runtime"
  29. "github.com/goanywhere/rex/internal"
  30. "github.com/goanywhere/rex/modules"
  31. "github.com/goanywhere/x/env"
  32. "github.com/goanywhere/x/fs"
  33. )
  34. var (
  35. DefaultMux = New()
  36. options = struct {
  37. debug bool
  38. port int
  39. maxprocs int
  40. }{
  41. debug: true,
  42. port: 5000,
  43. maxprocs: runtime.NumCPU(),
  44. }
  45. )
  46. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  47. // it also fetch the full function name of the handler (with package) to name the route.
  48. func Get(pattern string, handler interface{}) {
  49. DefaultMux.register("GET", pattern, handler)
  50. }
  51. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  52. // it also fetch the full function name of the handler (with package) to name the route.
  53. func Head(pattern string, handler interface{}) {
  54. DefaultMux.register("HEAD", pattern, handler)
  55. }
  56. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  57. // it also fetch the full function name of the handler (with package) to name the route.
  58. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  59. func Options(pattern string, handler interface{}) {
  60. DefaultMux.register("OPTIONS", pattern, handler)
  61. }
  62. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  63. // it also fetch the full function name of the handler (with package) to name the route.
  64. func Post(pattern string, handler interface{}) {
  65. DefaultMux.register("POST", pattern, handler)
  66. }
  67. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  68. // it also fetch the full function name of the handler (with package) to name the route.
  69. func Put(pattern string, handler interface{}) {
  70. DefaultMux.register("PUT", pattern, handler)
  71. }
  72. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  73. // it also fetch the full function name of the handler (with package) to name the route.
  74. func Delete(pattern string, handler interface{}) {
  75. DefaultMux.register("Delete", pattern, handler)
  76. }
  77. // Group creates a new application group under the given path.
  78. func Group(path string) *Router {
  79. return DefaultMux.Group(path)
  80. }
  81. // FileServer registers a handler to serve HTTP requests
  82. // with the contents of the file system rooted at root.
  83. func FileServer(prefix, dir string) {
  84. DefaultMux.FileServer(prefix, dir)
  85. }
  86. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  87. func Use(module func(http.Handler) http.Handler) {
  88. DefaultMux.Use(module)
  89. }
  90. func Run() {
  91. DefaultMux.Use(modules.Logger)
  92. DefaultMux.Run()
  93. }
  94. func init() {
  95. // ----------------------------------------
  96. // Project Root
  97. // ----------------------------------------
  98. var root = fs.Getcd(2)
  99. env.Set(internal.Root, root)
  100. env.Load(filepath.Join(root, ".env"))
  101. // cmd arguments
  102. flag.BoolVar(&options.debug, "debug", options.debug, "flag to toggle debug mode")
  103. flag.IntVar(&options.port, "port", options.port, "port to run the application server")
  104. flag.IntVar(&options.maxprocs, "maxprocs", options.maxprocs, "maximum cpu processes to run the server")
  105. flag.Parse()
  106. }