hooks.go 2.1 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

A
astaxie 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21
	"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 已提交
22
	m := map[string]func(http.ResponseWriter, *http.Request){
J
JessonChan 已提交
23 24 25 26 27 28 29 30 31 32
		"401": unauthorized,
		"402": paymentRequired,
		"403": forbidden,
		"404": notFound,
		"405": methodNotAllowed,
		"500": internalServerError,
		"501": notImplemented,
		"502": badGateway,
		"503": serviceUnavailable,
		"504": gatewayTimeout,
F
fud 已提交
33 34
	}
	for e, h := range m {
J
JessonChan 已提交
35 36 37
		if _, ok := ErrorMaps[e]; !ok {
			ErrorHandler(e, h)
		}
A
astaxie 已提交
38 39 40 41 42
	}
	return nil
}

func registerSession() error {
A
astaxie 已提交
43
	if BConfig.WebConfig.Session.SessionOn {
A
astaxie 已提交
44 45 46
		var err error
		sessionConfig := AppConfig.String("sessionConfig")
		if sessionConfig == "" {
F
fud 已提交
47 48 49 50
			conf := map[string]interface{}{
				"cookieName":      BConfig.WebConfig.Session.SessionName,
				"gclifetime":      BConfig.WebConfig.Session.SessionGCMaxLifetime,
				"providerConfig":  filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig),
A
astaxie 已提交
51
				"secure":          BConfig.Listen.EnableHTTPS,
F
fud 已提交
52 53 54 55 56 57 58 59 60
				"enableSetCookie": BConfig.WebConfig.Session.SessionAutoSetCookie,
				"domain":          BConfig.WebConfig.Session.SessionDomain,
				"cookieLifeTime":  BConfig.WebConfig.Session.SessionCookieLifeTime,
			}
			confBytes, err := json.Marshal(conf)
			if err != nil {
				return err
			}
			sessionConfig = string(confBytes)
A
astaxie 已提交
61
		}
F
fud 已提交
62
		if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); err != nil {
A
astaxie 已提交
63 64 65 66 67 68 69 70
			return err
		}
		go GlobalSessions.GC()
	}
	return nil
}

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

func registerDocs() error {
A
astaxie 已提交
81
	if BConfig.WebConfig.EnableDocs {
A
astaxie 已提交
82 83 84 85 86
		Get("/docs", serverDocs)
		Get("/docs/*", serverDocs)
	}
	return nil
}
A
astaxie 已提交
87 88

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