1
0
mirror of https://github.com/astaxie/beego.git synced 2025-01-12 12:47:13 +00:00
Beego/beego.go

133 lines
3.3 KiB
Go
Raw Normal View History

2014-08-18 16:41:43 +08:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 23:40:21 +08:00
//
2014-08-18 16:41:43 +08:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 23:40:21 +08:00
//
2014-08-18 16:41:43 +08:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 23:40:21 +08:00
//
2014-08-18 16:41:43 +08:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2012-12-17 13:53:03 +08:00
package beego
import (
"os"
2013-12-20 11:21:48 +08:00
"path/filepath"
2014-01-05 15:21:50 +08:00
"strconv"
2013-10-28 23:30:16 +08:00
"strings"
2012-12-17 13:53:03 +08:00
)
const (
2016-03-17 19:59:48 +08:00
// VERSION represent beego web framework version.
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
2020-06-30 20:54:24 +08:00
VERSION = "1.12.2"
2016-03-17 19:59:48 +08:00
// DEV is for develop
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
2016-01-06 11:48:23 +08:00
DEV = "dev"
2016-03-17 19:59:48 +08:00
// PROD is for production
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
PROD = "prod"
)
2013-03-14 00:14:09 +08:00
2018-11-09 17:54:20 +08:00
// M is Map shortcut
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
type M map[string]interface{}
// Hook function to run
2015-09-06 23:00:42 +08:00
type hookfunc func() error
2015-09-06 23:00:42 +08:00
var (
hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc
)
2015-09-07 21:38:53 +08:00
// AddAPPStartHook is used to register the hookfunc
// The hookfuncs will run in beego.Run()
2017-06-15 17:36:37 +08:00
// such as initiating session , starting middleware , building template, starting admin control and so on.
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
2017-06-15 17:36:37 +08:00
func AddAPPStartHook(hf ...hookfunc) {
hooks = append(hooks, hf...)
}
2013-12-20 19:36:54 +08:00
// Run beego application.
// beego.Run() default run on HttpPort
2015-09-19 05:52:52 +08:00
// beego.Run("localhost")
// beego.Run(":8089")
// beego.Run("127.0.0.1:8089")
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
func Run(params ...string) {
2016-03-17 19:09:38 +08:00
2015-09-07 21:38:53 +08:00
initBeforeHTTPRun()
2015-09-06 23:00:42 +08:00
2015-09-19 05:52:52 +08:00
if len(params) > 0 && params[0] != "" {
strs := strings.Split(params[0], ":")
if len(strs) > 0 && strs[0] != "" {
2015-12-09 23:35:04 +08:00
BConfig.Listen.HTTPAddr = strs[0]
2015-09-19 05:52:52 +08:00
}
if len(strs) > 1 && strs[1] != "" {
2015-12-09 23:35:04 +08:00
BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
2015-09-19 05:52:52 +08:00
}
2018-07-20 18:59:45 +02:00
BConfig.Listen.Domains = params
}
BeeApp.Run()
}
2017-11-25 19:16:37 +02:00
// RunWithMiddleWares Run beego application with middlewares.
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
func RunWithMiddleWares(addr string, mws ...MiddleWare) {
initBeforeHTTPRun()
strs := strings.Split(addr, ":")
if len(strs) > 0 && strs[0] != "" {
BConfig.Listen.HTTPAddr = strs[0]
2018-07-20 18:59:45 +02:00
BConfig.Listen.Domains = []string{strs[0]}
}
if len(strs) > 1 && strs[1] != "" {
BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
}
BeeApp.Run(mws...)
}
2015-09-07 21:38:53 +08:00
func initBeforeHTTPRun() {
2015-09-07 19:29:52 +08:00
//init hooks
2017-06-15 17:36:37 +08:00
AddAPPStartHook(
registerMime,
registerDefaultErrorHandler,
registerSession,
registerTemplate,
registerAdmin,
registerGzip,
)
2014-10-20 18:21:17 +08:00
for _, hk := range hooks {
2016-01-27 13:30:34 +08:00
if err := hk(); err != nil {
panic(err)
}
}
2015-09-07 19:18:04 +08:00
}
2015-09-07 19:27:53 +08:00
2015-09-07 21:38:53 +08:00
// TestBeegoInit is for test package init
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
2015-12-09 23:35:04 +08:00
func TestBeegoInit(ap string) {
path := filepath.Join(ap, "conf", "app.conf")
2015-12-09 23:35:04 +08:00
os.Chdir(ap)
InitBeegoBeforeTest(path)
}
// InitBeegoBeforeTest is for test package init
2020-08-05 21:57:20 +08:00
// Deprecated: using pkg/, we will delete this in v2.1.0
func InitBeegoBeforeTest(appConfigPath string) {
2016-04-27 22:26:32 +08:00
if err := LoadAppConfig(appConfigProvider, appConfigPath); err != nil {
panic(err)
}
BConfig.RunMode = "test"
2015-09-07 21:38:53 +08:00
initBeforeHTTPRun()
2015-09-07 19:27:53 +08:00
}