作者 yangfu

用例模板

... ... @@ -4,7 +4,7 @@ model:
.PHONY: api
api:
goctl api go -api .\cmd\discuss\mini\dsl\core.api -dir cmd/discuss/api -style go_zero
goctl api go -api .\cmd\discuss\api\dsl\core.api -dir cmd/discuss/api -style go_zero
.PHONY: swagger
swagger:
... ...
... ... @@ -46,7 +46,6 @@ func main() {
})
opts = append(opts, opt)
server := rest.MustNewServer(c.RestConf, opts...)
defer server.Stop()
... ...
import "core/test.api"
\ No newline at end of file
import "core/comment.api"
\ No newline at end of file
... ...
... ... @@ -15,41 +15,65 @@
"application/json"
],
"paths": {
"v1/health": {
"v1/mini/comment": {
"get": {
"summary": "健康",
"operationId": "miniHealth",
"summary": "小程序评论",
"operationId": "miniComment",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/MiniHealthResposne"
"$ref": "#/definitions/CommentResposne"
}
}
},
"requestBody": {},
"tags": [
"tool"
"comment"
]
}
},
"v1/system/comment": {
"get": {
"summary": "系统评论",
"operationId": "systemComment",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/CommentResposne"
}
}
},
"requestBody": {},
"tags": [
"comment"
]
}
}
},
"definitions": {
"MiniHealthRequest": {
"Comment": {
"type": "object",
"title": "Comment"
},
"CommentRequest": {
"type": "object",
"title": "MiniHealthRequest"
"title": "CommentRequest"
},
"MiniHealthResposne": {
"CommentResposne": {
"type": "object",
"properties": {
"ok": {
"type": "boolean",
"format": "boolean"
"list": {
"type": "array",
"items": {
"$ref": "#/definitions/Comment"
}
}
},
"title": "MiniHealthResposne",
"title": "CommentResposne",
"required": [
"ok"
"list"
]
}
},
... ...
... ... @@ -3,26 +3,46 @@ syntax = "v1"
info(
title: "天联鹰蜓"
desc: "天联鹰蜓"
author: "小火箭"
author: "email"
email: "email"
version: "v1"
)
// 小程序接口
@server(
prefix: v1
group: comment
jwt: MiniAuth
)
service Core {
@doc "小程序评论"
@handler miniComment
get /mini/comment (CommentRequest) returns (CommentResposne)
}
// 后台接口
@server(
prefix: v1
group: tool
group: comment
jwt: SystemAuth
)
service Core {
@doc "健康"
@handler miniHealth
get /health (MiniHealthRequest) returns (MiniHealthResposne)
@doc "系统评论"
@handler systemComment
get /system/comment (CommentRequest) returns (CommentResposne)
}
type(
MiniHealthRequest struct{
CommentRequest struct{
}
MiniHealthResposne struct{
Ok bool `json:"ok"`
CommentResposne struct{
List []Comment `json:"list"`
}
Comment struct{
}
)
\ No newline at end of file
... ...
... ... @@ -10,9 +10,13 @@ Log:
MaxSize: 1 # 2MB
TimeFormat: 2006-01-02 15:04:05.000
JwtAuth:
AccessSecret: digital-platform
Expire: 360000
SystemAuth:
AccessSecret: discuss-secret
AccessExpire: 360000
MiniAuth:
AccessSecret: discuss-secret
AccessExpire: 360000
Redis:
Host: 127.0.0.1:6379
... ...
... ... @@ -9,5 +9,7 @@ import (
type Config struct {
rest.RestConf
config.Config
Redis redis.RedisConf `json:",optional"`
Redis redis.RedisConf `json:",optional"`
SystemAuth config.Auth
MiniAuth config.Auth
}
... ...
package tool
package comment
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/tool"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/comment"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniHealthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func MiniCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniHealthRequest
var req types.CommentRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := tool.NewMiniHealthLogic(r.Context(), svcCtx)
resp, err := l.MiniHealth(&req)
result.HttpResult(r, w, resp, err)
l := comment.NewMiniCommentLogic(r.Context(), svcCtx)
resp, err := l.MiniComment(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package comment
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/comment"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func SystemCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.CommentRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := comment.NewSystemCommentLogic(r.Context(), svcCtx)
resp, err := l.SystemComment(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
... ... @@ -4,7 +4,7 @@ package handler
import (
"net/http"
tool "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/tool"
comment "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/comment"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"github.com/zeromicro/go-zero/rest"
... ... @@ -15,10 +15,23 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{
{
Method: http.MethodGet,
Path: "/health",
Handler: tool.MiniHealthHandler(serverCtx),
Path: "/mini/comment",
Handler: comment.MiniCommentHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/system/comment",
Handler: comment.SystemCommentHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
}
... ...
package tool
package comment
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -11,31 +9,22 @@ import (
"github.com/zeromicro/go-zero/core/logx"
)
type MiniHealthLogic struct {
type MiniCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniHealthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniHealthLogic {
return &MiniHealthLogic{
func NewMiniCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniCommentLogic {
return &MiniCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniHealthLogic) MiniHealth(req *types.MiniHealthRequest) (resp *types.MiniHealthResposne, err error) {
// 普通查询
var conn = l.svcCtx.DefaultDBConn()
l.svcCtx.CommentRepository.FindOne(l.ctx, conn, 0)
func (l *MiniCommentLogic) MiniComment(req *types.CommentRequest) (resp *types.CommentResposne, err error) {
// todo: add your logic here and delete this line
// 事务查询
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
l.svcCtx.CommentRepository.FindOne(ctx, l.svcCtx.DefaultDBConn(), 0)
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("健康检查失败", err)
}
return
}
... ...
package comment
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemCommentLogic {
return &SystemCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemCommentLogic) SystemComment(req *types.CommentRequest) (resp *types.CommentResposne, err error) {
// todo: add your logic here and delete this line
return
}
... ...
// Code generated by goctl. DO NOT EDIT.
package types
type MiniHealthRequest struct {
type CommentRequest struct {
}
type MiniHealthResposne struct {
Ok bool `json:"ok"`
type CommentResposne struct {
List []Comment `json:"list"`
}
type Comment struct {
Id int64 `json:"id"`
Content string `json:"content"`
}
... ...
... ... @@ -2,36 +2,21 @@ package config
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
"time"
)
type JWT struct {
Secret string `json:",optional"`
Expires time.Duration `json:",optional"`
}
type JwtAuth struct {
type Auth struct {
AccessSecret string
Expire int64
AccessExpire int64
}
type Config struct {
JwtAuth JwtAuth `json:",optional"`
UserRpc zrpc.RpcClientConf `json:",optional"`
AuthRpc zrpc.RpcClientConf `json:",optional"`
PostRpc zrpc.RpcClientConf `json:",optional"`
CommentRpc zrpc.RpcClientConf `json:",optional"`
JWT JWT `json:",optional"`
DB struct {
DB struct {
DataSource string
} `json:",optional"`
Cache cache.CacheConf `json:",optional"`
DTM DTM `json:",optional"`
Sms Sms `json:",optional"`
Oss Oss `json:",optional"`
Wechat Wechat `json:",optional"` // 学员端微信
CoachClient Wechat `json:",optional"` // 教练端微信
OfficialAccount Wechat `json:",optional"`
ThirdWechatApps []Wechat `json:",optional"`
Cache cache.CacheConf `json:",optional"`
DTM DTM `json:",optional"`
Sms Sms `json:",optional"`
Oss Oss `json:",optional"`
Wechat Wechat `json:",optional"`
}
type DTM struct {
... ...