本书未发布

13.8. Rpc式请求

未匹配的标注
package main

/*
rpc扩展函数要求函数类型为:func(eudore.Context, Request) (Response, error)
其中Request类型要求为map、struct、ptr之一,会调用ctx.Bind接续数据,Response会调用ctx.Render返回。
*/

import (
    "github.com/eudore/eudore"
    "github.com/eudore/eudore/component/httptest"
)

type (
    // Request 定义一个请求结构
    Request struct {
        Name string `json:"name"`
        Num  int    `json:"num"`
    }
    // Response 定义一个响应结构
    Response struct {
        Code    int    `json:"code"`
        Message string `json:"message"`
    }
)

func main() {
    app := eudore.NewApp()
    app.AnyFunc("/*", func(ctx eudore.Context, req Request) (Response, error) {
        ctx.Debugf("%#v", req)
        return Response{200, "Success"}, nil
    })

    // 请求测试
    client := httptest.NewClient(app)
    client.NewRequest("PUT", "/").WithBodyJSON(map[string]interface{}{
        "name": "eudore",
        "num":  44,
    }).WithHeaderValue("Accept", " application/json").Do().Out()

    app.Listen(":8088")
    // app.CancelFunc()
    app.Run()
}

反馈和交流请加群组:QQ群373278915

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~