github.com/astaxie/beego@v1.12.3/hooks.go (about)

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