您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

119 行
4.0KB

  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/modules"
  30. "github.com/goanywhere/x/env"
  31. "github.com/goanywhere/x/fs"
  32. )
  33. var (
  34. DefaultMux = New()
  35. config = &options{
  36. debug: true,
  37. port: 5000,
  38. maxprocs: runtime.NumCPU(),
  39. }
  40. )
  41. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  42. // it also fetch the full function name of the handler (with package) to name the route.
  43. func Get(pattern string, handler interface{}) {
  44. DefaultMux.Get(pattern, handler)
  45. }
  46. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  47. // it also fetch the full function name of the handler (with package) to name the route.
  48. func Head(pattern string, handler interface{}) {
  49. DefaultMux.Head(pattern, handler)
  50. }
  51. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  52. // it also fetch the full function name of the handler (with package) to name the route.
  53. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  54. func Options(pattern string, handler interface{}) {
  55. DefaultMux.Options(pattern, handler)
  56. }
  57. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  58. // it also fetch the full function name of the handler (with package) to name the route.
  59. func Post(pattern string, handler interface{}) {
  60. DefaultMux.Post(pattern, handler)
  61. }
  62. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  63. // it also fetch the full function name of the handler (with package) to name the route.
  64. func Put(pattern string, handler interface{}) {
  65. DefaultMux.Put(pattern, handler)
  66. }
  67. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  68. // it also fetch the full function name of the handler (with package) to name the route.
  69. func Delete(pattern string, handler interface{}) {
  70. DefaultMux.Delete(pattern, handler)
  71. }
  72. // Group creates a new application group under the given path.
  73. func Group(path string) *Router {
  74. return DefaultMux.Group(path)
  75. }
  76. // FileServer registers a handler to serve HTTP requests
  77. // with the contents of the file system rooted at root.
  78. /*
  79. func FileServer(prefix, dir string) {
  80. DefaultMux.FileServer(prefix, dir)
  81. }
  82. */
  83. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  84. func Use(module func(http.Handler) http.Handler) {
  85. DefaultMux.Use(module)
  86. }
  87. func Run() {
  88. DefaultMux.Use(modules.Logger)
  89. DefaultMux.Run()
  90. }
  91. func init() {
  92. // setup project root
  93. var root = fs.Getcd(2)
  94. env.Set("rex.root", fs.Getcd(2))
  95. env.Load(filepath.Join(root, ".env"))
  96. // cmd arguments
  97. flag.BoolVar(&config.debug, "debug", config.debug, "flag to toggle debug mode")
  98. flag.IntVar(&config.port, "port", config.port, "port to run the application server")
  99. flag.IntVar(&config.maxprocs, "maxprocs", config.maxprocs, "maximum cpu processes to run the server")
  100. flag.Parse()
  101. }