首页 > Web开发 > 详细

2.搭建第一个http服务:三层架构

时间:2019-12-21 13:43:55      阅读:95      评论:0      收藏:0      [点我收藏+]
package main

import (
    "github.com/go-kit/kit/transport/http"
    "gomicro/Services"
)

func main() {
    user := Services.UserService{}
    endp := Services.GenUserEnPoint(user)

    http.NewServer(endp, Services.DecodeUserRequest, Services.EncodeUserResponse) //使用go kit创建server传入我们之前定义的两个解析函数
}
/*
func DecodeUserRequest(c context.Context, r *http.Request) (interface{}, error) { //这个函数决定了使用哪个request来请求
    if r.URL.Query().Get("uid") != "" { //request会先进入这个函数去解析决定使用哪个request结构体来请求
        uid, _ := strconv.Atoi(r.URL.Query().Get("uid"))
        return UserRequest{Uid: uid}, nil
    }
    return nil,errors.New("参数错误")
}

func EncodeUserResponse(ctx context.Context,w http.ResponseWriter,response interface{}) error{
    w.Header().Set("Content-type","application/json") //设置响应格式为json,这样客户端接收到的值就是json,就是把我们设置的UserResponse给json化了
    return json.NewEncoder(w).Encode(response)
}
*/





2.搭建第一个http服务:三层架构

原文:https://www.cnblogs.com/hualou/p/12076332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!