package main import ( "github.com/astaxie/beego" "github.com/astaxie/beego/context" ) type MainController struct { beego.Controller } func (c *MainController) Logout() { // TODO 退出逻辑 c.Ctx.Output.Body([]byte("飘云 退出成功!!!")) } func (c *MainController) Login() { // TODO 登录逻辑 c.Ctx.Output.Body([]byte("飘云 登录成功!!!")) } func init() { ns_api_v1 := beego.NewNamespace("api/v1", beego.NSCond(func(ctx *context.Context) bool { if ctx.Input.Domain() == "localhost" { return true } // 输出一个友好提示!! ctx.Output.Body([]byte("域名被限制!")) return false }), beego.NSRouter("/login", &MainController{}, "get:Login"), beego.NSRouter("/logout", &MainController{}, "get:Logout"), ) ns_api_v2 := beego.NewNamespace("api/v2", beego.NSCond(func(ctx *context.Context) bool { if ctx.Input.Domain() == "127.0.0.1" { return true } // 输出一个友好提示!! ctx.Output.Body([]byte("域名被限制!")) return false }), beego.NSRouter("/login", &MainController{}, "get:Login"), beego.NSRouter("/logout", &MainController{}, "get:Logout"), ) ns_api_v3 := beego.NewNamespace("api/v3", beego.NSCond(func(ctx *context.Context) bool { if ctx.Input.Domain() == "www.chinapyg.com" { return true } // 输出一个友好提示!! ctx.Output.Body([]byte("域名被限制!请从www.chinapyg.com登录")) return false }), beego.NSRouter("/login", &MainController{}, "get:Login"), beego.NSRouter("/logout", &MainController{}, "get:Logout"), ) beego.AddNamespace(ns_api_v1, ns_api_v2, ns_api_v3) } func main() { beego.Run(":8989") }
发表评论