Skip to content

Commit 44ec318

Browse files
committed
feat(jzero): refactor frame
feat(jzero): refactor frame
1 parent 7afd821 commit 44ec318

25 files changed

Lines changed: 273 additions & 350 deletions

.github/workflows/upload.yaml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ jobs:
9292
9393
cd ..
9494
95+
rm -rf simplegateway-with-model-redis
96+
jzero new simplegateway-with-model-redis --frame gateway --features model,redis
97+
cd simplegateway-with-model-redis
98+
go mod tidy
99+
jzero gen swagger
100+
jzero gen sdk
101+
jzero gen zrpcclient
102+
jzero format --git-change=false
103+
go mod tidy && go mod download
104+
go run main.go version
105+
git add .
106+
git diff-index --quiet HEAD || git commit -m "chore(simplegateway-with-model-redis): update simplegateway-with-model-redis"
107+
git push -f
108+
109+
cd ..
110+
95111
rm -rf simplerpc
96112
jzero new simplerpc --frame rpc
97113
cd simplerpc
@@ -110,4 +126,17 @@ jobs:
110126
git diff-index --quiet HEAD || git commit -m "chore(simplerpc): update simplerpc"
111127
git push -f
112128
113-
cd ..
129+
cd ..
130+
131+
rm -rf simplerpc-with-model-redis
132+
jzero new simplerpc-with-model-redis --frame rpc --features model,redis
133+
cd simplerpc-with-model-redis
134+
go mod tidy
135+
jzero gen swagger
136+
jzero gen zrpcclient
137+
jzero format --git-change=false
138+
go mod tidy && go mod download
139+
go run main.go version
140+
git add .
141+
git diff-index --quiet HEAD || git commit -m "chore(simplerpc-with-model-redis): update simplerpc-with-model-redis"
142+
git push -f

cmd/jzero/.template/frame/api/app/cmd/root.go.tpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
var cfgFile string
10-
119
// rootCmd represents the base command when called without any subcommands
1210
var rootCmd = &cobra.Command{
1311
Use: "{{ .APP }}",
@@ -28,5 +26,5 @@ func Execute() {
2826
}
2927

3028
func init() {
31-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "etc/etc.yaml", "config file (default is project root dir etc/etc.yaml")
29+
rootCmd.PersistentFlags().String("config", "etc/etc.yaml", "config file (default is project root dir etc/etc.yaml")
3230
}

cmd/jzero/.template/frame/api/app/cmd/server.go.tpl

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package cmd
22

33
import (
4-
"net/http"
5-
64
"github.com/common-nighthawk/go-figure"
5+
"github.com/jzero-io/jzero/core/configcenter"
76
"github.com/jzero-io/jzero/core/configcenter/subscriber"
87
"github.com/spf13/cobra"
9-
configurator "github.com/zeromicro/go-zero/core/configcenter"
108
"github.com/zeromicro/go-zero/core/logx"
119
"github.com/zeromicro/go-zero/core/service"
1210
"github.com/zeromicro/go-zero/rest"
13-
"github.com/zeromicro/go-zero/rest/httpx"
1411

1512
"{{ .Module }}/internal/config"
1613
"{{ .Module }}/internal/custom"
@@ -26,51 +23,39 @@ var serverCmd = &cobra.Command{
2623
Short: "{{ .APP }} server",
2724
Long: "{{ .APP }} server",
2825
Run: func(cmd *cobra.Command, args []string) {
29-
cc := configurator.MustNewConfigCenter[config.Config](configurator.Config{
26+
cc := configcenter.MustNewConfigCenter[config.Config](configcenter.Config{
3027
Type: "yaml",
31-
}, subscriber.MustNewFsnotifySubscriber(cfgFile, subscriber.WithUseEnv(true)))
32-
33-
c, err := cc.GetConfig()
34-
logx.Must(err)
28+
}, subscriber.MustNewFsnotifySubscriber(cmd.Flag("config").Value.String(), subscriber.WithUseEnv(true)))
3529

3630
// set up logger
37-
if err = logx.SetUp(c.Log.LogConf); err != nil {
38-
logx.Must(err)
39-
}
31+
logx.Must(logx.SetUp(cc.MustGetConfig().Log.LogConf))
4032

4133
// print banner
42-
printBanner(c)
34+
printBanner(cc.MustGetConfig().Banner)
4335
// print version
4436
printVersion()
4537

4638
svcCtx := svc.NewServiceContext(cc)
47-
svcCtx.Middleware = middleware.NewMiddleware()
48-
global.ServiceContext = *svcCtx
49-
run(svcCtx)
50-
},
51-
}
52-
53-
func run(svcCtx *svc.ServiceContext) {
54-
server := rest.MustNewServer(svcCtx.MustGetConfig().Rest.RestConf)
5539

56-
ctm := custom.New(server)
57-
ctm.Init()
40+
restServer := rest.MustNewServer(svcCtx.ConfigCenter.MustGetConfig().Rest.RestConf)
41+
customServer := custom.New()
5842

59-
handler.RegisterHandlers(server, svcCtx)
60-
middleware.Register(server)
43+
handler.RegisterHandlers(restServer, svcCtx)
44+
middleware.Register(restServer)
6145

62-
{{ if not .Serverless }}// load plugins
63-
plugins.LoadPlugins(server, svcCtx){{end}}
46+
{{ if not .Serverless }}// load plugins
47+
plugins.LoadPlugins(restServer, svcCtx){{end}}
6448

65-
group := service.NewServiceGroup()
66-
group.Add(server)
67-
group.Add(ctm)
49+
group := service.NewServiceGroup()
50+
group.Add(restServer)
51+
group.Add(customServer)
6852

69-
group.Start()
53+
group.Start()
54+
},
7055
}
7156

