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

网友投稿 216 2022-07-19

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

博客系统后端接口开发

路由测试

http.HandleFunc("/ping", Pong)

func Pong(w http.ResponseWriter, r *http.Request) {

w.Write([]byte("pong"))

}

上传博客

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

http接口设计:

名称

说明

URL

/upload

METHOD

POST

请求数据

form文件中二进制数据

响应数据

请求示例:

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

代码处理:

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}]

请求举例:

curl http://localhost:8086/lists

响应示例:

[{"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

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:go语言模版编程(go模板语法)
下一篇:Go的标准项目布局(go开发规范)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~