golang + restful

用golang写一个restful api。如果您不知道什么是restful,可以看阮一峰老师的教程

首先,我们需要解决的是路由的问题,也就是如何将不同的url映射到不同的处理函数。

    router.GET("/api/todo/:todoid", getTodoById)
    router.POST("/api/todo/", addTodo)
    router.DELETE("/api/todo/:todoid", deleteTodo)
    router.PUT("/api/todo/:todoid", modifyTodo)

作为一个初学者,我马上打开github,找到了awesome-go,经过一番调研,我感觉有几个http router的库比较适合:bone, httprouter, mux

最终我选择了httprouter这个库。因为它真的很像express。

基本框架

首先,我们设计了四个路由,分别为根据Id获得todo,增加todo,修改todo,删除todo。这里关于解析路由参数,我们使用了httprouter.Params的ByName函数。

package main

import (
  "fmt"
  "github.com/julienschmidt/httprouter"
  "net/http"
  "log"
  "io"
  "io/ioutil"
)

func getTodoById(w http.ResponseWriter, r *http.Request, params httprouter.Params){
  todoid := params.ByName("todoid")
  fmt.Fprintf(w, "getTodo %s\n", todoid)
}

func addTodo(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
  body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  fmt.Fprintf(w, "addTodo! %s\n",body)
}

func deleteTodo(w http.ResponseWriter, r *http.Request, params httprouter.Params){
  todoid := params.ByName("todoid")
  fmt.Fprintf(w, "deleteTodo %s\n", todoid)
}

func modifyTodo(w http.ResponseWriter, r *http.Request, params httprouter.Params){
  todoid := params.ByName("todoid")
  body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  fmt.Fprintf(w, "modifyTodo %s to %s\n", todoid, body)
}

func main() {
    router := httprouter.New()
    router.GET("/api/todo/:todoid", getTodoById)
    router.POST("/api/todo/", addTodo)
    router.DELETE("/api/todo/:todoid", deleteTodo)
    router.PUT("/api/todo/:todoid", modifyTodo)
    log.Fatal(http.ListenAndServe(":8080", router))
}

我们可以用curl来测试一下我们的api,以put为例

curl --data "content=shopping&time=tomorrow" http://127.0.0.1:8080/api/todo/123 -X PUT

// modifyTodo 123 to content=shopping&time=tomorrow

json的解析

我们在使用restful api的时候,常常需要给后台传递数据。从上面可以看到,我们通过http.Request的Body属性可以获得数据

body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))

从上面,我们读出的数据是[]byte,但是我们希望将其解析为对象,那么在这之前,我们需要先定义我们的struct。假设我们的todo只有一个字段,就是Name

type Todo struct {
    Name      string
}

现在我们可以这样解析

var todo Todo;
json.Unmarshal(body, &todo);

model层设计

package main

import (
  "gopkg.in/mgo.v2"
  "fmt"
  "log"
  "gopkg.in/mgo.v2/bson"
)

var session *mgo.Session

func init(){
  session,_ = mgo.Dial("mongodb://127.0.0.1")
}

type Todo struct {
    Name      string
}

func createTodo(t Todo){
  c := session.DB("test").C("todo")
  c.Insert(&t)
}

func queryTodoById(id string){
  c := session.DB("test").C("todo")
  result := Todo{}

  err := c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&result)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println("Todo:", result.Name)
}

func removeTodo(id string){
  c := session.DB("test").C("todo")
  err := c.Remove(bson.M{"_id": bson.ObjectIdHex(id)})
  if err != nil{
    log.Fatal(err)
  }
}

func updateTodo(id string, update interface{}){
  //change := bson.M{"$set": bson.M{"name": "hahaha"}}
  c := session.DB("test").C("todo")
  err := c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, update)
  if err != nil{
    log.Fatal(err)
  }
}

我们定义了Todo的struct,并添加了几种函数。

main

package main

import (
  "fmt"
  "github.com/julienschmidt/httprouter"
  "net/http"
  "log"
  "io"
  "io/ioutil"
  "encoding/json"
  "gopkg.in/mgo.v2/bson"
)

func getTodoById(w http.ResponseWriter, r *http.Request, params httprouter.Params){
  todoid := params.ByName("todoid")
  queryTodoById(todoid)
  fmt.Fprintf(w, "getUser %s\n", todoid)
}

func addTodo(w http.ResponseWriter, r *http.Request, _ httprouter.Params){
  body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  var todo Todo;
  json.Unmarshal(body, &todo);
  createTodo(todo)
  fmt.Fprintf(w, "addUser! %s\n",body)
}

func deleteTodo(w http.ResponseWriter, r *http.Request, params httprouter.Params){
  todoid := params.ByName("todoid")
  removeTodo(todoid)
  fmt.Fprintf(w, "deleteUser %s\n", todoid)
}

func modifyTodo(w http.ResponseWriter, r *http.Request, params httprouter.Params){
  todoid := params.ByName("todoid")
  body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
  var todo Todo
  json.Unmarshal(body, &todo);
  change := bson.M{"$set": bson.M{"name": todo.Name}}
  updateTodo(todoid,change)
  fmt.Fprintf(w, "modifyUser %s to %s\n", todoid, body)
}

func main() {
    router := httprouter.New()
    router.GET("/api/todo/:todoid", getTodoById)
    router.POST("/api/todo/", addTodo)
    router.DELETE("/api/todo/:todoid", deleteTodo)
    router.PUT("/api/todo/:todoid", modifyTodo)
    log.Fatal(http.ListenAndServe(":8080", router))
}
    原文作者:bigtom
    原文地址: https://www.jianshu.com/p/a3f52b06d13d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