网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

go-zero docker-compose 搭建课件服务(三):编写courseware api服务

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
}

网站标题:go-zero docker-compose 搭建课件服务(三):编写courseware api服务
转载注明:http://bjjierui.cn/article/dsoicde.html

其他资讯