2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00: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 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00: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 05:53:03 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
2014-04-03 07:07:20 +00:00
|
|
|
"os"
|
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"
|
2012-12-17 05:53:03 +00:00
|
|
|
)
|
|
|
|
|
2015-12-29 18:32:37 +00:00
|
|
|
const (
|
2016-03-17 11:59:48 +00:00
|
|
|
// VERSION represent beego web framework version.
|
2016-03-11 03:35:24 +00:00
|
|
|
VERSION = "1.6.1"
|
2015-12-29 18:32:37 +00:00
|
|
|
|
2016-03-17 11:59:48 +00:00
|
|
|
// DEV is for develop
|
2016-01-06 03:48:23 +00:00
|
|
|
DEV = "dev"
|
2016-03-17 11:59:48 +00:00
|
|
|
// PROD is for production
|
2015-12-29 18:32:37 +00:00
|
|
|
PROD = "prod"
|
|
|
|
)
|
2013-03-13 16:14:09 +00:00
|
|
|
|
2015-09-06 15:00:42 +00:00
|
|
|
//hook function to run
|
|
|
|
type hookfunc func() error
|
2013-12-30 07:06:51 +00:00
|
|
|
|
2015-09-06 15:00:42 +00:00
|
|
|
var (
|
|
|
|
hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc
|
|
|
|
)
|
beego: support more router
//design model
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Handler(router, http.Handler)
//example
beego.Get("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("Get userlist"))
})
beego.Post("/user", func(ctx *context.Context) {
ctx.Output.Body([]byte("add userlist"))
})
beego.Delete("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte([]byte(ctx.Input.Param(":id")))
})
import (
"http"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
)
func init() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(HelloService), "")
beego.Handler("/rpc", s)
}
2014-05-16 02:18:15 +00:00
|
|
|
|
2015-09-07 13:38:53 +00:00
|
|
|
// AddAPPStartHook is used to register the hookfunc
|
|
|
|
// The hookfuncs will run in beego.Run()
|
2013-12-30 07:06:51 +00:00
|
|
|
// 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.
|
2014-05-25 14:35:20 +00:00
|
|
|
// beego.Run() default run on HttpPort
|
2015-09-18 21:52:52 +00:00
|
|
|
// beego.Run("localhost")
|
2014-05-25 14:35:20 +00:00
|
|
|
// beego.Run(":8089")
|
|
|
|
// beego.Run("127.0.0.1:8089")
|
|
|
|
func Run(params ...string) {
|
2016-03-17 11:09:38 +00:00
|
|
|
|
2015-09-07 13:38:53 +00:00
|
|
|
initBeforeHTTPRun()
|
2015-09-06 15:00:42 +00:00
|
|
|
|
2015-09-18 21:52:52 +00:00
|
|
|
if len(params) > 0 && params[0] != "" {
|
|
|
|
strs := strings.Split(params[0], ":")
|
|
|
|
if len(strs) > 0 && strs[0] != "" {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.Listen.HTTPAddr = strs[0]
|
2015-09-18 21:52:52 +00:00
|
|
|
}
|
|
|
|
if len(strs) > 1 && strs[1] != "" {
|
2015-12-09 15:35:04 +00:00
|
|
|
BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
|
2015-09-18 21:52:52 +00:00
|
|
|
}
|
2014-05-25 14:35:20 +00:00
|
|
|
}
|
2014-04-03 07:07:20 +00:00
|
|
|
|
|
|
|
BeeApp.Run()
|
|
|
|
}
|
|
|
|
|
2015-09-07 13:38:53 +00:00
|
|
|
func initBeforeHTTPRun() {
|
2015-09-07 11:29:52 +00:00
|
|
|
//init hooks
|
2015-09-07 11:18:04 +00:00
|
|
|
AddAPPStartHook(registerMime)
|
|
|
|
AddAPPStartHook(registerDefaultErrorHandler)
|
|
|
|
AddAPPStartHook(registerSession)
|
|
|
|
AddAPPStartHook(registerTemplate)
|
2015-09-07 11:27:53 +00:00
|
|
|
AddAPPStartHook(registerAdmin)
|
2016-03-17 11:09:38 +00:00
|
|
|
AddAPPStartHook(registerGzip)
|
2014-10-20 10:21:17 +00:00
|
|
|
|
2013-12-30 07:06:51 +00:00
|
|
|
for _, hk := range hooks {
|
2016-01-27 05:30:34 +00:00
|
|
|
if err := hk(); err != nil {
|
2013-12-30 07:06:51 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 11:18:04 +00:00
|
|
|
}
|
2015-09-07 11:27:53 +00:00
|
|
|
|
2015-09-07 13:38:53 +00:00
|
|
|
// TestBeegoInit is for test package init
|
2015-12-09 15:35:04 +00:00
|
|
|
func TestBeegoInit(ap string) {
|
2016-01-27 08:43:01 +00:00
|
|
|
appConfigPath = filepath.Join(ap, "conf", "app.conf")
|
2015-12-09 15:35:04 +00:00
|
|
|
os.Chdir(ap)
|
2016-04-27 14:26:32 +00:00
|
|
|
if err := LoadAppConfig(appConfigProvider, appConfigPath); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
BConfig.RunMode = "test"
|
2015-09-07 13:38:53 +00:00
|
|
|
initBeforeHTTPRun()
|
2015-09-07 11:27:53 +00:00
|
|
|
}
|