Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

90 lines
3.5KB

  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. "net/http"
  26. "github.com/goanywhere/rex/modules"
  27. )
  28. var DefaultMux = New()
  29. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  30. // it also fetch the full function name of the handler (with package) to name the route.
  31. func Get(pattern string, handler interface{}) {
  32. DefaultMux.register("GET", pattern, handler)
  33. }
  34. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  35. // it also fetch the full function name of the handler (with package) to name the route.
  36. func Head(pattern string, handler interface{}) {
  37. DefaultMux.register("HEAD", pattern, handler)
  38. }
  39. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  40. // it also fetch the full function name of the handler (with package) to name the route.
  41. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  42. func Options(pattern string, handler interface{}) {
  43. DefaultMux.register("OPTIONS", pattern, handler)
  44. }
  45. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  46. // it also fetch the full function name of the handler (with package) to name the route.
  47. func Post(pattern string, handler interface{}) {
  48. DefaultMux.register("POST", pattern, handler)
  49. }
  50. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  51. // it also fetch the full function name of the handler (with package) to name the route.
  52. func Put(pattern string, handler interface{}) {
  53. DefaultMux.register("PUT", pattern, handler)
  54. }
  55. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  56. // it also fetch the full function name of the handler (with package) to name the route.
  57. func Delete(pattern string, handler interface{}) {
  58. DefaultMux.register("Delete", pattern, handler)
  59. }
  60. // Group creates a new application group under the given path.
  61. func Group(path string) *Router {
  62. return DefaultMux.Group(path)
  63. }
  64. // FileServer registers a handler to serve HTTP requests
  65. // with the contents of the file system rooted at root.
  66. func FileServer(prefix, dir string) {
  67. DefaultMux.FileServer(prefix, dir)
  68. }
  69. // Use appends middleware module into the serving list, modules will be served in FIFO order.
  70. func Use(module func(http.Handler) http.Handler) {
  71. DefaultMux.Use(module)
  72. }
  73. func Run() {
  74. DefaultMux.Use(modules.Logger)
  75. DefaultMux.Run()
  76. }