1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-16 04:40:55 +00:00
Beego/beego.go

262 lines
6.8 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2012-12-17 05:53:03 +00:00
package beego
import (
"net/http"
"os"
2012-12-17 05:53:03 +00:00
"path"
2013-12-20 03:21:48 +00:00
"path/filepath"
2014-01-05 07:21:50 +00:00
"strconv"
2013-10-28 15:30:16 +00:00
"strings"
2013-12-03 11:26:51 +00:00
"github.com/astaxie/beego/middleware"
"github.com/astaxie/beego/session"
2012-12-17 05:53:03 +00:00
)
2013-12-20 11:36:54 +00:00
// beego web framework version.
2014-04-08 09:45:57 +00:00
const VERSION = "1.1.4"
2013-03-13 16:14:09 +00:00
type hookfunc func() error //hook function to run
var hooks []hookfunc //hook function slice to store the hookfunc
type groupRouter struct {
pattern string
controller ControllerInterface
mappingMethods string
}
// RouterGroups which will store routers
type GroupRouters []groupRouter
// Get a new GroupRouters
func NewGroupRouters() GroupRouters {
return make(GroupRouters, 0)
}
// Add Router in the GroupRouters
// it is for plugin or module to register router
func (gr *GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingMethod ...string) {
var newRG groupRouter
if len(mappingMethod) > 0 {
newRG = groupRouter{
pattern,
c,
mappingMethod[0],
}
} else {
newRG = groupRouter{
pattern,
c,
"",
}
}
*gr = append(*gr, newRG)
}
func (gr *GroupRouters) AddAuto(c ControllerInterface) {
2014-01-01 09:57:57 +00:00
newRG := groupRouter{
"",
c,
"",
}
*gr = append(*gr, newRG)
2014-01-01 09:57:57 +00:00
}
// AddGroupRouter with the prefix
// it will register the router in BeeApp
// the follow code is write in modules:
// GR:=NewGroupRouters()
// GR.AddRouter("/login",&UserController,"get:Login")
// GR.AddRouter("/logout",&UserController,"get:Logout")
// GR.AddRouter("/register",&UserController,"get:Reg")
// the follow code is write in app:
// import "github.com/beego/modules/auth"
// AddRouterGroup("/admin", auth.GR)
func AddGroupRouter(prefix string, groups GroupRouters) *App {
for _, v := range groups {
2014-01-01 09:57:57 +00:00
if v.pattern == "" {
BeeApp.AutoRouterWithPrefix(prefix, v.controller)
} else if v.mappingMethods != "" {
BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods)
} else {
BeeApp.Router(prefix+v.pattern, v.controller)
}
}
return BeeApp
}
2013-12-20 11:36:54 +00:00
// Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router.
2013-07-25 07:50:16 +00:00
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Router(rootpath, c, mappingMethods...)
2013-07-08 10:35:10 +00:00
return BeeApp
}
2013-12-20 11:36:54 +00:00
// RESTRouter adds a restful controller handler to BeeApp.
// its' controller implements beego.ControllerInterface and
// defines a param "pattern/:objectId" to visit each resource.
2013-07-08 10:35:10 +00:00
func RESTRouter(rootpath string, c ControllerInterface) *App {
Router(rootpath, c)
Router(path.Join(rootpath, ":objectId"), c)
2012-12-17 14:15:21 +00:00
return BeeApp
}
2013-12-20 11:36:54 +00:00
// AutoRouter adds defined controller handler to BeeApp.
// it's same to App.AutoRouter.
2013-07-27 02:25:14 +00:00
func AutoRouter(c ControllerInterface) *App {
BeeApp.AutoRouter(c)
return BeeApp
}
2014-01-01 09:57:57 +00:00
// AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix.
func AutoPrefix(prefix string, c ControllerInterface) *App {
BeeApp.AutoRouterWithPrefix(prefix, c)
return BeeApp
}
2013-12-20 11:36:54 +00:00
// ErrorHandler registers http.HandlerFunc to each http err code string.
// usage:
// beego.ErrorHandler("404",NotFound)
// beego.ErrorHandler("500",InternalServerError)
2013-05-06 16:17:25 +00:00
func Errorhandler(err string, h http.HandlerFunc) *App {
2013-09-11 09:00:39 +00:00
middleware.Errorhandler(err, h)
2013-05-06 16:17:25 +00:00
return BeeApp
}
2013-12-20 11:36:54 +00:00
// SetViewsPath sets view directory to BeeApp.
// it's alias of App.SetViewsPath.
2013-04-14 15:20:38 +00:00
func SetViewsPath(path string) *App {
BeeApp.SetViewsPath(path)
return BeeApp
}
2013-12-20 11:36:54 +00:00
// SetStaticPath sets static directory and url prefix to BeeApp.
// it's alias of App.SetStaticPath.
2013-04-14 15:20:38 +00:00
func SetStaticPath(url string, path string) *App {
2013-10-28 15:30:16 +00:00
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
2014-04-07 06:20:30 +00:00
url = strings.TrimRight(url, "/")
2013-04-14 15:20:38 +00:00
StaticDir[url] = path
return BeeApp
}
2013-12-20 11:36:54 +00:00
// DelStaticPath removes the static folder setting in this url pattern in beego application.
// it's alias of App.DelStaticPath.
func DelStaticPath(url string) *App {
delete(StaticDir, url)
return BeeApp
}
2013-12-20 11:36:54 +00:00
// [Deprecated] use InsertFilter.
// Filter adds a FilterFunc under pattern condition and named action.
// The actions contains BeforeRouter,AfterStatic,BeforeExec,AfterExec and FinishRouter.
// it's alias of App.Filter.
2013-09-09 16:00:11 +00:00
func AddFilter(pattern, action string, filter FilterFunc) *App {
BeeApp.Filter(pattern, action, filter)
2013-08-11 16:14:42 +00:00
return BeeApp
}
2013-12-20 11:36:54 +00:00
// InsertFilter adds a FilterFunc with pattern condition and action constant.
// The pos means action constant including
// beego.BeforeRouter, beego.AfterStatic, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
// it's alias of App.InsertFilter.
func InsertFilter(pattern string, pos int, filter FilterFunc) *App {
2013-12-03 11:26:51 +00:00
BeeApp.InsertFilter(pattern, pos, filter)
return BeeApp
}
// The hookfunc will run in beego.Run()
// such as sessionInit, middlerware start, buildtemplate, admin start
func AddAPPStartHook(hf hookfunc) {
hooks = append(hooks, hf)
}
2013-12-20 11:36:54 +00:00
// Run beego application.
// it's alias of App.Run.
2012-12-17 14:15:21 +00:00
func Run() {
initBeforeHttpRun()
if EnableAdmin {
2014-04-05 17:02:10 +00:00
go beeAdminApp.Run()
}
BeeApp.Run()
}
func initBeforeHttpRun() {
2013-12-04 15:53:36 +00:00
// if AppConfigPath not In the conf/app.conf reParse config
2013-12-20 03:21:48 +00:00
if AppConfigPath != filepath.Join(AppPath, "conf", "app.conf") {
2013-12-04 09:03:49 +00:00
err := ParseConfig()
2014-04-04 01:49:55 +00:00
if err != nil && AppConfigPath != filepath.Join(workPath, "conf", "app.conf") {
2013-12-04 15:53:36 +00:00
// configuration is critical to app, panic here if parse failed
panic(err)
2013-12-04 09:03:49 +00:00
}
}
2013-09-09 16:00:11 +00:00
// do hooks function
for _, hk := range hooks {
err := hk()
if err != nil {
panic(err)
}
}
2013-01-01 15:11:15 +00:00
if SessionOn {
var err error
2014-01-05 06:59:39 +00:00
sessionConfig := AppConfig.String("sessionConfig")
if sessionConfig == "" {
2014-01-09 13:37:50 +00:00
sessionConfig = `{"cookieName":"` + SessionName + `",` +
2014-01-05 07:21:50 +00:00
`"gclifetime":` + strconv.FormatInt(SessionGCMaxLifetime, 10) + `,` +
2014-01-09 13:37:50 +00:00
`"providerConfig":"` + SessionSavePath + `",` +
2014-01-05 07:21:50 +00:00
`"secure":` + strconv.FormatBool(HttpTLS) + `,` +
2014-01-09 13:37:50 +00:00
`"sessionIDHashFunc":"` + SessionHashFunc + `",` +
`"sessionIDHashKey":"` + SessionHashKey + `",` +
2014-01-05 07:21:50 +00:00
`"enableSetCookie":` + strconv.FormatBool(SessionAutoSetCookie) + `,` +
2014-01-09 13:37:50 +00:00
`"cookieLifeTime":` + strconv.Itoa(SessionCookieLifeTime) + `}`
2014-01-05 06:59:39 +00:00
}
2014-01-10 05:31:08 +00:00
GlobalSessions, err = session.NewManager(SessionProvider,
2014-01-05 06:59:39 +00:00
sessionConfig)
2014-01-09 13:37:50 +00:00
if err != nil {
panic(err)
}
2013-01-01 15:11:15 +00:00
go GlobalSessions.GC()
}
2013-09-09 16:00:11 +00:00
2013-09-19 15:40:55 +00:00
err := BuildTemplate(ViewsPath)
if err != nil {
if RunMode == "dev" {
Warn(err)
}
2013-03-21 13:55:54 +00:00
}
2013-09-11 09:00:39 +00:00
middleware.VERSION = VERSION
middleware.AppName = AppName
middleware.RegisterErrorHandler()
}
2013-09-11 09:00:39 +00:00
func TestBeegoInit(apppath string) {
AppPath = apppath
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
err := ParseConfig()
if err != nil && !os.IsNotExist(err) {
// for init if doesn't have app.conf will not panic
Info(err)
}
os.Chdir(AppPath)
initBeforeHttpRun()
2012-12-17 14:15:21 +00:00
}
func init() {
hooks = make([]hookfunc, 0)
//init mime
AddAPPStartHook(initMime)
}