[GO基礎] - web-router-server

起步

func HelloServer1(w http.ResponseWriter, req *http.Request) {

    fmt.Fprint(w,"hello world")
}
func main() {
    http.HandleFunc("/test", HelloServer1)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err.Error())
    }
}

上述代碼簡單實現了一個httpserver,訪問本地127.0.0.1:8080/test返回hello world,但是get、post等都返回一致結果,無法限制訪問方式。

router

router的基本功能是需要限制訪問路徑和訪問方式。
訪問方式一般有如下幾種

  • GET
  • POST
  • DELETE
  • PUT
    ...

仔細查看上述2行代碼:
http.HandleFunc("/test", HelloServer1)是為一個path指定了處理函數,http.ListenAndServe(":8080", nil)則是實現了監聽端口,其第二個參數實際為Handler

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

...

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

so,router的核心就是根據path和method來指向一個預定義的handlerFunc,用map/[ ]來存儲和查找就再適合不過了。

//router
const (
    GET         = iota
    POST
    PUT
    DELETE
    CONNECTIBNG
    HEAD
    OPTIONS
    PATCH
    TRACE
)

func NewRouter() MethodMaps {
    return []handler{
        GET:  make(handler),
        POST: make(handler),
        PUT: make(handler),
        DELETE: make(handler),
    }
}

type MethodMaps [] handler
type handler map[string]HandlerMapped
...
//存取函數.....
...

//簡單server
type SimpleServer struct {
    router MethodMaps
}


type ISimpleServer interface {
    PORT(addr string)
    GET(url string, f HandlerFunc)
    POST(url string, f HandlerFunc)
    PUT(url string, f HandlerFunc)
    DELETE(url string, f HandlerFunc)
}

type HandlerMapped struct {
    f HandlerFunc
}

type HandlerFunc func(w http.ResponseWriter, req *http.Request)

const PAGE_NOT_FOUND = "Page Not Found!"

func Create() *SimpleServer {
    return &SimpleServer{
        router:Router(),
    }
}


func (o *SimpleServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {

    o.doHandler(w,req)
}
//路徑處理相關函數:路徑匹配、路徑增加
...

此時,main函數的內部代碼塊就有了一種node.js中koa-router的意味

func main() {

    uc := new(controller.UserController)
    pc := new(controller.PageController)
    server := SimpleServer.Create()
    server.GET("/test", HelloServer1)
    server.GET("/query", queryHandler)
    server.GET("/test/query", pc.TestQuery)
    server.POST("/json", TestJson)
    server.POST("/user/login", uc.Login)
    server.POST("/user/add", uc.Add)
    server.PORT(":23457")

}

對比下koa中的結構:


koa-router

索性就把controller(未包裝的handler)用類似koa中結構進行構造

package controller

import (
    "net/http"
)

type PageController struct {}

type PageControllerInterface interface {
    Index(HandlerFunc)
    Error(HandlerFunc)
    TestQuery(HandlerFunc)
}

const INDEX_PAGE = "This Is Index Page!"
const ERROR_PAGE = "This Is Error Page!"

type HandlerFunc func(w http.ResponseWriter, req *http.Request)

func (ctl *PageController) Index(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte(INDEX_PAGE))
}

func (ctl *PageController) TestQuery(w http.ResponseWriter, req *http.Request) {
    paramCtx := parameterFactory(req)
    newQueryMap := paramCtx.QueryMap
    p := newQueryMap.Get("p")
    w.Write([]byte(p))
}

func (ctl *PageController) Error(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte(ERROR_PAGE))
}

PageController是基于GET請求查找的頁面handler,不可避免需要考慮帶參數的情況。URL中的參數可以由以下代碼獲得:

QueryMap ,_ := url.ParseQuery(req.URL.RawQuery)

既然要想辦法獲取get的參數,平時使用最多的莫過于post傳遞json參數,獲取方式就比較麻煩了:

func jsonValueHandler(r io.ReadCloser) (map[string]interface{}) {
    respBytes, err := ioutil.ReadAll(r)
    if err != nil {
        fmt.Println(err.Error())
        return nil
    }
    var jsonMap map[string]interface{}
    ok := json.Unmarshal(respBytes, &jsonMap)
    if ok != nil {
        fmt.Println(ok.Error())
        return nil
    }
    return jsonMap
}

入參的io.ReadCloser其實是http.Request.Body。
將上述兩種獲取參數的方法整合起來,就有了ParamCtx這個結構,分別單獨獲取url中的參數、post方法的json參數等

type ParamCtx struct {
    QueryMap url.Values
    PostJsonMap map[string]interface{}
    PostFormMaP url.Values
}

寫到這,又有一個不可避免的問題:json形式的返回數據,結構如下

{
    "code": 200,
    "msg": "SUCCESS",
    "data": null
}

那么就來定義一個新的結構,ApiResult,以及一些常用的方法

type ApiResult struct {
    Code int `json:"code"`
    Msg string `json:"msg"`
    Data map[string]interface{} `json:"data"`
}

type ApiResultInterface interface {
    Success(data map[string]string)
    Error(code int, msg string, data map[string]string)
    Test()
}

最后呢,針對不同的傳參進行測試:


返回json數據

返回json數據

url帶參數測試
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • 暴雨的夜,水粉鋪沒生意關的也早。 林掌柜去碼頭了,早早遣了丫頭們。 張橫波松松地挽了發,提了一盞油燈,慢慢檢點著店...
    蔡灃閱讀 355評論 0 0
  • 前言 彈指之間,八二屆大箕鋪高中畢業,值今己三十六年,感謝劉先海同學組建大箕鋪高中群,讓我們再重逢~~~同窗二載...
    斌部t蛟閱讀 470評論 0 0
  • 提起我的寒假生活,我就興奮,我覺的我的寒假生活是豐富多彩的,因為在我的寒假里,我過得也很充實。 首先,每天早晨,我...
    伊人何處尋閱讀 360評論 0 7