Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

120 rindas
4.1KB

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