72-
func printBanner(c config.Config) {
73-
figure.NewColorFigure(c.Banner.Text, c.Banner.FontName, c.Banner.Color, true).Print()
57+
func printBanner(c config.BannerConf) {
58+
figure.NewColorFigure(c.Text, c.FontName, c.Color, true).Print()
7459
}
7560

7661
func init() {

cmd/jzero/.template/frame/api/app/desc/api/v1/version.api.tpl renamed to cmd/jzero/.template/frame/api/app/desc/api/builtin.api.tpl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{{ if .Serverless }}{{else}}syntax = "v1"
22

33
info (
4-
go_package: "version"
5-
version: "v1"
4+
go_package: "builtin"
65
WrapCodeMsg: true
76
)
87

@@ -16,10 +15,14 @@ type VersionResponse {
1615
}
1716

1817
@server(
19-
prefix: /api/v1
20-
group: version
18+
group: builtin
19+
compact_handler: true
20+
compact_logic: true
2121
)
2222
service {{ .APP | ToCamel }} {
2323
@handler Version
2424
get /version (VersionRequest) returns (VersionResponse)
25+
26+
@handler Swagger
27+
get /swagger
2528
}{{end}}

cmd/jzero/.template/frame/api/app/internal/custom/custom.go.tpl

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
package custom
22

3-
import (
4-
"github.com/zeromicro/go-zero/rest"
5-
)
3+
type Custom struct{}
64

7-
type Custom struct {
8-
Server *rest.Server
9-
}
10-
11-
func New(server *rest.Server) *Custom {
12-
return &Custom{
13-
Server: server,
14-
}
15-
}
16-
17-
// Init Please add custom logic here.
18-
func (c *Custom) Init() {
19-
c.AddRoutes(c.Server)
5+
func New() *Custom {
6+
return &Custom{}
207
}
218

229
// Start Please add custom logic here.

cmd/jzero/.template/frame/api/app/internal/custom/routes.go.tpl

Lines changed: 0 additions & 14 deletions
This file was deleted.

cmd/jzero/.template/frame/api/app/internal/global/global.go.tpl

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package builtin
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/jzero-io/jzero/core/swaggerv2"
8+
"github.com/zeromicro/go-zero/core/logx"
9+
10+
"{{.Module}}/internal/svc"
11+
types "{{.Module}}/internal/types/builtin"
12+
)
13+
14+
type Swagger struct {
15+
logx.Logger
16+
ctx context.Context
17+
svcCtx *svc.ServiceContext
18+
r *http.Request
19+
w http.ResponseWriter
20+
}
21+
22+
func NewSwagger(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request, w http.ResponseWriter) *Swagger {
23+
return &Swagger{
24+
Logger: logx.WithContext(ctx),
25+
ctx: ctx,
26+
svcCtx: svcCtx,
27+
r: r,
28+
w: w,
29+
}
30+
}
31+
32+
func (l *Swagger) Swagger() error {
33+
opts := new(swaggerv2.Swaggerv2Opts).DefaultOptions()
34+
swaggerv2.SwaggerHandler(opts, l.w, l.r)
35+
return nil
36+
}
37+
38+
type Version struct {
39+
logx.Logger
40+
ctx context.Context
41+
svcCtx *svc.ServiceContext
42+
r *http.Request
43+
}
44+
45+
func NewVersion(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request) *Version {
46+
return &Version{
47+
Logger: logx.WithContext(ctx),
48+
ctx: ctx,
49+
svcCtx: svcCtx,
50+
r: r,
51+
}
52+
}
53+
54+
func (l *Version) Version(req *types.VersionRequest) (resp *types.VersionResponse, err error) {
55+
return &types.VersionResponse{
56+
Version: os.Getenv("VERSION"),
57+
GoVersion: runtime.Version(),
58+
Commit: os.Getenv("COMMIT"),
59+
Date: os.Getenv("DATE"),
60+
}, nil
61+
}

cmd/jzero/.template/frame/api/app/internal/logic/version/version.go.tpl

Lines changed: 0 additions & 38 deletions
This file was deleted.

cmd/jzero/.template/frame/api/app/internal/middleware/error.go.tpl

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)