写点什么

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

作者:六月的
  • 2022-10-19
    上海
  • 本文字数:2116 字

    阅读完需:约 1 分钟

0、转载

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

0.1 源码地址

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

1、创建项目目录

mkdir go-zero-coursewarecd 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 -vgoctl version 1.4.0 darwin/amd64 # 看到版本说明安装成功
复制代码

3、创建服务

创建服务目录


mkdir -p user/apimkdir -p user/rpcmkdir -p courseware/apimkdir -p courseware/rpc
复制代码


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


touch user/api/user.apitouch user/rpc/user.prototouch courseware/api/courseware.apitouch 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);}
复制代码


用户头像

六月的

关注

还未添加个人签名 2019-07-23 加入

还未添加个人简介

评论

发布
暂无评论
go-zero docker-compose 搭建课件服务(一):编写服务api和proto_Docker-compose_六月的_InfoQ写作社区