Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

112 lines
3.9KB

  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. mw "github.com/goanywhere/rex/middleware"
  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. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  78. func Use(module func(http.Handler) http.Handler) {
  79. DefaultMux.Use(module)
  80. }
  81. func Run() {
  82. DefaultMux.Use(mw.Logger)
  83. DefaultMux.Run()
  84. }
  85. func init() {
  86. // setup project root
  87. var root = fs.Getcd(2)
  88. env.Set(internal.ROOT, root)
  89. env.Load(filepath.Join(root, ".env"))
  90. // cmd arguments
  91. flag.BoolVar(&config.debug, "debug", config.debug, "flag to toggle debug mode")
  92. flag.IntVar(&config.port, "port", config.port, "port to run the application server")
  93. flag.IntVar(&config.maxprocs, "maxprocs", config.maxprocs, "maximum cpu processes to run the server")
  94. flag.Parse()
  95. }