go-zero docker-compose 搭建课件服务(三):编写 courseware api 服务
作者:六月的
- 2022-10-19 上海
本文字数:2507 字
阅读完需:约 1 分钟
0、转载
go-zero docker-compose 搭建课件服务(三):编写courseware api服务
0.1 源码地址
https://github.com/liuyuede123/go-zero-courseware
1、生成 api 相关文件
# 到之前创建的courseware/api目录下
goctl api go -api courseware.api -dir . -style gozero
复制代码
2、courseware/api/etc/user.yaml 下增加 rpc 配置
Name: courseware
Host: 0.0.0.0
Port: 8400
CoursewareRpc:
Etcd:
Hosts:
- etcd:2379
Key: courseware.rpc
复制代码
3、courseware/api/internal/config/config.go 下增加 CoursewareRpc
package config
import (
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
rest.RestConf
CoursewareRpc zrpc.RpcClientConf
}
复制代码
3、courseware/api/internal/svc/servicecontext.go 下增加 CoursewareRpc 配置
package svc
import (
"github.com/zeromicro/go-zero/zrpc"
"go-zero-courseware/courseware/api/internal/config"
"go-zero-courseware/courseware/rpc/coursewareclient"
)
type ServiceContext struct {
Config config.Config
CoursewareRpc coursewareclient.Courseware
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
CoursewareRpc: coursewareclient.NewCourseware(zrpc.MustNewClient(c.CoursewareRpc)),
}
}
复制代码
4、补充增删改查相关接口逻辑
courseware/api/internal/logic/coursewareaddlogic.go
package logic
import (
"context"
"go-zero-courseware/courseware/rpc/coursewareclient"
"go-zero-courseware/courseware/api/internal/svc"
"go-zero-courseware/courseware/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CoursewareAddLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCoursewareAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CoursewareAddLogic {
return &CoursewareAddLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CoursewareAddLogic) CoursewareAdd(req *types.AddRequest) (resp *types.AddResponse, err error) {
_, err = l.svcCtx.CoursewareRpc.Add(l.ctx, &coursewareclient.AddRequest{
Code: req.Code,
Name: req.Name,
Type: req.Type,
})
if err != nil {
return nil, err
}
return &types.AddResponse{}, nil
}
复制代码
courseware/api/internal/logic/coursewaredeletelogic.go
package logic
import (
"context"
"go-zero-courseware/courseware/rpc/coursewareclient"
"go-zero-courseware/courseware/api/internal/svc"
"go-zero-courseware/courseware/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CoursewareDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCoursewareDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CoursewareDeleteLogic {
return &CoursewareDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CoursewareDeleteLogic) CoursewareDelete(req *types.DeleteRequest) (resp *types.DeleteResponse, err error) {
_, err = l.svcCtx.CoursewareRpc.Delete(l.ctx, &coursewareclient.DeleteRequest{
Id: req.Id,
})
if err != nil {
return nil, err
}
return &types.DeleteResponse{}, nil
}
复制代码
courseware/api/internal/logic/coursewaregetlogic.go
package logic
import (
"context"
"go-zero-courseware/courseware/rpc/coursewareclient"
"go-zero-courseware/courseware/api/internal/svc"
"go-zero-courseware/courseware/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CoursewareGetLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCoursewareGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CoursewareGetLogic {
return &CoursewareGetLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CoursewareGetLogic) CoursewareGet(req *types.GetRequest) (resp *types.GetResponse, err error) {
cw, err := l.svcCtx.CoursewareRpc.Get(l.ctx, &coursewareclient.GetRequest{
Id: req.Id,
})
if err != nil {
return nil, err
}
return &types.GetResponse{
Id: cw.Id,
Code: cw.Code,
Name: cw.Name,
Type: cw.Type,
}, nil
}
复制代码
courseware/api/internal/logic/coursewareupdatelogic.go
package logic
import (
"context"
"go-zero-courseware/courseware/rpc/coursewareclient"
"go-zero-courseware/courseware/api/internal/svc"
"go-zero-courseware/courseware/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CoursewareUpdateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCoursewareUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CoursewareUpdateLogic {
return &CoursewareUpdateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CoursewareUpdateLogic) CoursewareUpdate(req *types.UpdateRequest) (resp *types.UpdateResponse, err error) {
_, err = l.svcCtx.CoursewareRpc.Update(l.ctx, &coursewareclient.UpdateRequest{
Id: req.Id,
Code: req.Code,
Name: req.Name,
Type: req.Type,
})
if err != nil {
return nil, err
}
return &types.UpdateResponse{}, nil
}
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
六月的
关注
还未添加个人签名 2019-07-23 加入
还未添加个人简介
评论