在上篇文章go语言打造我的博客系统(一)中,咱们了解了go语言的优势和go语言的数据库操做,本次咱们会完成博客系统的后端开发。html
http.HandleFunc("/ping", Pong)
func Pong(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) }
博客上传正常须要传递不少文本,这个字符串存储不太理想,习惯上会把博客内容造成一个文件,将文件信息存储到后端服务器当中前端
http接口设计:golang
名称 | 说明 |
---|---|
URL | /upload |
METHOD | POST |
请求数据 | form文件中二进制数据 |
响应数据 | 无 |
请求示例:数据库
curl --form "fileupload=@22.txt" http://localhost:8086/upload
代码处理:json
http.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}] |
请求举例:session
curl http://localhost:8086/lists
响应示例:curl
[{"Title":"11.txt","Length":16,"FileDir":"11.txt"},{"Title":"22.txt","Length":21,"FileDir":"22.txt"}]
http.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 |
请求数据 | |
响应数据 | 文件内容 |
请求举例:
curl http://localhost:8086/22.txt
文件服务
http.Handle("/", http.FileServer(http.Dir("../file/")))
所有代码
/* 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语言的优势,快来学习吧。