89、Beego框架(上)——2020年07⽉30⽇22:05:40 89、Beego框架(上)
2020年07⽉29⽇14:46:59
![image-20200729150123094](/Users/apple/Library/Application Support/typora-user-images/image-20200729150123094.png) 1、go get配置
配置使得下载包快⼀点——国内源
export GO111MODULE=on
export GOPROXY=goproxy.io
2、下载beego和bee
下载包
go get -u github/astaxie/beego
go get -u github/beego/bee
✘⚙  ~/go/bin  export GO111MODULE=on
⚙  ~/go/bin  export GOPROXY=goproxy.io
⚙  ~/go/bin  go get -u github/astaxie/beego
go: finding github/astaxie/beego v1.12.2
go: downloading github/astaxie/beego v1.12.2
go: extracting github/astaxie/beego v1.12.2
go: downloading github/prometheus/client_golang v1.7.0
go: /x/crypto v0.0.0-20191011191535-87dc89f01550
go: downloading github/hashicorp/golang-lru v0.5.4
go: downloading github/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644
go: downloading gopkg.in/yaml.v2 v2.2.8
go: extracting github/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644
go: extracting github/hashicorp/golang-lru v0.5.4
go: extracting gopkg.in/yaml.v2 v2.2.8
go: extracting github/prometheus/client_golang v1.7.0
go: downloading github/prometheus/common v0.10.0
go: downloading github/prometheus/client_model v0.2.0
go: downloading github/golang/protobuf v1.4.2
go: downloading github/prometheus/procfs v0.1.3
go: downloading github/beorn7/perks v1.0.1
go: downloading github/cespare/xxhash/v2 v2.1.1
go: /x/sys v0.0.0-20200615200032-f1bc736245b1
go: extracting github/prometheus/client_model v0.2.0
go: extracting github/prometheus/common v0.10.0
go: downloading github/matttproud/golang_protobuf_extensions v1.0.1
go: extracting github/beorn7/perks v1.0.1
go: extracting github/cespare/xxhash/v2 v2.1.1
go: extracting github/golang/protobuf v1.4.2
go: /protobuf v1.23.0
go: extracting github/matttproud/golang_protobuf_extensions v1.0.1
go: extracting github/prometheus/procfs v0.1.3
go: /x/crypto v0.0.0-20191011191535-87dc89f01550
go: /protobuf v1.23.0
go: /x/net v0.0.0-20190620200207-3b0461eec859
go: /x/sys v0.0.0-20200615200032-f1bc736245b1
go: /x/net v0.0.0-20190620200207-3b0461eec859
go: /x/text v0.3.0
go: /x/text v0.3.0
go: finding github/beorn7/perks v1.0.1
go: finding github/prometheus/client_model v0.2.0
go: /x/sys latest
go: finding github/hashicorp/golang-lru v0.5.4
go: /x/net latest
go: finding github/golang/protobuf v1.4.2
go: finding github/prometheus/common v0.10.0
go: finding github/prometheus/procfs v0.1.3
go: finding gopkg.in/yaml.v2 v2.3.0
go: /x/crypto latest
go: finding github/prometheus/client_golang v1.7.1
go: finding github/shiena/ansicolor latest
go: finding github/cespare/xxhash/v2 v2.1.1
go: /protobuf v1.25.0
go: finding github/matttproud/golang_protobuf_extensions v1.0.1
go: /x/text v0.3.3
go: downloading github/prometheus/client_golang v1.7.1
go: /x/crypto v0.0.0-20200728195943-123391ffb6de go: downloading gopkg.in/yaml.v2 v2.3.0
go: extracting gopkg.in/yaml.v2 v2.3.0
go: extracting github/prometheus/client_golang v1.7.1
go: /protobuf v1.25.0
go: /protobuf v1.25.0
go: /x/sys v0.0.0-20200728102440-3e129f6d46b1
go: /x/crypto v0.0.0-20200728195943-123391ffb6de
go: /x/net v0.0.0-20200707034311-ab3426394381
go: /x/sys v0.0.0-20200728102440-3e129f6d46b1
go: /x/net v0.0.0-20200707034311-ab3426394381
go: /x/text v0.3.3
.......
3、配置环境——重要
//编辑配置⽂件
vi ~/.bash_profile
//加⼊以下语句
# bee
PATH="${GOPATH}/bin:${PATH}"
export PATH
//运⾏命令⽣效:
source ~/.bash_profile
4、新建项⽬
bee new beego2
2020/07/29 15:16:07 INFO    ▶ 0001 generate new project support go modules. 2020/07/29 15:16:07 INFO    ▶ 0002
create  /Users/apple/go/src/d
create  /Users/apple/go/src/beego2/
create  /Users/apple/go/src/beego2/conf/
create  /Users/apple/go/src/beego2/controllers/
create  /Users/apple/go/src/beego2/models/
create  /Users/apple/go/src/beego2/routers/
create  /Users/apple/go/src/beego2/tests/
create  /Users/apple/go/src/beego2/static/
create  /Users/apple/go/src/beego2/static/js/
create  /Users/apple/go/src/beego2/static/css/
create  /Users/apple/go/src/beego2/static/img/
create  /Users/apple/go/src/beego2/views/
create  /Users/apple/go/src/beego2/f
create  /Users/apple/go/src/beego2/
create  /Users/apple/go/src/beego2/views/index.tpl
create  /Users/apple/go/src/beego2/
create  /Users/apple/go/src/beego2/tests/
create  /Users/apple/go/src/
2020/07/29 15:16:07 SUCCESS  ▶ 0003 New application successfully created! bee run
______
| ___ \
| |_/ /  ___  ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.11.0
2020/07/29 15:16:18 INFO    ▶ 0001 Using 'beego2' as 'appname'
2020/07/29 15:16:18 INFO    ▶ 0002
go: downloading gopkg.in/yaml.v2 v2.2.1
go: finding github/shiena/ansicolor latest
go: extracting gopkg.in/yaml.v2 v2.2.1
go: finding github/astaxie/beego v1.12.1
go: /x/crypto v0.0.0-20191011191535-87dc89f01550
go: finding gopkg.in/yaml.v2 v2.2.1
go: /x/net v0.0.0-20190620200207-3b0461eec859
go: /x/text v0.3.0
github/astaxie/beego/config
github/astaxie/beego/utils
gopkg.in/yaml.v2
github/astaxie/beego/logs
github/astaxie/beego/session
github/astaxie/beego/grace
github/astaxie/beego/toolbox
github/astaxie/beego/context
github/astaxie/beego/context/param
github/astaxie/beego
beego2/controllers
beego2/routers
beego2
2020/07/29 15:16:29 SUCCESS  ▶ 0003 Built Successfully!
2020/07/29 15:16:29 INFO    ▶ 0004 Restarting 'beego2'...
2020/07/29 15:16:29 SUCCESS  ▶ 0005 './beego2'
2020/07/29 15:16:29.860 [I] [asm_amd64.s:1357]  http server Running on :8080
2020/07/29 15:16:44.140 [D] [:2802]  |            ::1| 200 |  2.579479ms|  match| GET      /    r:/
2020/07/29 15:16:44.929 [D] [:2802]  |            ::1| 200 |    173.159µs|  match| GET      /static/js/reload.min.js
4、参数配置及路由
4.1 配置
Beego默认会解析当前应⽤下的f⽂件
4.2 路由
参考⽂章:
RESTful Controller 路由
⾃定义⽅法及 RESTful 规则
如果⽤户期望⾃定义函数名,那么可以使⽤如下⽅式:
beego.Router("/",&IndexController{},"*:Index")
使⽤第三个参数,第三个参数就是⽤来设置对应 method 到函数名,定义如下
*表⽰任意的 method 都执⾏该函数
使⽤ httpmethod:funcname 格式来展⽰
多个不同的格式使⽤;分割
多个 method 对应同⼀个 funcname,method 之间通过,来分割
以下是⼀个 RESTful 的设计⽰例:
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")
以下是多个 HTTP Method 指向同⼀个函数的⽰例:
beego.Router("/api",&RestController{},"get,post:ApiFunc")
以下是不同的 method 对应不同的函数,通过 ; 进⾏分割的⽰例:
beego.Router("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
可⽤的 HTTP Method:
*: 包含以下所有的函数
get: GET 请求
post: POST 请求
put: PUT 请求
delete: DELETE 请求
patch: PATCH 请求
options: OPTIONS 请求
head: HEAD 请求
如果同时存在 * 和对应的 HTTP Method,那么优先执⾏ HTTP Method 的⽅法,例如同时注册了如下所⽰的路由:
beego.Router("/simple",&SimpleController{},"*:AllFunc;post:PostFunc")
那么执⾏POST请求的时候,执⾏PostFunc⽽不执⾏AllFunc。
⾃定义函数的路由默认不⽀持 RESTful 的⽅法,也就是如果你设置了
beego.Router("/api",&RestController{},"post:ApiFunc")这样的路由,如果请求的⽅法是POST,那么不会默认去执⾏
Post函数。
5、Controller
beego.Controller 拥有很多⽅法,其中包括 Init、Prepare、Post、Get、Delete、Head等⽅法。我们可以通过重写的⽅式来实现这些⽅法,⽽我们上⾯的代码就是重写了 Get ⽅法。
我们可以通过各种⽅式获取数据,然后赋值到 this.Data 中,这是⼀个⽤来存储输出数据的 map,可以赋值任意类型的值,这⾥我们只是简单举例输出两个字符串。
最后⼀个就是需要去渲染的模板,this.TplName 就是需要渲染的模板,这⾥指定了 index.tpl,如果⽤户
不设置该参数,那么默认会去到模板⽬录的 Controller/<⽅法名>.tpl 查,例如上⾯的⽅法会去 maincontroller/get.tpl(⽂件、⽂件夹必须⼩写)。
⽤户设置了模板之后系统会⾃动的调⽤ Render 函数(这个函数是在 beego.Controller 中实现的),所以⽆需⽤户⾃⼰来调⽤渲染。
5.1 新建⼀个controller
//
package controllers
import (
"github/astaxie/beego"
)
type TestController struct {
beego.Controller
}
func (c *TestController) Get1(){
c.Ctx.WriteString("hello xiaoye")
}
5.2 写好路由
//
package routers
import (
"beego2/controllers"
"github/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{},"post:Get1")
beego.Router("/test", &controllers.TestController{},"get:Get1")
}
6、请求数据处理
6.1 get显⽰form,并将数据展⽰出来
//
type User struct{
Username string
Password string
}
func (c *TestInputController) Get1(){
//id := c.GetString("id")
//c.Ctx.WriteString(id)
//name := c.Input().Get("name")
//c.Ctx.WriteString(name + "</html>")
c.Ctx.WriteString(`<html><form action="127.0.0.1:8083/testinput" method="post">
<input type="text" name="Username"/>
<input type="password" name="Password"/>
<input type="submit" value="提交"/>
</form></html>`)
}
func (c *TestInputController) Post1(){
u := User{}react router6
if err:=c.ParseForm(&u) ; err != nil{
//process error
}
c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password)
}
//
package routers
import (
"beego2/controllers"
"github/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{},"post:Get1")
beego.Router("/test", &controllers.TestController{},"get:Get1")
beego.Router("/testinput", &controllers.TestInputController{},"get:Get1;post:Post1")
}
直接解析到struct
func (c *TestInputController) Post1(){
u := User{}
if err:=c.ParseForm(&u) ; err != nil{
//process error
}
c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password)
}
6.2 获取Request Body⾥的内容
在配置⽂件⾥设置 copyrequestbody = true
func (this *ObjectController) Poost(){
var ob models.Object
json.Unmarshal(this.Ctx.Input.RequestBody,&ob)
objectid:=models.AddOne(ob)
this.Data["json"]="{\"ObjectId\":\""+objectid+"\"}"
this.ServeJSON()
}
7、cookie与session
beego 内置了 session 模块,⽬前 session 模块⽀持的后端引擎包括 memory、cookie、file、mysql、redis、couchbase、memcache、postgres,⽤户也可以根据相应的 interface 实现⾃⼰的引擎。
7.1 cookie保存及清除
//
package controllers
import (
"github/astaxie/beego"
)
type TestloginController struct {
beego.Controller
}