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 (
|
|
|
|
"net/http"
|
2014-04-03 07:07:20 +00:00
|
|
|
"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/session"
|
2012-12-17 05:53:03 +00:00
|
|
|
)
|
|
|
|
|
2013-12-20 11:36:54 +00:00
|
|
|
// beego web framework version.
|
2015-06-15 15:49:13 +00:00
|
|
|
const VERSION = "1.5.0"
|
2013-03-13 16:14:09 +00:00
|
|
|
|
2013-12-30 07:06:51 +00:00
|
|
|
type hookfunc func() error //hook function to run
|
|
|
|
var hooks []hookfunc //hook function slice to store the hookfunc
|
|
|
|
|
2013-12-20 11:36:54 +00:00
|
|
|
// Router adds a patterned controller handler to BeeApp.
|
|
|
|
// it's an alias method of App.Router.
|
2014-05-20 09:28:06 +00:00
|
|
|
// usage:
|
|
|
|
// simple router
|
|
|
|
// beego.Router("/admin", &admin.UserController{})
|
|
|
|
// beego.Router("/admin/index", &admin.ArticleController{})
|
|
|
|
//
|
|
|
|
// regex router
|
|
|
|
//
|
2014-06-08 12:24:01 +00:00
|
|
|
// beego.Router("/api/:id([0-9]+)", &controllers.RController{})
|
2014-05-20 09:28:06 +00:00
|
|
|
//
|
|
|
|
// custom rules
|
|
|
|
// beego.Router("/api/list",&RestController{},"*:ListFood")
|
|
|
|
// beego.Router("/api/create",&RestController{},"post:CreateFood")
|
|
|
|
// beego.Router("/api/update",&RestController{},"put:UpdateFood")
|
|
|
|
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
|
2013-07-25 07:50:16 +00:00
|
|
|
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
|
2013-07-08 10:35:10 +00:00
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:24:01 +00:00
|
|
|
// Router add list from
|
|
|
|
// usage:
|
|
|
|
// beego.Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
|
|
|
|
// type BankAccount struct{
|
|
|
|
// beego.Controller
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// register the function
|
|
|
|
// func (b *BankAccount)Mapping(){
|
|
|
|
// b.Mapping("ShowAccount" , b.ShowAccount)
|
|
|
|
// b.Mapping("ModifyAccount", b.ModifyAccount)
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
// //@router /account/:id [get]
|
|
|
|
// func (b *BankAccount) ShowAccount(){
|
|
|
|
// //logic
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// //@router /account/:id [post]
|
|
|
|
// func (b *BankAccount) ModifyAccount(){
|
|
|
|
// //logic
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// the comments @router url methodlist
|
|
|
|
// url support all the function Router's pattern
|
|
|
|
// methodlist [get post head put delete options *]
|
|
|
|
func Include(cList ...ControllerInterface) *App {
|
|
|
|
BeeApp.Handlers.Include(cList...)
|
|
|
|
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.
|
2014-05-20 09:28:06 +00:00
|
|
|
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
|
|
|
|
// visit the url /main/list to exec List function or /main/page to exec Page function.
|
2013-07-27 02:25:14 +00:00
|
|
|
func AutoRouter(c ControllerInterface) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.AddAuto(c)
|
2013-07-27 02:25:14 +00:00
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
2014-01-01 09:57:57 +00:00
|
|
|
// AutoPrefix adds controller handler to BeeApp with prefix.
|
|
|
|
// it's same to App.AutoRouterWithPrefix.
|
2014-05-20 09:28:06 +00:00
|
|
|
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
|
|
|
|
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
|
2014-01-01 09:57:57 +00:00
|
|
|
func AutoPrefix(prefix string, c ControllerInterface) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.AddAutoPrefix(prefix, c)
|
2014-01-01 09:57:57 +00:00
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// register router for Get method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Get("/", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Get(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Get(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for Post method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Post("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Post(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Post(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for Delete method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Delete("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Delete(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Delete(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for Put method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Put("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Put(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Put(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for Head method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Head("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Head(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Head(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for Options method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Options("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Options(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Options(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for Patch method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Patch("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Patch(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Patch(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for all method
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Any("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
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
|
|
|
func Any(rootpath string, f FilterFunc) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Any(rootpath, f)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// register router for own Handler
|
2014-05-20 10:47:41 +00:00
|
|
|
// usage:
|
|
|
|
// beego.Handler("/api", func(ctx *context.Context){
|
|
|
|
// ctx.Output.Body("hello world")
|
|
|
|
// })
|
2014-05-16 15:47:15 +00:00
|
|
|
func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
BeeApp.Handlers.Handler(rootpath, h, options...)
|
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
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
2014-05-20 09:28:06 +00:00
|
|
|
// SetViewsPath sets view directory path in beego application.
|
2013-04-14 15:20:38 +00:00
|
|
|
func SetViewsPath(path string) *App {
|
2014-05-20 09:28:06 +00:00
|
|
|
ViewsPath = path
|
2013-04-14 15:20:38 +00:00
|
|
|
return BeeApp
|
|
|
|
}
|
|
|
|
|
2014-05-20 09:28:06 +00:00
|
|
|
// SetStaticPath sets static directory path and proper url pattern in beego application.
|
|
|
|
// if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
|
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.
|
2013-06-25 10:49:08 +00:00
|
|
|
func DelStaticPath(url string) *App {
|
2014-09-20 10:56:46 +00:00
|
|
|
if !strings.HasPrefix(url, "/") {
|
|
|
|
url = "/" + url
|
|
|
|
}
|
|
|
|
url = strings.TrimRight(url, "/")
|
2013-06-25 10:49:08 +00:00
|
|
|
delete(StaticDir, url)
|
|
|
|
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
|
2014-10-07 20:35:30 +00:00
|
|
|
// beego.BeforeStatic, beego.BeforeRouter, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
|
|
|
|
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
|
|
|
|
func InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) *App {
|
|
|
|
BeeApp.Handlers.InsertFilter(pattern, pos, filter, params...)
|
2013-12-03 11:26:51 +00:00
|
|
|
return BeeApp
|
2013-11-25 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 07:06:51 +00:00
|
|
|
// 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.
|
2014-05-25 14:35:20 +00:00
|
|
|
// beego.Run() default run on HttpPort
|
|
|
|
// beego.Run(":8089")
|
|
|
|
// beego.Run("127.0.0.1:8089")
|
|
|
|
func Run(params ...string) {
|
|
|
|
if len(params) > 0 && params[0] != "" {
|
|
|
|
strs := strings.Split(params[0], ":")
|
|
|
|
if len(strs) > 0 && strs[0] != "" {
|
|
|
|
HttpAddr = strs[0]
|
|
|
|
}
|
|
|
|
if len(strs) > 1 && strs[1] != "" {
|
|
|
|
HttpPort, _ = strconv.Atoi(strs[1])
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 07:07:20 +00:00
|
|
|
initBeforeHttpRun()
|
|
|
|
|
|
|
|
if EnableAdmin {
|
2014-04-05 17:02:10 +00:00
|
|
|
go beeAdminApp.Run()
|
2014-04-03 07:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-20 10:21:17 +00:00
|
|
|
//init mime
|
|
|
|
AddAPPStartHook(initMime)
|
|
|
|
|
2013-12-30 07:06:51 +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 {
|
2014-01-11 06:28:11 +00:00
|
|
|
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-10-09 10:47:22 +00:00
|
|
|
`"providerConfig":"` + filepath.ToSlash(SessionSavePath) + `",` +
|
2014-05-20 07:30:17 +00:00
|
|
|
`"secure":` + strconv.FormatBool(EnableHttpTLS) + `,` +
|
2014-01-05 07:21:50 +00:00
|
|
|
`"enableSetCookie":` + strconv.FormatBool(SessionAutoSetCookie) + `,` +
|
2014-08-05 00:55:46 +00:00
|
|
|
`"domain":"` + SessionDomain + `",` +
|
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()
|
|
|
|
}
|
2015-08-06 01:36:43 +00:00
|
|
|
|
|
|
|
if AutoRender {
|
|
|
|
err := BuildTemplate(ViewsPath)
|
|
|
|
if err != nil {
|
|
|
|
if RunMode == "dev" {
|
|
|
|
Warn(err)
|
|
|
|
}
|
2013-04-11 03:07:32 +00:00
|
|
|
}
|
2013-03-21 13:55:54 +00:00
|
|
|
}
|
2013-09-11 09:00:39 +00:00
|
|
|
|
2015-02-26 15:34:43 +00:00
|
|
|
registerDefaultErrorHandler()
|
2014-06-10 17:11:32 +00:00
|
|
|
|
2014-06-16 08:05:15 +00:00
|
|
|
if EnableDocs {
|
2014-06-21 03:44:24 +00:00
|
|
|
Get("/docs", serverDocs)
|
2014-06-16 08:05:15 +00:00
|
|
|
Get("/docs/*", serverDocs)
|
|
|
|
}
|
2014-04-03 07:07:20 +00:00
|
|
|
}
|
2013-09-11 09:00:39 +00:00
|
|
|
|
2014-05-25 14:35:20 +00:00
|
|
|
// this function is for test package init
|
2014-04-03 07:07:20 +00:00
|
|
|
func TestBeegoInit(apppath string) {
|
|
|
|
AppPath = apppath
|
2015-06-14 10:10:10 +00:00
|
|
|
os.Setenv("BEEGO_RUNMODE", "test")
|
2014-04-03 07:07:20 +00:00
|
|
|
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
2014-04-04 02:04:22 +00:00
|
|
|
err := ParseConfig()
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
// for init if doesn't have app.conf will not panic
|
|
|
|
Info(err)
|
|
|
|
}
|
2014-04-03 07:07:20 +00:00
|
|
|
os.Chdir(AppPath)
|
|
|
|
initBeforeHttpRun()
|
2012-12-17 14:15:21 +00:00
|
|
|
}
|
2013-12-31 15:43:15 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
hooks = make([]hookfunc, 0)
|
|
|
|
}
|