经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
go语言打造个人博客系统(二)
来源:cnblogs  作者:通证派大本营  时间:2019/1/18 9:30:26  对本文有异议

go语言打造个人博客系统(二)



??在上篇文章go语言打造个人博客系统(一)中,我们了解了go语言的优点和go语言的数据库操作,本次我们会完成博客系统的后端开发。

博客系统后端接口开发

  • 路由测试
  1. http.HandleFunc("/ping", Pong)
  1. func Pong(w http.ResponseWriter, r *http.Request) {
  2. w.Write([]byte("pong"))
  3. }
  • 上传博客

??博客上传正常需要传递很多文本,这个字符串存储不太理想,习惯上会把博客内容形成一个文件,将文件信息存储到后端服务器当中

http接口设计:

名称 说明
URL /upload
METHOD POST
请求数据 form文件中二进制数据
响应数据

请求示例:

  1. curl --form "fileupload=@22.txt" http://localhost:8086/upload

代码处理:

  1. http.HandleFunc("/upload", UploadFile)
  2. //文件上传
  3. func UploadFile(w http.ResponseWriter, r *http.Request) {
  4. f, h, err := r.FormFile("fileupload")
  5. if err != nil {
  6. panic(err)
  7. }
  8. dirname := "../file/" + h.Filename
  9. file, err := os.Create(dirname)
  10. if err != nil {
  11. panic(err)
  12. }
  13. _, err = io.Copy(file, f)
  14. if err != nil {
  15. panic(err)
  16. }
  17. defer file.Close()
  18. fmt.Println(h)
  19. //w.Write([]byte("upload success"))
  20. //写到 数据库 中
  21. fmt.Println(h.Filename, dirname, h.Size)
  22. MgSess.UploadFile(h.Filename, h.Filename, h.Size)
  23. }
  24. //mongo处理
  25. func (m *MongoSessin) UploadFile(title, dir string, length int64) error {
  26. fmt.Println("call UploadFile")
  27. table := m.Session.DB("myblog").C("blogs")
  28. return table.Insert(&BlogInfo{title, length, dir})
  29. }
  • 查看博客列表

??对于发表的多篇博客,有一个列表的展示功能

http接口设计:

名称 说明
URL /lists
METHOD GET
请求数据
响应数据 [{title,length,filedir},{title,length,filedir}]

请求举例:

  1. curl http://localhost:8086/lists

响应示例:

  1. [{"Title":"11.txt","Length":16,"FileDir":"11.txt"},{"Title":"22.txt","Length":21,"FileDir":"22.txt"}]
  1. http.HandleFunc("/lists", Lists)
  2. //路由函数
  3. func Lists(w http.ResponseWriter, r *http.Request) {
  4. s, err := MgSess.Lists()
  5. if err != nil {
  6. panic(err)
  7. }
  8. fmt.Println(s)
  9. data, err := json.Marshal(s)
  10. fmt.Println(string(data))
  11. w.Write(data)
  12. }
  13. //mongo处理
  14. func (m *MongoSessin) Lists() ([]BlogInfo, error) {
  15. fmt.Println("call Lists")
  16. var blogInfos []BlogInfo
  17. err := m.Session.DB("myblog").C("blogs").Find(nil).All(&blogInfos)
  18. return blogInfos, err
  19. }
  • 查看博客详细内容

??对于某一篇博文,可以查看详细内容,这个就要将之前的博客文件传递给前端。

http接口设计:

名称 说明
URL /:filename
METHOD GET
请求数据
响应数据 文件内容

请求举例:

  1. curl http://localhost:8086/22.txt

文件服务

  1. http.Handle("/", http.FileServer(http.Dir("../file/")))

全部代码

  1. /*
  2. main.go
  3. yekai
  4. pdj
  5. */
  6. package main
  7. import (
  8. "fmt"
  9. "net/http"
  10. //"gopkg.in/mgo.v2/bson"
  11. )
  12. func main() {
  13. fmt.Println("blog begin ...")
  14. MgSess = &MongoSessin{}
  15. MgSess.Connect("localhost:27017")
  16. http.HandleFunc("/ping", Pong)
  17. http.HandleFunc("/upload", UploadFile)
  18. http.HandleFunc("/lists", Lists)
  19. http.Handle("/", http.FileServer(http.Dir("../file/")))
  20. http.ListenAndServe(":8086", nil)
  21. }
  22. /*
  23. router.go
  24. yekai
  25. pdj
  26. */
  27. package main
  28. import (
  29. "encoding/json"
  30. "fmt"
  31. "io"
  32. "net/http"
  33. "os"
  34. )
  35. func Pong(w http.ResponseWriter, r *http.Request) {
  36. w.Write([]byte("pong"))
  37. }
  38. func UploadFile(w http.ResponseWriter, r *http.Request) {
  39. f, h, err := r.FormFile("fileupload")
  40. if err != nil {
  41. panic(err)
  42. }
  43. dirname := "../file/" + h.Filename
  44. file, err := os.Create(dirname)
  45. if err != nil {
  46. panic(err)
  47. }
  48. _, err = io.Copy(file, f)
  49. if err != nil {
  50. panic(err)
  51. }
  52. defer file.Close()
  53. fmt.Println(h)
  54. //w.Write([]byte("upload success"))
  55. //写到 数据库 中
  56. fmt.Println(h.Filename, dirname, h.Size)
  57. MgSess.UploadFile(h.Filename, h.Filename, h.Size)
  58. }
  59. func Lists(w http.ResponseWriter, r *http.Request) {
  60. s, err := MgSess.Lists()
  61. if err != nil {
  62. panic(err)
  63. }
  64. fmt.Println(s)
  65. data, err := json.Marshal(s)
  66. fmt.Println(string(data))
  67. w.Write(data)
  68. }
  69. /*
  70. blog.go
  71. yekai
  72. pdj
  73. */
  74. package main
  75. import (
  76. "fmt"
  77. "gopkg.in/mgo.v2"
  78. )
  79. type MongoSessin struct {
  80. Session *mgo.Session
  81. }
  82. var MgSess *MongoSessin
  83. type BlogInfo struct {
  84. Title string
  85. Length int64
  86. FileDir string
  87. }
  88. func (m *MongoSessin) Connect(url string) {
  89. session, err := mgo.Dial(url)
  90. if err != nil {
  91. panic(err)
  92. }
  93. m.Session = session
  94. }
  95. func (m *MongoSessin) UploadFile(title, dir string, length int64) error {
  96. fmt.Println("call UploadFile")
  97. table := m.Session.DB("myblog").C("blogs")
  98. return table.Insert(&BlogInfo{title, length, dir})
  99. }
  100. func (m *MongoSessin) Lists() ([]BlogInfo, error) {
  101. fmt.Println("call Lists")
  102. var blogInfos []BlogInfo
  103. err := m.Session.DB("myblog").C("blogs").Find(nil).All(&blogInfos)
  104. return blogInfos, err
  105. }

??以上就是博客系统后端接口的全部内容,再搭配上一套好看的前端界面就可以使用啦。亲自写过golang代码,才会真正的体会到go语言的优点,快来学习吧。



image

原文链接:http://www.cnblogs.com/tokenpai/p/10282094.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号