github.com/astaxie/beego@v1.12.3/beego.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package beego 16 17 import ( 18 "os" 19 "path/filepath" 20 "strconv" 21 "strings" 22 ) 23 24 const ( 25 // VERSION represent beego web framework version. 26 VERSION = "1.12.3" 27 28 // DEV is for develop 29 DEV = "dev" 30 // PROD is for production 31 PROD = "prod" 32 ) 33 34 // M is Map shortcut 35 type M map[string]interface{} 36 37 // Hook function to run 38 type hookfunc func() error 39 40 var ( 41 hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc 42 ) 43 44 // AddAPPStartHook is used to register the hookfunc 45 // The hookfuncs will run in beego.Run() 46 // such as initiating session , starting middleware , building template, starting admin control and so on. 47 func AddAPPStartHook(hf ...hookfunc) { 48 hooks = append(hooks, hf...) 49 } 50 51 // Run beego application. 52 // beego.Run() default run on HttpPort 53 // beego.Run("localhost") 54 // beego.Run(":8089") 55 // beego.Run("127.0.0.1:8089") 56 func Run(params ...string) { 57 58 initBeforeHTTPRun() 59 60 if len(params) > 0 && params[0] != "" { 61 strs := strings.Split(params[0], ":") 62 if len(strs) > 0 && strs[0] != "" { 63 BConfig.Listen.HTTPAddr = strs[0] 64 } 65 if len(strs) > 1 && strs[1] != "" { 66 BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1]) 67 } 68 69 BConfig.Listen.Domains = params 70 } 71 72 BeeApp.Run() 73 } 74 75 // RunWithMiddleWares Run beego application with middlewares. 76 func RunWithMiddleWares(addr string, mws ...MiddleWare) { 77 initBeforeHTTPRun() 78 79 strs := strings.Split(addr, ":") 80 if len(strs) > 0 && strs[0] != "" { 81 BConfig.Listen.HTTPAddr = strs[0] 82 BConfig.Listen.Domains = []string{strs[0]} 83 } 84 if len(strs) > 1 && strs[1] != "" { 85 BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1]) 86 } 87 88 BeeApp.Run(mws...) 89 } 90 91 func initBeforeHTTPRun() { 92 //init hooks 93 AddAPPStartHook( 94 registerMime, 95 registerDefaultErrorHandler, 96 registerSession, 97 registerTemplate, 98 registerAdmin, 99 registerGzip, 100 ) 101 102 for _, hk := range hooks { 103 if err := hk(); err != nil { 104 panic(err) 105 } 106 } 107 } 108 109 // TestBeegoInit is for test package init 110 func TestBeegoInit(ap string) { 111 path := filepath.Join(ap, "conf", "app.conf") 112 os.Chdir(ap) 113 InitBeegoBeforeTest(path) 114 } 115 116 // InitBeegoBeforeTest is for test package init 117 func InitBeegoBeforeTest(appConfigPath string) { 118 if err := LoadAppConfig(appConfigProvider, appConfigPath); err != nil { 119 panic(err) 120 } 121 BConfig.RunMode = "test" 122 initBeforeHTTPRun() 123 }