zoukankan      html  css  js  c++  java
  • 2.搭建第一个http服务:三层架构

    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)
    }
    */
    
    




  • 相关阅读:
    VS Code 调试报错
    Nginx反向代理设置
    Nginx 的配置文件
    Nginx 的常用的命令
    CentOS7安装Nginx
    Docker配置
    Centos7 安装MySQL 5.7
    限制Redis使用的最大内存
    C#操作Redis
    Font Awesome 字体图标
  • 原文地址:https://www.cnblogs.com/hualou/p/12076332.html
Copyright © 2011-2022 走看看