设为首页 加入收藏

TOP

go语言打造个人博客系统(二)(一)
2019-01-17 16:08:48 】 浏览:275
Tags:语言 打造 个人 博客 系统

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



??在上篇文章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 Up
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[Go] 使用go语言解决现代编程难题 下一篇Golang并行判断素数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目