hooks.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package beego
  2. import (
  3. "encoding/json"
  4. "mime"
  5. "net/http"
  6. "path/filepath"
  7. "github.com/cnlh/nps/vender/github.com/astaxie/beego/context"
  8. "github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
  9. "github.com/cnlh/nps/vender/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. "417": invalidxsrf,
  32. "422": missingxsrf,
  33. }
  34. for e, h := range m {
  35. if _, ok := ErrorMaps[e]; !ok {
  36. ErrorHandler(e, h)
  37. }
  38. }
  39. return nil
  40. }
  41. func registerSession() error {
  42. if BConfig.WebConfig.Session.SessionOn {
  43. var err error
  44. sessionConfig := AppConfig.String("sessionConfig")
  45. conf := new(session.ManagerConfig)
  46. if sessionConfig == "" {
  47. conf.CookieName = BConfig.WebConfig.Session.SessionName
  48. conf.EnableSetCookie = BConfig.WebConfig.Session.SessionAutoSetCookie
  49. conf.Gclifetime = BConfig.WebConfig.Session.SessionGCMaxLifetime
  50. conf.Secure = BConfig.Listen.EnableHTTPS
  51. conf.CookieLifeTime = BConfig.WebConfig.Session.SessionCookieLifeTime
  52. conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig)
  53. conf.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly
  54. conf.Domain = BConfig.WebConfig.Session.SessionDomain
  55. conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
  56. conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
  57. conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
  58. } else {
  59. if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
  60. return err
  61. }
  62. }
  63. if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
  64. return err
  65. }
  66. go GlobalSessions.GC()
  67. }
  68. return nil
  69. }
  70. func registerTemplate() error {
  71. defer lockViewPaths()
  72. if err := AddViewPath(BConfig.WebConfig.ViewsPath); err != nil {
  73. if BConfig.RunMode == DEV {
  74. logs.Warn(err)
  75. }
  76. return err
  77. }
  78. return nil
  79. }
  80. func registerAdmin() error {
  81. if BConfig.Listen.EnableAdmin {
  82. go beeAdminApp.Run()
  83. }
  84. return nil
  85. }
  86. func registerGzip() error {
  87. if BConfig.EnableGzip {
  88. context.InitGzip(
  89. AppConfig.DefaultInt("gzipMinLength", -1),
  90. AppConfig.DefaultInt("gzipCompressLevel", -1),
  91. AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
  92. )
  93. }
  94. return nil
  95. }