hooks.go 2.5 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 14 15 16 17 18 19 20 21 22 23
	"github.com/astaxie/beego/session"
)

//
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,
F
fud 已提交
35 36
	}
	for e, h := range m {
J
JessonChan 已提交
37 38 39
		if _, ok := ErrorMaps[e]; !ok {
			ErrorHandler(e, h)
		}
A
astaxie 已提交
40 41 42 43 44
	}
	return nil
}

func registerSession() error {
A
astaxie 已提交
45
	if BConfig.WebConfig.Session.SessionOn {
A
astaxie 已提交
46 47
		var err error
		sessionConfig := AppConfig.String("sessionConfig")
M
maxin 已提交
48
		conf := new(session.ManagerConfig)
A
astaxie 已提交
49
		if sessionConfig == "" {
M
maxin 已提交
50 51 52 53 54 55 56 57 58 59 60 61
			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)
			conf.Domain = BConfig.WebConfig.Session.SessionDomain
			conf.EnableSidInHttpHeader = BConfig.WebConfig.Session.EnableSidInHttpHeader
			conf.SessionNameInHttpHeader = BConfig.WebConfig.Session.SessionNameInHttpHeader
			conf.EnableSidInUrlQuery = BConfig.WebConfig.Session.EnableSidInUrlQuery
		} else {
			if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
F
fud 已提交
62 63
				return err
			}
A
astaxie 已提交
64
		}
M
maxin 已提交
65
		if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
A
astaxie 已提交
66 67 68 69 70 71 72 73
			return err
		}
		go GlobalSessions.GC()
	}
	return nil
}

func registerTemplate() error {
A
astaxie 已提交
74 75
	if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
		if BConfig.RunMode == DEV {
76
			logs.Warn(err)
A
astaxie 已提交
77
		}
A
astaxie 已提交
78
		return err
A
astaxie 已提交
79 80 81 82
	}
	return nil
}

A
astaxie 已提交
83
func registerAdmin() error {
A
astaxie 已提交
84
	if BConfig.Listen.EnableAdmin {
A
astaxie 已提交
85 86 87 88
		go beeAdminApp.Run()
	}
	return nil
}
J
JessonChan 已提交
89 90 91

func registerGzip() error {
	if BConfig.EnableGzip {
92 93 94 95 96
		context.InitGzip(
			AppConfig.DefaultInt("gzipMinLength", -1),
			AppConfig.DefaultInt("gzipCompressLevel", -1),
			AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
		)
J
JessonChan 已提交
97 98 99
	}
	return nil
}