Skip to content

Commit

Permalink
feat(api): refine gz-template impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice52 committed Dec 20, 2024
1 parent 1dff825 commit 0697dfd
Show file tree
Hide file tree
Showing 107 changed files with 3,012 additions and 515 deletions.
13 changes: 13 additions & 0 deletions .deploy/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.1'
services:
api-api-admin-dev:
image: registry.cn-shanghai.aliyuncs.com/alice52/wpp-api-admin-go1.20.x:20240530.9953bdb
restart: 'always'
container_name: api-api-admin-dev
volumes:
- /root/wpp/api-admin/log:/app/log
environment:
JASYPT_ENCRYPTOR_PASSWORD: ZACK_KEY
config: etc/admin-api-staging.yaml
extra_hosts:
- 'host.docker.internal:172.17.0.1'
18 changes: 12 additions & 6 deletions .deploy/goctl/1.6.3/api/handler.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package {{.PkgName}}

import (
"net/http"
"github.com/micro-services-roadmap/oneid-core/model"

"github.com/zeromicro/go-zero/rest/httpx"
{{.ImportPackages}}
Expand All @@ -11,16 +12,21 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
{{if .HasRequest}}var req types.{{.RequestType}}
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
httpx.OkJsonCtx(r.Context(), w, model.FailWithError(err))
return
}

{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})

if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
{{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, resp){{else}}httpx.Ok(w){{end}}
}
if v, ok := err.(*model.CodeError); ok {
httpx.OkJsonCtx(r.Context(), w, model.Res(v.Code, nil, v.Msg))
} else {
httpx.OkJsonCtx(r.Context(), w, model.FailWithError(err))
}
} else {
httpx.OkJsonCtx(r.Context(), w, {{if .HasResp}}model.OkWithData(resp){{else}}model.Ok(){{end}})
}
}
}
2 changes: 1 addition & 1 deletion .deploy/goctl/1.6.3/rpc/logic-func.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{if .hasComment}}{{.comment}}{{end}}
func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}},stream {{.streamBody}}{{end}}{{else}}stream {{.streamBody}}{{end}}) ({{if .hasReply}}{{.response}},{{end}} error) {
// todo: add your logic here and delete this line
return {{if .hasReply}}&{{.responseType}}{},{{end}} nil
}
3 changes: 3 additions & 0 deletions .deploy/goctl/1.6.3/rpc/logic.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package {{.packageName}}

import (
"context"
"github.com/micro-services-roadmap/kit-common/util/copier"

{{.imports}}

"github.com/wordpress-plus/rpc-pms/source/gen/dal"
"github.com/wordpress-plus/rpc-pms/source/gen/model"
"github.com/zeromicro/go-zero/core/logx"
)

Expand Down
11 changes: 10 additions & 1 deletion .deploy/goctl/1.6.3/rpc/main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package main
import (
"flag"
"fmt"
"github.com/micro-services-roadmap/kit-common"
"github.com/micro-services-roadmap/kit-common/gz/mw"
"github.com/micro-services-roadmap/kit-common/kg"

{{.imports}}

"github.com/wordpress-plus/rpc-pms/source/gen/dal"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
Expand All @@ -15,9 +19,13 @@ import (

var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file")

func init() {
kit.Init(*configFile)
dal.SetDefault(kg.DB)
}

func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
Expand All @@ -29,6 +37,7 @@ func main() {
reflection.Register(grpcServer)
}
})
s.AddUnaryInterceptors(mw.LoggerInterceptor)
defer s.Stop()

fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
Expand Down
9 changes: 9 additions & 0 deletions .deploy/goctl/1.6.5/api/config.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

import {{.authImport}}

type Config struct {
rest.RestConf
{{.auth}}
{{.jwtTrans}}
}
17 changes: 17 additions & 0 deletions .deploy/goctl/1.6.5/api/context.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package svc

import (
{{.configImport}}
)

type ServiceContext struct {
Config {{.config}}
{{.middleware}}
}

func NewServiceContext(c {{.config}}) *ServiceContext {
return &ServiceContext{
Config: c,
{{.middlewareAssignment}}
}
}
3 changes: 3 additions & 0 deletions .deploy/goctl/1.6.5/api/etc.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name: {{.serviceName}}
Host: {{.host}}
Port: {{.port}}
35 changes: 35 additions & 0 deletions .deploy/goctl/1.6.5/api/handler.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package {{.PkgName}}

import (
"net/http"
"github.com/micro-services-roadmap/kit-common/errorx"
"github.com/micro-services-roadmap/oneid-core/modelo"

"github.com/zeromicro/go-zero/rest/httpx"
{{.ImportPackages}}
)

func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
{{if .HasRequest}}var req types.{{.RequestType}}
if err := httpx.Parse(r, &req); err != nil {
httpx.WriteJsonCtx(r.Context(), w, http.StatusBadRequest, modelo.Res(999_999, nil, err.Error()))
return
}

{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx, r)
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})

