hooks.go 2.7 KB
Newer Older
A
astaxie 已提交
1 2 3
package beego

import (
F
fud 已提交
4
	"encoding/json"
A
astaxie 已提交
5
	"mime"
J
JessonChan 已提交
6
	"net/http"
F
fud 已提交
7
	"path/filepath"
J
JessonChan 已提交
8

J
JessonChan 已提交
9
	"github.com/astaxie/beego/context"
10
	"github.com/astaxie/beego/logs"
A
astaxie 已提交
11 12 13
	"github.com/astaxie/beego/session"
)

Y
Ying Zou 已提交
14
// register MIME type with content type
A
astaxie 已提交
15 16 17 18 19 20 21 22 23
func registerMime() error {
	for k, v := range mimemaps {
		mime.AddExtensionType(k, v)
	}
	return nil
}

// register default error http handlers, 404,401,403,500 and 503.
func registerDefaultErrorHandler() error {
F
fud 已提交
24
	m := map[string]func(http.ResponseWriter, *http.Request){
J
JessonChan 已提交
25 26 27 28 29 30 31 32 33 34
		"401": unauthorized,
		"402": paymentRequired,
		"403": forbidden,
		"404": notFound,
		"405": methodNotAllowed,
		"500": internalServerError,
		"501": notImplemented,
		"502": badGateway,
		"503": serviceUnavailable,
		"504": gatewayTimeout,
35 36
		"417": invalidxsrf,
		"422": missingxsrf,
C
Chenrui 已提交
37
		"413": payloadTooLarge,
F
fud 已提交
38 39
	}
	for e, h := range m {
J
JessonChan 已提交
40 41 42
		if _, ok := ErrorMaps[e]; !ok {
			ErrorHandler(e, h)
		}
A
astaxie 已提交
43 44 45 46 47
	}
	return nil
}

func registerSession() error {
A
astaxie 已提交
48
	if BConfig.WebConfig.Session.SessionOn {
A
astaxie 已提交
49 50
		var err error
		sessionConfig := AppConfig.String("sessionConfig")
M
maxin 已提交
51
		conf := new(session.ManagerConfig)
A
astaxie 已提交
52
		if sessionConfig == "" {
M
maxin 已提交
53 54 55 56 57 58
			conf.CookieName = BConfig.WebConfig.Session.SessionName
			conf.EnableSetCookie = BConfig.WebConfig.Session.SessionAutoSetCookie
			conf.Gclifetime = BConfig.WebConfig.Session.SessionGCMaxLifetime
			conf.Secure = BConfig.Listen.EnableHTTPS
			conf.CookieLifeTime = BConfig.WebConfig.Session.SessionCookieLifeTime
			conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig)
G
GrimTheReaper 已提交
59
			conf.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly
M
maxin 已提交
60
			conf.Domain = BConfig.WebConfig.Session.SessionDomain
A
astaxie 已提交
61 62 63
			conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
			conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
			conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
M
maxin 已提交
64 65
		} else {
			if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
F
fud 已提交
66 67
				return err
			}
A
astaxie 已提交
68
		}
M
maxin 已提交
69
		if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
A
astaxie 已提交
70 71 72 73 74 75 76 77
			return err
		}
		go GlobalSessions.GC()
	}
	return nil
}

func registerTemplate() error {
78
	defer lockViewPaths()
E
Eyal Post 已提交
79
	if err := AddViewPath(BConfig.WebConfig.ViewsPath); err != nil {
A
astaxie 已提交
80
		if BConfig.RunMode == DEV {
81
			logs.Warn(err)
A
astaxie 已提交
82
		}
A
astaxie 已提交
83
		return err
A
astaxie 已提交
84 85 86 87
	}
	return nil
}

A
astaxie 已提交
88
func registerAdmin() error {
A
astaxie 已提交
89
	if BConfig.Listen.EnableAdmin {
A
astaxie 已提交
90 91 92 93
		go beeAdminApp.Run()
	}
	return nil
}
J
JessonChan 已提交
94 95 96

func registerGzip() error {
	if BConfig.EnableGzip {
97 98 99 100 101
		context.InitGzip(
			AppConfig.DefaultInt("gzipMinLength", -1),
			AppConfig.DefaultInt("gzipCompressLevel", -1),
			AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
		)
J
JessonChan 已提交
102 103 104
	}
	return nil
}