在上篇文章go语言打造个人博客系统(一)中,我们了解了go语言的优点和go语言的数据库操作,本次我们会完成博客系统的后端开发。
博客系统后端接口开发
- 路由测试
复制代码
1http.HandleFunc("/ping", Pong)
复制代码
1
2
3func Pong(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) }
- 上传博客
博客上传正常需要传递很多文本,这个字符串存储不太理想,习惯上会把博客内容形成一个文件,将文件信息存储到后端服务器当中
http接口设计:
名称 | 说明 |
---|---|
URL | /upload |
METHOD | POST |
请求数据 | form文件中二进制数据 |
响应数据 | 无 |
请求示例:
复制代码
1curl --form "fileupload=@22.txt" http://localhost:8086/upload
代码处理:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31http.HandleFunc("/upload", UploadFile) //文件上传 func UploadFile(w http.ResponseWriter, r *http.Request) { f, h, err := r.FormFile("fileupload") if err != nil { panic(err) } dirname := "../file/" + h.Filename file, err := os.Create(dirname) if err != nil { panic(err) } _, err = io.Copy(file, f) if err != nil { panic(err) } defer file.Close() fmt.Println(h) //w.Write([]byte("upload success")) //写到 数据库 中 fmt.Println(h.Filename, dirname, h.Size) MgSess.UploadFile(h.Filename, h.Filename, h.Size) } //mongo处理 func (m *MongoSessin) UploadFile(title, dir string, length int64) error { fmt.Println("call UploadFile") table := m.Session.DB("myblog").C("blogs") return table.Insert(&BlogInfo{title, length, dir}) }
- 查看博客列表
对于发表的多篇博客,有一个列表的展示功能
http接口设计:
名称 | 说明 |
---|---|
URL | /lists |
METHOD | GET |
请求数据 | |
响应数据 | [{title,length,filedir},{title,length,filedir}] |
请求举例:
复制代码
1
2curl http://localhost:8086/lists
响应示例:
复制代码
1[{"Title":"11.txt","Length":16,"FileDir":"11.txt"},{"Title":"22.txt","Length":21,"FileDir":"22.txt"}]
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20http.HandleFunc("/lists", Lists) //路由函数 func Lists(w http.ResponseWriter, r *http.Request) { s, err := MgSess.Lists() if err != nil { panic(err) } fmt.Println(s) data, err := json.Marshal(s) fmt.Println(string(data)) w.Write(data) } //mongo处理 func (m *MongoSessin) Lists() ([]BlogInfo, error) { fmt.Println("call Lists") var blogInfos []BlogInfo err := m.Session.DB("myblog").C("blogs").Find(nil).All(&blogInfos) return blogInfos, err }
- 查看博客详细内容
对于某一篇博文,可以查看详细内容,这个就要将之前的博客文件传递给前端。
http接口设计:
名称 | 说明 |
---|---|
URL | /:filename |
METHOD | GET |
请求数据 | |
响应数据 | 文件内容 |
请求举例:
复制代码
1
2curl http://localhost:8086/22.txt
文件服务
复制代码
1http.Handle("/", http.FileServer(http.Dir("../file/")))
全部代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111/* main.go yekai pdj */ package main import ( "fmt" "net/http" //"gopkg.in/mgo.v2/bson" ) func main() { fmt.Println("blog begin ...") MgSess = &MongoSessin{} MgSess.Connect("localhost:27017") http.HandleFunc("/ping", Pong) http.HandleFunc("/upload", UploadFile) http.HandleFunc("/lists", Lists) http.Handle("/", http.FileServer(http.Dir("../file/"))) http.ListenAndServe(":8086", nil) } /* router.go yekai pdj */ package main import ( "encoding/json" "fmt" "io" "net/http" "os" ) func Pong(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) } func UploadFile(w http.ResponseWriter, r *http.Request) { f, h, err := r.FormFile("fileupload") if err != nil { panic(err) } dirname := "../file/" + h.Filename file, err := os.Create(dirname) if err != nil { panic(err) } _, err = io.Copy(file, f) if err != nil { panic(err) } defer file.Close() fmt.Println(h) //w.Write([]byte("upload success")) //写到 数据库 中 fmt.Println(h.Filename, dirname, h.Size) MgSess.UploadFile(h.Filename, h.Filename, h.Size) } func Lists(w http.ResponseWriter, r *http.Request) { s, err := MgSess.Lists() if err != nil { panic(err) } fmt.Println(s) data, err := json.Marshal(s) fmt.Println(string(data)) w.Write(data) } /* blog.go yekai pdj */ package main import ( "fmt" "gopkg.in/mgo.v2" ) type MongoSessin struct { Session *mgo.Session } var MgSess *MongoSessin type BlogInfo struct { Title string Length int64 FileDir string } func (m *MongoSessin) Connect(url string) { session, err := mgo.Dial(url) if err != nil { panic(err) } m.Session = session } func (m *MongoSessin) UploadFile(title, dir string, length int64) error { fmt.Println("call UploadFile") table := m.Session.DB("myblog").C("blogs") return table.Insert(&BlogInfo{title, length, dir}) } func (m *MongoSessin) Lists() ([]BlogInfo, error) { fmt.Println("call Lists") var blogInfos []BlogInfo err := m.Session.DB("myblog").C("blogs").Find(nil).All(&blogInfos) return blogInfos, err }
以上就是博客系统后端接口的全部内容,再搭配上一套好看的前端界面就可以使用啦。亲自写过golang代码,才会真正的体会到go语言的优点,快来学习吧。
转载于:https://www.cnblogs.com/tokenpai/p/10282094.html
最后
以上就是苗条枫叶最近收集整理的关于go语言打造个人博客系统(二)的全部内容,更多相关go语言打造个人博客系统(二)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复