if err != nil {
l.Logger.Errorf("Handler error: ", err)
err = errorx.GrpcError(err)
if v, ok := err.(*modelo.CodeError); ok {
httpx.WriteJsonCtx(r.Context(), w, http.StatusBadRequest, modelo.Res(v.Code, nil, v.Error()))
} else {
httpx.WriteJsonCtx(r.Context(), w, http.StatusBadRequest, modelo.Res(888_888, nil, err.Error()))
}
} else {
httpx.OkJsonCtx(r.Context(), w, {{if .HasResp}}resp{{else}}nil{{end}})
}
}
}
29 changes: 29 additions & 0 deletions .deploy/goctl/1.6.5/api/logic.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package {{.pkgName}}

import (
{{.imports}}
"net/http"
)

type {{.logic}} struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
req *http.Request
}

{{if .hasDoc}}{{.doc}}{{end}}
func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext, req *http.Request) *{{.logic}} {
return &{{.logic}}{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
req: req,
}
}

func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
// todo: add your logic here and delete this line
{{.returnString}}
}
26 changes: 26 additions & 0 deletions .deploy/goctl/1.6.5/api/main.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"flag"
"fmt"

{{.importPackages}}
)

var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file")

func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
19 changes: 19 additions & 0 deletions .deploy/goctl/1.6.5/api/middleware.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middleware

import "net/http"

type {{.name}} struct {
}

func New{{.name}}() *{{.name}} {
return &{{.name}}{}
}

func (m *{{.name}})Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// TODO generate middleware implement function, delete after code implementation
// Passthrough to next handler if need
next(w, r)
}
}
4 changes: 4 additions & 0 deletions .deploy/goctl/1.6.5/api/route-addition.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

server.AddRoutes(
{{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}} {{.maxBytes}}
)
13 changes: 13 additions & 0 deletions .deploy/goctl/1.6.5/api/routes.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Code generated by goctl. DO NOT EDIT.
package handler

import (
"net/http"{{if .hasTimeout}}
"time"{{end}}

{{.importPackages}}
)

func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
{{.routesAdditions}}
}
24 changes: 24 additions & 0 deletions .deploy/goctl/1.6.5/api/template.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syntax = "v1"

info (
title: // TODO: add title
desc: // TODO: add description
author: "{{.gitUser}}"
email: "{{.gitEmail}}"
)

type request {
// TODO: add members here and delete this comment
}

type response {
// TODO: add members here and delete this comment
}

service {{.serviceName}} {
@handler GetUser // TODO: set handler name and delete this comment
get /users/id/:userId(request) returns(response)
@handler CreateUser // TODO: set handler name and delete this comment
post /users/create(request)
}
6 changes: 6 additions & 0 deletions .deploy/goctl/1.6.5/api/types.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Code generated by goctl. DO NOT EDIT.
package types{{if .containsTime}}
import (
"time"
){{end}}
{{.types}}
33 changes: 33 additions & 0 deletions .deploy/goctl/1.6.5/docker/docker.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM golang:{{.Version}}alpine AS builder

LABEL stage=gobuilder

ENV CGO_ENABLED 0
{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
{{end}}{{if .HasTimezone}}
RUN apk update --no-cache && apk add --no-cache tzdata
{{end}}
WORKDIR /build

ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
{{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoMainFrom}}


FROM {{.BaseImage}}

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
{{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}}
ENV TZ {{.Timezone}}
{{end}}
WORKDIR /app
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
COPY --from=builder /app/etc /app/etc{{end}}
{{if .HasPort}}
EXPOSE {{.Port}}
{{end}}
CMD ["./{{.ExeFile}}"{{.Argument}}]
18 changes: 18 additions & 0 deletions .deploy/goctl/1.6.5/gateway/etc.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Name: gateway-example # gateway name
Host: localhost # gateway host
Port: 8888 # gateway port
Upstreams: # upstreams
- Grpc: # grpc upstream
Target: 0.0.0.0:8080 # grpc target,the direct grpc server address,for only one node
# Endpoints: [0.0.0.0:8080,192.168.120.1:8080] # grpc endpoints, the grpc server address list, for multiple nodes
# Etcd: # etcd config, if you want to use etcd to discover the grpc server address
# Hosts: [127.0.0.1:2378,127.0.0.1:2379] # etcd hosts
# Key: greet.grpc # the discovery key
# protoset mode
ProtoSets:
- hello.pb
# Mappings can also be written in proto options
# Mappings: # routes mapping
# - Method: get
# Path: /ping
# RpcPath: hello.Hello/Ping
20 changes: 20 additions & 0 deletions .deploy/goctl/1.6.5/gateway/main.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"flag"

"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/gateway"
)

var configFile = flag.String("f", "etc/gateway.yaml", "config file")

func main() {
flag.Parse()
var c gateway.GatewayConf
conf.MustLoad(*configFile, &c)
gw := gateway.MustNewServer(c)
defer gw.Stop()
gw.Start()
}
Loading

0 comments on commit 0697dfd

Please sign in to comment.