beego学习笔记

beego

bee脚手架工具

go get github.com/beego/bee

bee创建项目

// 将下载好的二进制执行文件bee.exe路径添加到系统变量Path中
// F:\all_go_project\goproject\bin
bee new beegodemo01  // 创建项目,默认会在c盘C:\Users\Administrator\下创建,可以把目录复制到别的地方进行开发,默认已经用go mod管理了

测试运行

bee run  // 运行项目

目录介绍

├─conf  //配置文件目录
	-app.conf
├─controllers  //控制器目录,写逻辑代码的目录
	-default.go
├─models  //模型字段目录
├─routers // 路由url目录
	-router.go
├─static //静态资源目录
	├─css
	├─img
	├─js
├─tests // 测试目录
	-default_test.go
├─views // 模板目录
	-index.tpl

下载

// https://github.com/beego/beego/tree/v1.12.3
import (
	"github.com/astaxie/beego"  // 1.xx版本下载方式,2.x版本下载方式不同可参照github
)
>>>go run main.go

简单启动

package main
import (
	"github.com/astaxie/beego"
)
func main(){
    // 启动 服务
    beego.Run()//默认监听8080端口
}

调试模式

1.创建目录conf
2.在conf目录下创建app.conf
3.写入runmode=dev

路由

基本路由

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context" //使用的是beego里面的context而并非单独从context包
)

func main() {
	// 为根路径的Get方法绑定函数
	beego.Get("/", func(ctx *context.Context) {
        // Output就相当于reponse响应,Body函数里面传递字节切片
		ctx.Output.Body([]byte("hello world"))
	})
    
    // post请求
	beego.Post("/alice", func(ctx *context.Context) {
        // Output就相当于reponse响应
		ctx.Output.Body([]byte("this is post function"))
	})
    
    // Any请求,表示可以响应任何http请求
    beego.Any("/foo",func(ctx *context.Context){
     ctx.Output.Body([]byte("bar"))
	})

	//启动服务
	beego.Run()
}

正则路由

web.Router(“/api/?:id”, &controllers.RController{})
默认匹配 //例如对于URL”/api/123”可以匹配成功,此时变量”:id”值为”123”,URL”/api/“可正常匹配

web.Router(“/api/:id”, &controllers.RController{})
默认匹配 //例如对于URL”/api/123”可以匹配成功,此时变量”:id”值为”123”,但URL”/api/“匹配失败

web.Router(“/api/:id([0-9]+)“, &controllers.RController{})
自定义正则匹配 //例如对于URL”/api/123”可以匹配成功,此时变量”:id”值为”123”

web.Router(“/user/:username([\\w]+)“, &controllers.RController{})
正则字符串匹配 //例如对于URL”/user/astaxie”可以匹配成功,此时变量”:username”值为”astaxie”

web.Router(“/download/*.*”, &controllers.RController{})
*匹配方式 //例如对于URL”/download/file/api.xml”可以匹配成功,此时变量”:path”值为”file/api”, “:ext”值为”xml”        */

web.Router(“/download/ceshi/*“, &controllers.RController{})
*全匹配方式 //例如对于URL”/download/ceshi/file/api.json”可以匹配成功,此时变量”:splat”值为”file/api.json”               */

web.Router(“/:id:int”, &controllers.RController{})
int 类型设置方式,匹配 :id为int 类型,框架帮你实现了正则 ([0-9]+)

web.Router(“/:hi:string”, &controllers.RController{})
string 类型设置方式,匹配 :hi 为 string 类型。框架帮你实现了正则 ([\w]+)

web.Router(“/cms_:id([0-9]+).html”, &controllers.CmsController{})

带有前缀的自定义正则 //匹配 :id 为正则类型。匹配 cms_123.html 这样的 url :id = 123


// 示例
package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context" //使用的是beego里面的context而并非单独从context包
)

func main() {
    // Any请求,表示可以响应任何http请求
    // :id 匹配 123
    beego.Any("/foo/:id/",func(ctx *context.Context){
     ctx.Output.Body([]byte("this is id"))
	})

	//启动服务
	beego.Run()
}
>>> http://127.0.0.1:8080/foo/123/

获取路由参数

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context" //使用的是beego里面的context而并非单独从context包
)

func main() {
    // Any请求,表示可以响应任何http请求
    // :id 匹配 "123"
    beego.Any("/foo/:id/",func(ctx *context.Context){
        //获取路由参数
        fmt.Println(ctx.Input.Param(":id")) // "123"
        ctx.Output.Body([]byte("this is id"))
	})

	//启动服务
	beego.Run()
}

控制器

控制器介绍

// 基于 beego 的 Controller 设计,只需要匿名组合 beego.Controller 就可以了,如下所示:
package main
import (
	"github.com/astaxie/beego"
)
type xxxController struct{
    beego.Controller  // 嵌套beego的Controller,就拥有了beego.Controller的所有方法
}

