golang + restful

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

首先,我們需要解決的是路由的問題,也就是如何將不同的url映射到不同的處理函數(shù)。

    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,經(jīng)過一番調(diào)研,我感覺有幾個http router的庫比較適合:bone, httprouter, mux。

最終我選擇了httprouter這個庫。因為它真的很像express。

基本框架

首先,我們設(shè)計了四個路由,分別為根據(jù)Id獲得todo,增加todo,修改todo,刪除todo。這里關(guān)于解析路由參數(shù),我們使用了httprouter.Params的ByName函數(shù)。

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的時候,常常需要給后臺傳遞數(shù)據(jù)。從上面可以看到,我們通過http.Request的Body屬性可以獲得數(shù)據(jù)

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

從上面,我們讀出的數(shù)據(jù)是[]byte,但是我們希望將其解析為對象,那么在這之前,我們需要先定義我們的struct。假設(shè)我們的todo只有一個字段,就是Name

type Todo struct {
    Name      string
}

現(xiàn)在我們可以這樣解析

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

model層設(shè)計

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,并添加了幾種函數(shù)。

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))
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容