前面介紹了beego框架實現的整體構思以及部分實現的偽代碼,這小節介紹通過beego建立一個博客系統,包括博客瀏覽、添加、修改、刪除等操作。
## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#博客目錄)博客目錄
博客目錄如下所示:
~~~
.
├── controllers
│?? ├── delete.go
│?? ├── edit.go
│?? ├── index.go
│?? ├── new.go
│?? └── view.go
├── main.go
├── models
│?? └── model.go
└── views
├── edit.tpl
├── index.tpl
├── layout.tpl
├── new.tpl
└── view.tpl
~~~
## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#博客路由)博客路由
博客主要的路由規則如下所示:
~~~
//顯示博客首頁
beego.Router("/", &controllers.IndexController{})
//查看博客詳細信息
beego.Router("/view/:id([0-9]+)", &controllers.ViewController{})
//新建博客博文
beego.Router("/new", &controllers.NewController{})
//刪除博文
beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{})
//編輯博文
beego.Router("/edit/:id([0-9]+)", &controllers.EditController{})
~~~
## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#數據庫結構)數據庫結構
數據庫設計最簡單的博客信息
~~~
CREATE TABLE entries (
id INT AUTO_INCREMENT,
title TEXT,
content TEXT,
created DATETIME,
primary key (id)
);
~~~
## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#控制器)控制器
IndexController:
~~~
type IndexController struct {
beego.Controller
}
func (this *IndexController) Get() {
this.Data["blogs"] = models.GetAll()
this.Layout = "layout.tpl"
this.TplNames = "index.tpl"
}
~~~
ViewController:
~~~
type ViewController struct {
beego.Controller
}
func (this *ViewController) Get() {
id, _ := strconv.Atoi(this.Ctx.Input.Params(":id"))
this.Data["Post"] = models.GetBlog(id)
this.Layout = "layout.tpl"
this.TplNames = "view.tpl"
}
~~~
NewController
~~~
type NewController struct {
beego.Controller
}
func (this *NewController) Get() {
this.Layout = "layout.tpl"
this.TplNames = "new.tpl"
}
func (this *NewController) Post() {
inputs := this.Input()
var blog models.Blog
blog.Title = inputs.Get("title")
blog.Content = inputs.Get("content")
blog.Created = time.Now()
models.SaveBlog(blog)
this.Ctx.Redirect(302, "/")
}
~~~
EditController
~~~
type EditController struct {
beego.Controller
}
func (this *EditController) Get() {
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
this.Data["Post"] = models.GetBlog(id)
this.Layout = "layout.tpl"
this.TplNames = "new.tpl"
}
func (this *EditController) Post() {
inputs := this.Input()
var blog models.Blog
blog.Id, _ = strconv.Atoi(inputs.Get("id"))
blog.Title = inputs.Get("title")
blog.Content = inputs.Get("content")
blog.Created = time.Now()
models.SaveBlog(blog)
this.Ctx.Redirect(302, "/")
}
~~~
DeleteController
~~~
type DeleteController struct {
beego.Controller
}
func (this *DeleteController) Get() {
id, _ := strconv.Atoi(this.Ctx.Input.Params(":id"))
blog := GetBlog(id int)
this.Data["Post"] = blog
models.DelBlog(blog)
this.Ctx.Redirect(302, "/")
}
~~~
## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#model層)model層
~~~
package models
import (
"database/sql"
"github.com/astaxie/beedb"
_ "github.com/ziutek/mymysql/godrv"
"time"
)
type Blog struct {
Id int `PK`
Title string
Content string
Created time.Time
}
func GetLink() beedb.Model {
db, err := sql.Open("mymysql", "blog/astaxie/123456")
if err != nil {
panic(err)
}
orm := beedb.New(db)
return orm
}
func GetAll() (blogs []Blog) {
db := GetLink()
db.FindAll(&blogs)
return
}
func GetBlog(id int) (blog Blog) {
db := GetLink()
db.Where("id=?", id).Find(&blog)
return
}
func SaveBlog(blog Blog) (bg Blog) {
db := GetLink()
db.Save(&blog)
return bg
}
func DelBlog(blog Blog) {
db := GetLink()
db.Delete(&blog)
return
}
~~~
## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#view層)view層
layout.tpl
~~~
<html>
<head>
<title>My Blog</title>
<style>
#menu {
width: 200px;
float: right;
}
</style>
</head>
<body>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/new">New Post</a></li>
</ul>
{{.LayoutContent}}
</body>
</html>
~~~
index.tpl
~~~
<h1>Blog posts</h1>
<ul>
{{range .blogs}}
<li>
<a href="/view/{{.Id}}">{{.Title}}</a>
from {{.Created}}
<a href="/edit/{{.Id}}">Edit</a>
<a href="/delete/{{.Id}}">Delete</a>
</li>
{{end}}
</ul>
~~~
view.tpl
~~~
<h1>{{.Post.Title}}</h1>
{{.Post.Created}}<br/>
{{.Post.Content}}
~~~
new.tpl
~~~
<h1>New Blog Post</h1>
<form action="" method="post">
標題:<input type="text" name="title"><br>
內容:<textarea name="content" colspan="3" rowspan="10"></textarea>
<input type="submit">
</form>
~~~
edit.tpl
~~~
<h1>Edit {{.Post.Title}}</h1>
<h1>New Blog Post</h1>
<form action="" method="post">
標題:<input type="text" name="title" value="{{.Post.Title}}"><br>
內容:<textarea name="content" colspan="3" rowspan="10">{{.Post.Content}}</textarea>
<input type="hidden" name="id" value="{{.Post.Id}}">
<input type="submit">
</form>
~~~
- 第一章 Go環境配置
- 1.1 Go安裝
- 1.2 GOPATH 與工作空間
- 1.3 Go 命令
- 1.4 Go開發工具
- 1.5 小結
- 第二章 Go語言基礎
- 2.1 你好,Go
- 2.2 Go基礎
- 2.3 流程和函數
- 2.4 struct類型
- 2.5 面向對象
- 2.6 interface
- 2.7 并發
- 2.8 總結
- 第三章 Web基礎
- 3.1 Web工作方式
- 3.2 Go搭建一個Web服務器
- 3.3 Go如何使得Web工作
- 3.4 Go的http包詳解
- 3.5 小結
- 第四章 表單
- 4.1 處理表單的輸入
- 4.2 驗證表單的輸入
- 4.3 預防跨站腳本
- 4.4 防止多次遞交表單
- 4.5 處理文件上傳
- 4.6 小結
- 第五章 訪問數據庫
- 5.1 database/sql接口
- 5.2 使用MySQL數據庫
- 5.3 使用SQLite數據庫
- 5.4 使用PostgreSQL數據庫
- 5.5 使用beedb庫進行ORM開發
- 5.6 NOSQL數據庫操作
- 5.7 小結
- 第六章 session和數據存儲
- 6.1 session和cookie
- 6.2 Go如何使用session
- 6.3 session存儲
- 6.4 預防session劫持
- 6.5 小結
- 第七章 文本處理
- 7.1 XML處理
- 7.2 JSON處理
- 7.3 正則處理
- 7.4 模板處理
- 7.5 文件操作
- 7.6 字符串處理
- 7.7 小結
- 第八章 Web服務
- 8.1 Socket編程
- 8.2 WebSocket
- 8.3 REST
- 8.4 RPC
- 8.5 小結
- 第九章 安全與加密
- 9.1 預防CSRF攻擊
- 9.2 確保輸入過濾
- 9.3 避免XSS攻擊
- 9.4 避免SQL注入
- 9.5 存儲密碼
- 9.6 加密和解密數據
- 9.7 小結
- 第十章 國際化和本地化
- 10.1 設置默認地區
- 10.2 本地化資源
- 10.3 國際化站點
- 10.4 小結
- 第十一章 錯誤處理,調試和測試
- 11.1 錯誤處理
- 11.2 使用GDB調試
- 11.3 Go怎么寫測試用例
- 11.4 小結
- 第十二章 部署與維護
- 12.1 應用日志
- 12.2 網站錯誤處理
- 12.3 應用部署
- 12.4 備份和恢復
- 12.5 小結
- 第十三章 如何設計一個Web框架
- 13.1 項目規劃
- 13.2 自定義路由器設計
- 13.3 controller設計
- 13.4 日志和配置設計
- 13.5 實現博客的增刪改
- 13.6 小結
- 第十四章 擴展Web框架
- 14.1 靜態文件支持
- 14.2 Session支持
- 14.3 表單及驗證支持
- 14.4 用戶認證
- 14.5 多語言支持
- 14.6 pprof支持
- 14.7 小結
- 附錄A 參考資料