起步
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帶參數測試