func main() { glog.SetLogDirs(".") glog.SetLogToStderr(true) conf := controllers.Gconf beego.Get("/federation", func(ctx *context.Context) { federation(ctx, conf) }) beego.Get("/ripple.txt", func(ctx *context.Context) { f, err := os.Open("ripple.txt") if err != nil { glog.Fatal(err) } io.Copy(ctx.ResponseWriter, f) f.Close() }) beego.Get("/quote", func(ctx *context.Context) { u := "http://" + conf.Host + "/api/quote?" + ctx.Request.URL.RawQuery glog.Infoln(u) r, err := http.Get(u) if err != nil { glog.Errorln(err) return } io.Copy(ctx.ResponseWriter, r.Body) r.Body.Close() }) beego.Run() }
func ConfigRoutes() { beego.Router("/", &HomeController{}) beego.Get("/health", func(ctx *context.Context) { ctx.Output.Body([]byte("ok")) }) beego.Get("/version", func(ctx *context.Context) { ctx.Output.Body([]byte(g.VERSION)) }) }
func main() { beego.Get("books", func(ctx *context.Context) { ctx.Output.Body([]byte("hello world")) }) beego.Run() }
func init() { // config beego.BConfig.WebConfig.Session.SessionOn = true beego.BConfig.WebConfig.Session.SessionName = "SESSIONID" beego.BConfig.WebConfig.Session.SessionCookieLifeTime = ONE_DAYS beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600 beego.Router("/", &controllers.HomeController{}) beego.Router("/p/:page([0-9]+)", &controllers.HomeController{}) beego.Router("/cat/:cat([a-zA-Z]+)", &controllers.CategoryController{}) beego.Router("/cat/:cat([a-zA-Z]+)/p/:page([0-9]+)", &controllers.CategoryController{}) beego.Router("/tag/:tag([a-zA-Z0-9\u4e00-\u9fa5]+)", &controllers.TagController{}) beego.Router("/tag/:tag([a-zA-Z0-9\u4e00-\u9fa5]+)/p/:page([0-9]+)", &controllers.TagController{}) beego.Router("/:year([0-9]{4})/:month([0-9]{2})/:day([0-9]{2})/:id([0-9]+).html", &controllers.TopicController{}) beego.Router("/archives/:year([0-9]{4})/:month([0-9]{2})", &controllers.ArchivesController{}) beego.Router("/message", &controllers.MessageController{}) beego.Router("/about", &controllers.AboutController{}) beego.Router("/search", &controllers.SearchController{}) // admin beego.Router("/login", &background.AuthController{}) beego.InsertFilter("/admin/*", beego.BeforeRouter, FilterUser) beego.Router("/admin/user", &background.UserController{}) beego.Router("/admin/data", &background.DataController{}) beego.Router("/admin/topics", &background.TopicsController{}) beego.Router("/admin/category", &background.CategoryController{}) beego.Router("/admin/message", &background.MessageController{}) beego.Router("/admin/social", &background.SocialController{}) beego.Router("/admin/blogroll", &background.BlogrollController{}) beego.Router("/admin/ad", &background.ADController{}) beego.Router("/admin/sysconfig", &background.SysconfigController{}) beego.Router("/admin/databackup", &background.DataBackupRecover{}) beego.Router("/admin/datarecover", &background.DataBackupRecover{}) beego.Router("/admin/syslog", &background.SyslogController{}) beego.Router("/admin/trash", &background.TrashController{}) // proxy beego.Router("/proxy/:url(.*)", &proxy.ProxyController{}) // rss beego.Get("/feed", feed.Feed) beego.Get("/sitemap", feed.SiteMap) beego.Get("/robots.txt", feed.Robots) // 404 beego.ErrorHandler("404", HTTPNotFound) // redirect https beego.InsertFilter("*", beego.BeforeRouter, RedirectHttps) // plugin beego.Router("/plugin/useragent.html", &plugin.UserAgent{}) }
func main() { beego.Get("/petre", func(ctx *context.Context) { var a string var x *int a = string(*x) ctx.Output.Body([]byte("hello petre!!!")) ctx.Output.Body([]byte(a)) }) beego.Run() }
func main() { // Fixed Router beego.Get("/fixed", func(ctx *context.Context) { json := "Fixed Router" ctx.Output.Json(json, true, true) }) // Default matching //matching /para1/123 :id = 123 also matching /para1/ beego.Get("/para1/:id", func(ctx *context.Context) { para := ctx.Input.Param(":id") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // Default matching //matching /para2/123 :id = 123 doesn't match /para2/ beego.Get("/para2/:id!", func(ctx *context.Context) { para := ctx.Input.Param(":id") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // Full matching //matching /para3/path/to/123.html :all= path/to/123.html beego.Get("/para3-1/:all", func(ctx *context.Context) { para := ctx.Input.Param(":all") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // full matching //matching /para3-2/path/to/123.html :splat=path/to/123.html beego.Get("/para3-2/*", func(ctx *context.Context) { para := ctx.Input.Param(":splat") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // Regex matching //matching /regex1-1/123 beego.Get("/regex1-1/:id([0-9]+)", func(ctx *context.Context) { para := ctx.Input.Param(":id") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // int type, matching :id is int type, same as regex ([0-9]+) beego.Get("/regex1-2/:id:int", func(ctx *context.Context) { para := ctx.Input.Param(":id") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // Regex string matching //matching /regex2-1/astaxie :username = astaxie beego.Get(`/regex2-1/:username([\w]+)`, func(ctx *context.Context) { para := ctx.Input.Param(":username") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // string type, matching :hi is string type。same as ([\w]+) beego.Get("/regex2-2/:username:string", func(ctx *context.Context) { para := ctx.Input.Param(":username") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) // matching //matching /regex3/file/api.xml :path= file/api :ext=xml // ** The issue I was facing in the video has been fixed in `beego` develop branch ** // ** https://github.com/astaxie/beego/commit/3f7e91e6a40edc57596d4d6aa18fb7be1e0cbabb ** beego.Get("/regex3/*.*", func(ctx *context.Context) { path := ctx.Input.Param(":path") ext := ctx.Input.Param(":ext") json := map[string]string{ "path": path, "ext": ext, } ctx.Output.Json(json, true, true) }) // custom regex with prefix //matching :id is regex type, matching pre_123.html, :id = 123 beego.Get("/pre_:id([0-9]+).html", func(ctx *context.Context) { para := ctx.Input.Param(":id") json := map[string]string{ "para": para, } ctx.Output.Json(json, true, true) }) beego.Run() }
func init() { beego.Get("/meta/healthcheck", func(ctx *context.Context) { ctx.ResponseWriter.WriteHeader(200) }) beego.Include(&controllers.MagicController{}) }