hooks.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package beego
  2. import (
  3. "encoding/json"
  4. "mime"
  5. "net/http"
  6. "path/filepath"
  7. "github.com/astaxie/beego/context"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/astaxie/beego/session"
  10. )
  11. //
  12. func registerMime() error {
  13. for k, v := range mimemaps {
  14. mime.AddExtensionType(k, v)
  15. }
  16. return nil
  17. }
  18. // register default error http handlers, 404,401,403,500 and 503.
  19. func registerDefaultErrorHandler() error {
  20. m := map[string]func(http.ResponseWriter, *http.Request){
  21. "401": unauthorized,
  22. "402": paymentRequired,
  23. "403": forbidden,
  24. "404": notFound,
  25. "405": methodNotAllowed,
  26. "500": internalServerError,
  27. "501": notImplemented,
  28. "502": badGateway,
  29. "503": serviceUnavailable,
  30. "504": gatewayTimeout,
  31. }
  32. for e, h := range m {
  33. if _, ok := ErrorMaps[e]; !ok {
  34. ErrorHandler(e, h)
  35. }
  36. }
  37. return nil
  38. }
  39. func registerSession() error {
  40. if BConfig.WebConfig.Session.SessionOn {
  41. var err error
  42. sessionConfig := AppConfig.String("sessionConfig")
  43. conf := new(session.ManagerConfig)
  44. if sessionConfig == "" {
  45. conf.CookieName = BConfig.WebConfig.Session.SessionName
  46. conf.EnableSetCookie = BConfig.WebConfig.Session.SessionAutoSetCookie
  47. conf.Gclifetime = BConfig.WebConfig.Session.SessionGCMaxLifetime
  48. conf.Secure = BConfig.Listen.EnableHTTPS
  49. conf.CookieLifeTime = BConfig.WebConfig.Session.SessionCookieLifeTime
  50. conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig)
  51. conf.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly
  52. conf.Domain = BConfig.WebConfig.Session.SessionDomain
  53. conf.EnableSidInHttpHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
  54. conf.SessionNameInHttpHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
  55. conf.EnableSidInUrlQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
  56. } else {
  57. if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
  58. return err
  59. }
  60. }
  61. if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
  62. return err
  63. }
  64. go GlobalSessions.GC()
  65. }
  66. return nil
  67. }
  68. func registerTemplate() error {
  69. defer lockViewPaths()
  70. if err := AddViewPath(BConfig.WebConfig.ViewsPath); err != nil {
  71. if BConfig.RunMode == DEV {
  72. logs.Warn(err)
  73. }
  74. return err
  75. }
  76. return nil
  77. }
  78. func registerAdmin() error {
  79. if BConfig.Listen.EnableAdmin {
  80. go beeAdminApp.Run()
  81. }
  82. return nil
  83. }
  84. func registerGzip() error {
  85. if BConfig.EnableGzip {
  86. context.InitGzip(
  87. AppConfig.DefaultInt("gzipMinLength", -1),
  88. AppConfig.DefaultInt("gzipCompressLevel", -1),
  89. AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
  90. )
  91. }
  92. return nil
  93. }