0、转载

go-zero docker-compose 搭建课件服务(一):编写服务api和proto

0.1源码地址

https://github.com/liuyuede123/go-zero-courseware

1、创建项目目录

mkdir go-zero-courseware
cd go-zero-courseware

2、安装goctl

# 安装
GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest

# mac放到/usr/local/bin/下面
ln -s ~/go/bin/goctl /usr/local/bin/goctl

# 校验
goctl -v
goctl version 1.4.0 darwin/amd64 # 看到版本说明安装成功

3、创建服务

创建服务目录

mkdir -p user/api
mkdir -p user/rpc
mkdir -p courseware/api
mkdir -p courseware/rpc

创建user.api、courseware.api、user.proto、courseware.proto

touch user/api/user.api
touch user/rpc/user.proto
touch courseware/api/courseware.api
touch courseware/rpc/courseware.proto

4、编写user.api、user.proto

type (
    // 登录
    LoginRequest {
        LoginName string `json:"loginName"`
        Password  string `json:"password"`
    }
    LoginResponse {
        Id    uint64 `json:"id"`
        Token string `json:"token"`
    }

    // 注册
    RegisterRequest {
        LoginName string `json:"loginName"`
        Username  string `json:"username"`
        Password  string `json:"password"`
        Sex       uint64 `json:"sex"`
    }
    RegisterResponse {
    }

    // 用户信息
    UserInfoRequest {
        Id uint64 `json:"id"`
    }
    UserInfoResponse {
        Id        uint64 `json:"id"`
        Username  string `json:"username"`
        LoginName string `json:"loginName"`
        Sex       uint64 `json:"sex"`
    }
)

service user {
    @handler userLogin
    post /api/user/login (LoginRequest) returns (LoginResponse)
    
    @handler userRegister
    post /api/user/register (RegisterRequest) returns (RegisterResponse)
    
    @handler userInfo
    post /api/user/userInfo (UserInfoRequest) returns (UserInfoResponse)
}
syntax = "proto3";

package user;
option go_package = "./user";

// 登录
message  LoginRequest {
  string LoginName = 1;
  string  Password = 2;
}
message LoginResponse {
  uint64 Id = 1;
  string Token = 2;
}

// 注册
message RegisterRequest {
  string LoginName = 1;
  string Username = 2;
  string Password = 3;
  uint64 Sex = 4;
}
message RegisterResponse {
}

// 用户信息
message UserInfoRequest {
  uint64 Id = 1;
}
message UserInfoResponse {
  uint64 Id = 1;
  string Username = 2;
  string LoginName = 3;
  uint64 Sex = 4;
}

service User {
  rpc Login(LoginRequest) returns(LoginResponse);
  rpc Register(RegisterRequest) returns(RegisterResponse);
  rpc UserInfo(UserInfoRequest) returns(UserInfoResponse);
}

5、编写courseware.api、courseware.proto

type (
    // 新增
    AddRequest {
        Code string `json:"code"`
        Name string `json:"name"`
        Type uint64 `json:"type"`
    }
    AddResponse {
    }

    // 更新
    UpdateRequest {
        Id   uint64 `json:"id"`
        Code string `json:"code"`
        Name string `json:"name"`
        Type uint64 `json:"type"`
    }
    UpdateResponse {
    }

    // 查看
    GetRequest {
        Id uint64 `json:"id"`
    }
    GetResponse {
        Id   uint64 `json:"id"`
        Code string `json:"code"`
        Name string `json:"name"`
        Type uint64 `json:"type"`
    }

    // 删除
    DeleteRequest {
        Id uint64 `json:"id"`
    }
    DeleteResponse {
    }
)

service courseware {
    @handler coursewareAdd
    post /api/courseware/add (AddRequest) returns (AddResponse)
    
    @handler coursewareUpdate
    post /api/courseware/update (UpdateRequest) returns (UpdateResponse)
    
    @handler coursewareGet
    post /api/courseware/get (GetRequest) returns (GetResponse)
    
    @handler coursewareDelete
    post /api/courseware/delete (DeleteRequest) returns (DeleteResponse)
}
syntax = "proto3";

package user;
option go_package = "./courseware";

// 新增
message AddRequest {
  string Code = 1;
  string Name = 2;
  uint64 Type = 3;
}
message AddResponse {
}

// 更新
message UpdateRequest {
  uint64 Id = 1;
  string Code = 2;
  string Name = 3;
  uint64 Type = 4;
}
message UpdateResponse {
}

// 查看
message GetRequest {
  uint64 Id = 1;
}
message GetResponse {
  uint64 Id = 1;
  string Code = 2;
  string Name = 3;
  uint64 Type = 4;
}

// 删除
message DeleteRequest {
  uint64 Id = 1;
}
message DeleteResponse {
}

service Courseware {
  rpc Add(AddRequest) returns(AddResponse);
  rpc Update(UpdateRequest) returns(UpdateResponse);
  rpc Get(GetRequest) returns(GetResponse);
  rpc Delete(DeleteRequest) returns(DeleteResponse);
}

liuyuede
15 声望0 粉丝