内容简介:在上篇文章博客上传正常需要传递很多文本,这个字符串存储不太理想,习惯上会把博客内容形成一个文件,将文件信息存储到后端服务器当中http接口设计:
在上篇文章 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
}
以上就是博客系统后端接口的全部内容,再搭配上一套好看的前端界面就可以使用啦。亲自写过golang代码,才会真正的体会到go语言的优点,快来学习吧。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 闲鱼如何打造高效 CEP 系统及 DSL 编程语言
- Google 希望将 Go 打造成云端应用开发的首选语言
- GoLand 2019.2.1 发布,JetBrains 打造的 Go 语言 IDE
- Go语言打造分布式Crontab 轻松搞定高性能任务调度
- Go语言打造分布式Crontab 轻松搞定高性能任务调度 高清无密
- 打造刀郎安全PHP系统
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
虚拟现实:最后的传播
聂有兵 / 中国发展出版社 / 2017-4-1 / 39.00
本书对“虚拟现实”这一诞生自70年代却在今天成为热门话题的概念进行了历史发展式的分析和回顾,认为虚拟现实是当今最重大的社会变革的技术因素之一,对虚拟现实在未来百年可能给人类社会的各个层面带来的影响进行说明,结合多个大众媒介的发展趋势,合理地推演未来虚拟现实在政治、经济、文化等领域的态势,并基于传播学理论框架提出了几个新的观点。对于普通读者,本书可以普及一般的虚拟现实知识;对于传媒行业,本书可以引导......一起来看看 《虚拟现实:最后的传播》 这本书的介绍吧!