共计 5510 个字符,预计需要花费 14 分钟才能阅读完成。
转载自:go-zero docker-compose 搭建课件服务(二):编写 courseware rpc 服务
1、创立 model
mysql 中创立数据库
create database go_zero_courseware default character set utf8mb4 collate utf8mb4_unicode_ci;
创立 sql 文件
touch courseware/rpc/model/courseware.sql
CREATE TABLE `courseware`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(255) NOT NULL DEFAULT ''COMMENT' 编号 ',
`name` varchar(255) NOT NULL DEFAULT ''COMMENT' 用户姓名 ',
`type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '课件类型 1-h5 2-scorm 3- 多媒体多章节',
`is_delete` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否删除 0- 未删除 1- 已删除',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_mobile_unique` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
生成 model
# 到对应 model 目录下
cd courseware/rpc/model
# 生成 model
goctl model mysql ddl -src="./*.sql" -dir="./" -c
2、生成 rpc 文件
须要提前装置了 protoc & protoc-gen-go,自行搜寻装置
# 回到 courseware rpc 目录 ./go-zero-courseware/courseware/rpc
cd ../
goctl rpc protoc courseware.proto --go_out=. --go-grpc_out=. --zrpc_out=.
3、初始化 module
# 门路 courseware
go mod init
go mod tidy
4、增加数据库源
courseware.yaml 中减少 mysql 配置
# 门路
# go-zero-courseware/courseware/rpc/etc/courseware.yaml
Name: courseware.rpc
ListenOn: 0.0.0.0:9400
Etcd:
Hosts:
- etcd:2379
Key: courseware.rpc
# mysql 数据源
Mysql:
DataSource: root:liufutian@tcp(192.168.0.110:3306)/go_zero_courseware?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
# redis 缓存
CacheRedis:
- Host: 192.168.0.110:6379
Pass:
config.go 文件中减少 mysql 配置项
// 门路 courseware/rpc/internal/config/config.go
package config
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
zrpc.RpcServerConf
Mysql struct {DataSource string}
CacheRedis cache.CacheConf
}
servicecontext.go 中减少 CoursewareModel 配置
// 门路 courseware/rpc/internal/svc/servicecontext.go
package svc
import (
"github.com/zeromicro/go-zero/core/stores/sqlx"
"go-zero-courseware/courseware/rpc/internal/config"
"go-zero-courseware/courseware/rpc/model"
)
type ServiceContext struct {
Config config.Config
CoursewareModel model.CoursewareModel
}
func NewServiceContext(c config.Config) *ServiceContext {conn := sqlx.NewMysql(c.Mysql.DataSource)
return &ServiceContext{
Config: c,
CoursewareModel: model.NewCoursewareModel(conn, c.CacheRedis),
}
}
5、编写增删改查逻辑
courseware/rpc/internal/logic/addlogic.go 批改如下
package logic
import (
"context"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"go-zero-courseware/courseware/rpc/model"
"google.golang.org/grpc/status"
"github.com/zeromicro/go-zero/core/logx"
)
type AddLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
return &AddLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *AddLogic) Add(in *courseware.AddRequest) (*courseware.AddResponse, error) {_, err := l.svcCtx.CoursewareModel.FindOneByCode(l.ctx, in.Code)
if err == nil {return nil, status.Error(5000, "课件已存在")
}
if err == model.ErrNotFound {
newCourseware := model.Courseware{
Code: in.Code,
Name: in.Name,
Type: in.Type,
}
_, err = l.svcCtx.CoursewareModel.Insert(l.ctx, &newCourseware)
if err != nil {return nil, status.Error(500, err.Error())
}
return &courseware.AddResponse{}, nil}
return nil, status.Error(500, err.Error())
}
courseware/rpc/internal/logic/deletelogic.go 批改如下
package logic
import (
"context"
"google.golang.org/grpc/status"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
return &DeleteLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DeleteLogic) Delete(in *courseware.DeleteRequest) (*courseware.DeleteResponse, error) {err := l.svcCtx.CoursewareModel.Delete(l.ctx, in.Id)
if err != nil {return nil, status.Error(500, err.Error())
}
return &courseware.DeleteResponse{}, nil}
courseware/rpc/internal/logic/getlogic.go 批改如下
package logic
import (
"context"
"google.golang.org/grpc/status"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type GetLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLogic {
return &GetLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetLogic) Get(in *courseware.GetRequest) (*courseware.GetResponse, error) {cw, err := l.svcCtx.CoursewareModel.FindOne(l.ctx, in.Id)
if err != nil {return nil, status.Error(5000, "课件不存在")
}
return &courseware.GetResponse{
Id: cw.Id,
Code: cw.Code,
Name: cw.Name,
Type: cw.Type,
}, nil
}
courseware/rpc/internal/logic/updatelogic.go 批改如下:
package logic
import (
"context"
"google.golang.org/grpc/status"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
return &UpdateLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateLogic) Update(in *courseware.UpdateRequest) (*courseware.UpdateResponse, error) {cw, err := l.svcCtx.CoursewareModel.FindOne(l.ctx, in.Id)
if err != nil {return nil, status.Error(5000, "课件不存在")
}
_, err = l.svcCtx.CoursewareModel.FindOneByCode(l.ctx, in.Code)
if err == nil {return nil, status.Error(5000, "课件编号已存在")
}
if in.Code != "" {cw.Code = in.Code}
if in.Name != "" {cw.Name = in.Name}
if in.Type > 0 {cw.Type = in.Type}
err = l.svcCtx.CoursewareModel.Update(l.ctx, cw)
if err != nil {return nil, status.Error(500, err.Error())
}
return &courseware.UpdateResponse{}, nil}
正文完