控制器方法

package main
import (
	"github.com/astaxie/beego"
)
type HomeController struct{
    beego.Controller
}

// 上面我们嵌套了beego.Controller,所以我们就可以重写它里面的get等方法
func (c *HomeController) Get(){
    
}

路由控制器绑定

package main
import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)
type HomeController struct{
    beego.Controller
}

// 定义方法
func (c *HomeController) Get(){
    // 方式1不使用模板,直接返回字符串
    c.Ctx.WriteString("hello")
    // 返回字节切片
    c.Ctx.Output.Body([]byte("HomeController.Get"))
    // 方式2使用渲染模板,如果用户不设置该参数,那么默认会去到模板目录的 Controller/<方法名>.tpl 查找,例如上面的方法会去 homecontroller/get.tpl(文件、文件夹必须小写)
    c.TplName = "index.tpl"
}

func main(){
    // /home/ 路由地址
    // &HomeController{} 结构体实例
    beego.Router("/home/", &HomeController{})
}

路由参数控制器绑定

package main
import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)
type UserController struct{
    beego.Controller
}

func (c *UserController) Get(){
    c.Ctx.Output.Body([]byte(c.Ctx.Input.Param(":id")))
}

func main(){
    // /user/123/
    beego.Router("/user/:id/", &UserController{})
    // 正则路由需要用反引号
    beego.Router(`/muser/:id(\d+)/`, &UserController{})
}

自定义方法

// 上面列举的是默认的请求方法名(请求的 method 和函数名一致,例如 GET 请求执行 Get 函数,POST 请求执行 Post 函数),如果用户期望自定义函数名,那么可以使用如下方式
beego.Router("/",&IndexController{},"*:Index")  // *表示所有方法

// 示例
package main
import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)
type UserController struct{
    beego.Controller
}

// 当get请求时访问Detail方法
func (c *UserController) Detail(){
    c.Ctx.Output.Body([]byte("this is detail function"))
}
// get和post请求时访问ApiFunc方法
func (c *UserController) ApiFunc(){
    c.Ctx.Output.Body([]byte("this is ApiFunc function"))
}

func main(){
    // 参数3的位置表示指定请求方法对应的要执行的方法名
    beego.Router("/user/", &UserController{}, "get:Detail")
    
    // 多个请求方法指向同一个要执行的方法
    beego.Router("/user/", &UserController{},"get,post:ApiFunc")
	
    //不同的请求对应不同的方法,通过 ; 进行分割
    beego.Router("/api/food/",&RestController{}, "get:ListFood;post:CreateFood;put:UpdateFood;delete:DeleteFood")
}

//注意事项
如果你自定义了get请求对应的Detail方法以及post请求的Create方法,put和delete请求还使用原来的Put和Delete方法,那么在路由控制器中不仅要指定你自定义请求对应的方法,也需要将原来的方法隐射指定上,如下
beego.Router("/api/food/",&RestController{}, "get:Deatil;post:Create;put:Put;delete:Delete")

自动路由(跟请求方法无关,只跟url有关)

package main
import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)
type AuthController struct{
    beego.Controller
}

func (c *AuthController) Login(){
    c.Ctx.Output.Body([]byte("Login"))
}

func (c *AuthController) Logout(){
    c.Ctx.Output.Body([]byte("Logout"))
}

func main(){
    beego.AutoRouter(&AuthController{}) // 只需要传递一个参数,控制器结构体实例
}
// auth则是声明的控制器名称小写前缀,login则是对应的方法名小写
>>> http://127.0.0.1:8080/auth/login

请求数据处理

获取请求控制器和动作

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type RequestController struct{
    beego.Controller
}

func (c *RequestController) Header(){
    // 获取请求控制器和动作
    controllerName, actionName := c.GetControllerAndAction()
    fmt.Println(controllerName, actionName) // RequestController, Header
    c.Ctx.Output.Body([]byte("header"))
}

func main(){
    beego.AutoRouter(&RequestController{})
    beego.Run()
}

获取请求头信息

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type RequestController struct{
    beego.Controller
}

func (c *RequestController) Header(){
    ctx := c.Ctx
    input := ctx.Input
    //获取请求方法
    fmt.Println(input.Method())
    
    //请求协议。uri,url
    fmt.Println(input.Protocol(), input.URI(), input.URL())
    
    //获取头信息中的user-agent
    fmt.Println(input.Header("User-Agent"))
    
    c.Ctx.Output.Body([]byte("header"))
}

func main(){
    beego.AutoRouter(&RequestController{})
    beego.Run()
}

获取请求数据(查询字符串,body数据)

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type RequestController struct{
    beego.Controller
}

func (c *RequestController) Header(){
    ctx := c.Ctx
    input := ctx.Input
	
    // url查询字符串和body中数据都可以获取
    input.Query("id")
    
    c.Ctx.Output.Body([]byte("header"))
}

