Beego官網自己已經整理的很是詳細了,可是做爲一個學習者,我仍是決定本身好好整理一下,這樣在後面使用的時候本身對每部分才能很是熟悉,即便忘記了,也能夠迅速定位本身要用的知識在哪裏。固然也是對官網的一個精簡整理,同時結合一些例子,更好的理解和學習Beegocss
Beego官網地址:https://beego.me
html
此次整理也是一個初步的整理,正好在這個以後把以前經過net/http實現的短url項目經過beego框架實現一遍mysql
go get github.com/astaxie/beegogit
bee 工具是一個爲了協助快速開發 beego 項目而建立的項目,經過 bee 您能夠很容易的進行 beego 項目的建立、熱編譯、開發、測試、和部署。github
go get github.com/beego/beeweb
當咱們安裝好bee以後,bee命令默認是放在GOPATH/bin裏面,所以需要把GOPATH/bin裏面,因此須要把GOPATH/bin 添加到您的環境變量中sql
bee經常使用的命令數據庫
bee new <項目名> :建立一個項目,這樣的項目通常是web項目
須要注意的是這個命令須在 $GOPATH/src 下執行,最後生成的目錄結構爲:json
├── conf
│ └── app.conf
├── controllers
│ └── default.go
├── main.go
├── models
├── routers
│ └── router.go
├── static
│ ├── css
│ ├── img
│ └── js
│ └── reload.min.js
├── tests
│ └── default_test.go
└── views
└── index.tpl
bee api myapi : 建立一個api項目,生成的目錄結構爲api
├── conf
│ └── app.conf
├── controllers
│ ├── object.go
│ └── user.go
├── main.go
├── models
│ ├── object.go
│ └── user.go
├── routers
│ └── router.go
└── tests
└── default_test.go
bee run 命令是監控 beego 的項目,經過 fsnotify監控文件系統。可是注意該命令必須在 $GOPATH/src/appname 下執行。這樣當咱們在開發過程當中,beego能夠實時監測咱們的代碼文件發生變化,這樣咱們就不用從新編譯運行,很是方便咱們調試,咱們能夠將上面的myweb2項目經過bee run運行起來
localhost:myweb2 zhaofan$ go run go run: no go files listed localhost:myweb2 zhaofan$ bee run ______ | ___ \ | |_/ / ___ ___ | ___ \ / _ \ / _ \ | |_/ /| __/| __/ \____/ \___| \___| v1.9.1 2018/03/19 23:59:20 INFO ▶ 0001 Using 'myweb2' as 'appname' 2018/03/19 23:59:20 INFO ▶ 0002 Initializing watcher... myweb2/controllers myweb2/routers myweb2 2018/03/19 23:59:22 SUCCESS ▶ 0003 Built Successfully! 2018/03/19 23:59:22 INFO ▶ 0004 Restarting 'myweb2'... 2018/03/19 23:59:22 SUCCESS ▶ 0005 './myweb2' is running... 2018/03/19 23:59:22.572 [I] [asm_amd64.s:2337] http server Running on http://:8080
咱們經過打開瀏覽器訪問:http://127.0.0.1:8080,能夠看到如圖:
Beego架構圖如:
這就是Beegode 八大獨立的模塊
Beego的執行邏輯,beego 是一個典型的 MVC 架構
咱們從上面myweb2的目錄接口也能夠看出來:
M(models 目錄)、V(views 目錄)和 C(controllers 目錄)的結構
咱們看一下Beego的入口函數:
package main import ( _ "myweb2/routers" "github.com/astaxie/beego" ) func main() { beego.Run() }
引入了一個包 _ "myweb2/routers 咱們看一下這個包的內容
package routers import ( "myweb2/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) }
這裏僅僅作了一個初始化,路由包裏面咱們看到執行了路由註冊 beego.Router, 這個函數的功能是映射 URL 到 controller,第一個參數是 URL (用戶請求的地址),這裏咱們註冊的是 /,也就是咱們訪問的不帶任何參數的 URL,第二個參數是對應的 Controller,也就是咱們即將把請求分發到那個控制器來執行相應的邏輯。這裏對路由設置的一個小結:
咱們看一下controller的代碼:
package controllers import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller // 這裏能夠看作是其餘語言中的繼承 } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" }
對上述代碼進行簡單分析:
上述代碼中:c.TplName = "index.tpl"默認回去views下面去找模板文件
除了上面的C.Data這種方法以外,還有不少方法,如經常使用的c.ServerJson()這樣就會去c.Data中尋找key爲json的值
用戶設置了模板以後系統會自動的調用 Render 函數(這個函數是在 beego.Controller 中實現的),因此無需用戶本身來調用渲染。
固然也能夠不使用模版,直接用 this.Ctx.WriteString 輸出字符串,如:
func (c *MainController) Get() { c.Ctx.WriteString("hello") }
正則路由
beego.Router(「/api/?:id」, &controllers.RController{}) 默認匹配 //匹配 /api/123 :id = 123 能夠匹配 /api/ 這個URL
beego.Router(「/api/:id」, &controllers.RController{}) 默認匹配 //匹配 /api/123 :id = 123 不能夠匹配 /api/ 這個URL
beego.Router(「/api/:id([0-9]+)「, &controllers.RController{}) 自定義正則匹配 //匹配 /api/123 :id = 123
beego.Router(「/user/:username([\w]+)「, &controllers.RController{}) 正則字符串匹配 //匹配 /user/astaxie :username = astaxie
beego.Router(「/download/*.*」, &controllers.RController{}) *匹配方式 //匹配 /download/file/api.xml :path= file/api :ext=xml
beego.Router(「/download/ceshi/*「, &controllers.RController{}) *全匹配方式 //匹配 /download/ceshi/file/api.json :splat=file/api.json
beego.Router(「/:id:int」, &controllers.RController{}) int 類型設置方式,匹配 :id爲int 類型,框架幫你實現了正則 ([0-9]+)
beego.Router(「/:hi:string」, &controllers.RController{}) string 類型設置方式,匹配 :hi 爲 string 類型。框架幫你實現了正則 ([\w]+)
beego.Router(「/cms_:id([0-9]+).html」, &controllers.CmsController{}) 帶有前綴的自定義正則 //匹配 :id 爲正則類型。匹配 cms_123.html 這樣的 url :id = 123
能夠在 Controller 中經過以下方式獲取上面的變量:
this.Ctx.Input.Param(":id")
this.Ctx.Input.Param(":username")
this.Ctx.Input.Param(":splat")
this.Ctx.Input.Param(":path")
this.Ctx.Input.Param(":ext")
自定義方法及 RESTful 規則:
beego.Router("/",&IndexController{},"*:Index")
使用第三個參數,第三個參數就是用來設置對應 method 到函數名,定義以下
beego.Router("/api",&RestController{},"get,post:ApiFunc")
可用的 HTTP Method:
若是同時存在 * 和對應的 HTTP Method,那麼優先執行 HTTP Method 的方法
咱們知道 Web 應用中咱們用的最多的就是數據庫操做,而 model 層通常用來作這些操做,咱們的 bee new 例子不存在 Model 的演示,可是 bee api 應用中存在 model 的應用。說的簡單一點,若是您的應用足夠簡單,那麼 Controller 能夠處理一切的邏輯,若是您的邏輯裏面存在着能夠複用的東西,那麼就抽取出來變成一個模塊。所以 Model 就是逐步抽象的過程,
beego 默認註冊了 static 目錄爲靜態處理的目錄,註冊樣式:URL 前綴和映射的目錄(在/main.go文件中beego.Run()以前加入):StaticDir["/static"] = "static"
Beego默認使用static目錄做爲靜態文件目錄
beego.SetStaticPath增長新的靜態文件目錄
默認的配置文件內容:
appname = myweb2 httpport = 8080 runmode = dev
固然前提是你已經在本身配置文件中添加了下面這些字段:
beego.AppConfig.String("mysql_user")
beego.AppConfig.String("mysql_pwd")
若是咱們將配置文件更改成:
appname = myweb2 httpport = 8080 runmode = dev [dbconfig] mysqlhost = 127.0.0.1 mysqlport = 3308 username = root passwd = 123
咱們能夠經過[命名]的方式將配置文件進行分組
這樣當咱們獲取變量的時候,能夠經過下面方式獲取:
beego.AppConfig.String("dbconfig::mysql_user")
beego.AppConfig.String("dbconfig::mysql_port")
AppConfig 的方法以下:
Set(key, val string) error String(key string) string Strings(key string) []string Int(key string) (int, error) Int64(key string) (int64, error) Bool(key string) (bool, error) Float(key string) (float64, error) DefaultString(key string, defaultVal string) string DefaultStrings(key string, defaultVal []string) DefaultInt(key string, defaultVal int) int DefaultInt64(key string, defaultVal int64) int64 DefaultBool(key string, defaultVal bool) bool DefaultFloat(key string, defaultVal float64) float64 DIY(key string) (interface{}, error) GetSection(section string) (map[string]string, error) SaveConfigFile(filename string) error
Controller中的方法獲取
不論是post仍是get的其餘方式提交的數據均可以經過上面的方式獲取
若是在日常開發中確定設計到和其餘語言的對接數據,這個時候對方發過來的數據可能就是一個json格式的數據,若是想要獲取能夠經過下面方法:
這樣就能夠經過c.Ctx.Input.RequestBody 獲取