0、轉載
go-zero docker-compose 搭建課件服務(一):編寫服務api和proto
0.1原始碼地址
github.com/liuyuede123/go-zero-cou...
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);
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結