func main(){
    beego.AutoRouter(&RequestController{})
    beego.Run()
}

Controller获取请求数据

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type RequestController struct{
    beego.Controller
}

func (c *RequestController) Header(){
    // 获取id的数据
    fmt.Println(c.GetInt("id"))
    c.GetString("name")
	c.GetStrings("hobby")// 返回字符串数组切片
    c.GetBool("sex")// 返回bool和error
	c.GetFloat("money")// 返回float64和error
    c.Input() // 获取全部数据
    
    c.Ctx.Output.Body([]byte("header"))
}

func main(){
    beego.AutoRouter(&RequestController{})
    beego.Run()
}

解析到struct

//如果要把表单里的内容赋值到一个 struct 里,除了用上面的方法一个一个获取再赋值外,beego 提供了通过另外一个更便捷的方式,就是通过 struct 的字段名或 tag 与表单字段对应直接解析到 struct
package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type user struct {
    Id    int         `form:"-"`
    Name  interface{} `form:"username"`
    Age   int         `form:"age"`
    Email string
}

type LoginController struct{
    beego.Controller
}

/*html表单
名字:<input name="username" type="text" />
年龄:<input name="age" type="text" />
邮箱:<input name="Email" type="text" />
<input type="submit" value="提交" />
*/

func (c *LoginController) Post() {
    u := user{}
    if err := c.ParseForm(&u); err != nil {
        //handle error
        fmt.Println(error)
    }
    fmt.Println(u)
}

// 注意事项
1.StructTag form 的定义和 renderform方法 共用一个标签
2.定义 struct 时,字段名后如果有 form 这个 tag,则会以把 form 表单里的 name 和 tag 的名称一样的字段赋值给这个字段,否则就会把 form 表单里与字段名一样的表单内容赋值给这个字段。如上面例子中,会把表单中的 username 和 age 分别赋值给 user 里的 Name 和 Age 字段,而 Email 里的内容则会赋给 Email 这个字段。
3.调用 Controller ParseForm 这个方法的时候,传入的参数必须为一个 struct 的指针,否则对 struct 的赋值不会成功并返回 xx must be a struct pointer 的错误。
4.如果要忽略一个字段,有两种办法,一是:字段名小写开头,二是:form 标签的值设置为 -

获取requets body中数据

/*
1.在配置文件app.conf里设置 copyrequestbody = true
*/
package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type RequestController struct{
    beego.Controller
}

func (c *RequestController) Header(){
    input := ctx.Input
    fmt.Println(input.CopyBody(1024 * 1024))
    fmt.Println(string(input.RequestBody))
    
    c.Ctx.Output.Body([]byte("header"))
}

func main(){
    beego.AutoRouter(&RequestController{})
    beego.Run()
}

文件上传

func (c *FormController) Post() {
    f, h, err := c.GetFile("uploadname")
    if err != nil {
        log.Fatal("getfile err ", err)
    }
    defer f.Close()
    c.SaveToFile("uploadname", "static/upload/" + h.Filename) // 保存位置在 static/upload, 没有文件夹要先创建
    
}

响应数据

TplName

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type ReponseController struct{
    beego.Controller
}

func (c *ReponseController) Test(){
    //不指定默认会去找views/responsecontroller/test.tpl
    c.TplName = "responsecontroller/test.html"
}

func main(){
    beego.AutoRouter(&ReponseController{})
    beego.Run()
}

模板数据返回

package main

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type ReponseController struct{
    beego.Controller
}

func (c *ReponseController) Test(){
    // 模板数据返回
    c.Data["name"] = "kk"
    //不指定默认会去找views/responsecontroller/test.tpl
    c.TplName = "responsecontroller/test.html"
}

func main(){
    beego.AutoRouter(&ReponseController{})
    beego.Run()
}

返回json数据

//返回json数据,需要把数据放在结构体中
type User struct{
    //form表单解析的时候用的tag,json解析的时候用的tag
    Username string `form:"username" json:"username"`
    Password string `form:"password" json:"password"`
    Hobby []string `form:"hobby" json:"hobby"`
}
func (c *UserController) GetUser(){
    u := User{
        Username: "张三",
        Password: "123456",
        Hobby: []string{"1", "2"}
    }
    //返回json数据
    c.Data["json"] = u
    c.ServerJSON()
}

路由跳转redirect

//方式1
c.Redirect("/article/xml", 302)
return
//方式2
c.Ctx.Redirect(302, "/article/xml")
return

Session

配置开启

// 需要在配置文件中开启
sessionon = true|false

操作session

// 都是通过contorller对象来操作
// 设置
c.SetSession(name string, value interface{})
// 获取
c.GetSession(name string) interface{}
// 删除
c.DelSession(name string)
// 销毁
c.DestroySession()
posted @ 2022-01-16 22:07  我在路上回头看  阅读(179)  评论(0编辑  收藏  举报