Revel是一個go語言寫的web框架,這個框架源於java的 Play! Framework.清晰的MVC結構,是如今go語言Web框架中優秀的框架。 css
Revel的功能有
1.熱部署
Revel會自動編譯你的go代碼和templates模板文件,這個相似jsp功能。
2.簡單的選擇
Revel提供了一個工具包,用戶平常的網絡維護。也能夠放棄這個工具包,直接訪問底層應用。
3.輕量級線程
Revel創建在Go Http Server之上,使用Goroutine來處理請求。 html
Quick Start
安裝revel以前,先安裝go環境
安裝還須要hg和git支持
設置GOPATH,而後把revel安裝到GOPATH內
go get github.com/robfig/revel
編譯revel
go build -o bin/revel github.com/robfig/revel/cmd
運行revel
bin/revel run github.com/robfig/revel/samples/chat
這裏運行了一個revel的應用chat java
Revel應用結構
3 |
/controllers #app下用於存放controllers的目錄 |
6 |
/controllerName #與controller名對應目錄,其下存放模板文件 |
9 |
/public #靜態文件,css,js,圖片 |
Revel的MVC實現
Model
model就是一個普通結構體,存放屬性 git
View
Revel的View經過 Go Templates來實現
模板存放於views目錄下,命名方式是ControllerName/ActionName.html github
1 |
{{/* app/views/Application/Register.html */}} |
3 |
{{template "header.html" .}} |
6 |
<form action="/register" method="POST"> |
7 |
{{with $field := field "user.Username" .}} |
8 |
<p class="{{$field.ErrorClass}}"> |
9 |
<strong>Username:</strong> |
10 |
<input type="text" name="{{$field.Name}}" size="16" value="{{$field.Flash}}"> * |
11 |
<span class="error">{{$field.Error}}</span> |
15 |
{{/* other fields */}} |
18 |
<input type="submit" value="Register"> <a href="/">Cancel</a> |
22 |
{{template "footer.html" .}} |
Controller
Controller:負責數據的綁定,驗證,cookie和session的處理,業務處理及返回 web
1 |
// app/controllers/app.go |
2 |
type Application struct { |
3 |
*revel.Controller //建立一個Application的Controller,每個Controller必須依賴*revel.Controller,在go中,至關於繼承了*revel.Controller |
6 |
func (c Application) Register() revel.Result {//定義一個Controller的Action |
11 |
func (c Application) SaveUser(user models.User, verifyPassword string) revel.Result { |
12 |
c.Validation.Required(verifyPassword)//經過Controller內部工具來驗證驗證 |
13 |
c.Validation.Required(verifyPassword == user.Password) |
14 |
Message("Password does not match") |
15 |
user.Validate(c.Validation) |
17 |
if c.Validation.HasErrors() { |
20 |
return c.Redirect(Application.Register) |
23 |
user.HashedPassword, _ = bcrypt.GenerateFromPassword( |
24 |
[]byte(user.Password), bcrypt.DefaultCost) |
25 |
err := c.Txn.Insert(&user) |
30 |
c.Session["user"] = user.Username //Session處理 |
31 |
c.Flash.Success("Welcome, " + user.Name) |
32 |
return c.Redirect(Hotels.Index) //從定向到另一個Action |
Routing
在mvc中還有重要一點就是routing,Revel如何經過鏈接來定位Action呢,那須要Routing。
在conf下有一個routes文件用來配置,格式以下 cookie
2 |
GET /login Application.Login # A simple path |
3 |
GET /hotels/? Hotels.Index # Match /hotels and /hotels/ (optional trailing slash) |
4 |
GET /hotels/{id} Hotels.Show # Extract a URI argument (matching /[^/]+/) |
5 |
POST /hotels/{<[0-9]+>id} Hotels.Save # URI arg with custom regex |
6 |
WS /hotels/{id}/feed Hotels.Feed # WebSockets. |
7 |
POST /hotels/{id}/{action} Hotels.{action} # Automatically route some actions. |
8 |
GET /public/ staticDir:public # Map /app/public resources under /public/... |
9 |
* /{controller}/{action} {controller}.{action} # Catch all; Automatic URL generation |
支持Http的方法及WobSockets
對於Revel簡單介紹到這裏了 網絡
本篇來源:http://blog.gcove.net/go%E8%AF%AD%E8%A8%80web%E6%A1%86%E6%9E%B6revel%E4%BB%8B%E7%BB%8D.html session