Skip to content

Commit 622f390

Browse files
committed
feat(jzero): update template to support serverless arch
feat(jzero): update template to support serverless arch feat(jzero): update template to support serverless arch
1 parent ed0f8f8 commit 622f390

35 files changed

Lines changed: 388 additions & 115 deletions

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package cmd
22

33
import (
4+
"net/http"
5+
6+
"github.com/common-nighthawk/go-figure"
7+
"github.com/jzero-io/jzero/core/configcenter/subscriber"
48
"github.com/spf13/cobra"
59
configurator "github.com/zeromicro/go-zero/core/configcenter"
6-
"github.com/jzero-io/jzero/core/configcenter/subscriber"
7-
"github.com/zeromicro/go-zero/core/logx"
10+
"github.com/zeromicro/go-zero/core/logx"
811
"github.com/zeromicro/go-zero/core/service"
912
"github.com/zeromicro/go-zero/rest"
10-
"github.com/common-nighthawk/go-figure"
13+
"github.com/zeromicro/go-zero/rest/httpx"
1114

1215
"{{ .Module }}/internal/config"
16+
"{{ .Module }}/internal/custom"
17+
"{{ .Module }}/internal/global"
1318
"{{ .Module }}/internal/middleware"
1419
"{{ .Module }}/internal/handler"
1520
"{{ .Module }}/internal/svc"
16-
{{ if has "serverless_core" .Features }}"{{ .Module }}/plugins"{{end}}
21+
{{ if not .Serverless }}"{{ .Module }}/plugins"{{end}}
1722
)
1823

19-
// serverCmd represents the server command
2024
var serverCmd = &cobra.Command{
2125
Use: "server",
2226
Short: "{{ .APP }} server",
@@ -25,41 +29,44 @@ var serverCmd = &cobra.Command{
2529
cc := configurator.MustNewConfigCenter[config.Config](configurator.Config{
2630
Type: "yaml",
2731
}, subscriber.MustNewFsnotifySubscriber(cfgFile, subscriber.WithUseEnv(true)))
32+
2833
c, err := cc.GetConfig()
2934
logx.Must(err)
3035

31-
// set up logger
32-
logx.Must(logx.SetUp(c.Log.LogConf))
36+
// set up logger
37+
if err = logx.SetUp(c.Log.LogConf); err != nil {
38+
logx.Must(err)
39+
}
3340

34-
svcCtx := svc.NewServiceContext(cc)
35-
run(svcCtx)
41+
// print banner
42+
printBanner(c)
43+
// print version
44+
printVersion()
45+
46+
svcCtx := svc.NewServiceContext(cc)
47+
svcCtx.Middleware = middleware.NewMiddleware()
48+
global.ServiceContext = *svcCtx
49+
run(svcCtx)
3650
},
3751
}
3852

3953
func run(svcCtx *svc.ServiceContext) {
40-
c := svcCtx.MustGetConfig()
54+
server := rest.MustNewServer(svcCtx.MustGetConfig().Rest.RestConf)
4155

42-
server := rest.MustNewServer(c.Rest.RestConf)
43-
middleware.Register(server)
56+
ctm := custom.New(server)
57+
ctm.Init()
4458

45-
// server add api handlers
4659
handler.RegisterHandlers(server, svcCtx)
60+
middleware.Register(server)
4761

48-
// server add custom routes
49-
svcCtx.Custom.AddRoutes(server)
50-
51-
{{ if has "serverless_core" .Features }}// load plugins features
52-
plugins.LoadPlugins(server, *svcCtx){{end}}
62+
{{ if not .Serverless }}// load plugins
63+
plugins.LoadPlugins(server, svcCtx){{end}}
5364

5465
group := service.NewServiceGroup()
5566
group.Add(server)
56-
group.Add(svcCtx.Custom)
57-
58-
printBanner(c)
59-
printVersion()
67+
group.Add(ctm)
6068

61-
logx.Infof("Starting rest server at %s:%d...", c.Rest.Host, c.Rest.Port)
62-
group.Start()
69+
group.Start()
6370
}
6471

6572
func printBanner(c config.Config) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type VersionResponse {
1515
}
1616

1717
@server(
18-
prefix: /api/v1{{ if has "serverless" .Features }}/{{ .APP }}{{end}}
18+
prefix: /api/v1{{ if .Serverless }}/{{ .APP }}{{end}}
1919
group: version
2020
)
2121
service {{ .APP | ToCamel }} {

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

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

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

5-
func New() *Custom {
6-
return &Custom{}
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)
720
}
821

922
// Start Please add custom logic here.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package global
2+
3+
import (
4+
"{{ .Module }}/internal/svc"
5+
)
6+
7+
var ServiceContext svc.ServiceContext
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package version
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"os"
7+
"runtime"
8+
9+
"github.com/zeromicro/go-zero/core/logx"
10+
11+
"{{ .Module }}/internal/svc"
12+
"{{ .Module }}/internal/types"
13+
)
14+
15+
type Version struct {
16+
logx.Logger
17+
ctx context.Context
18+
svcCtx *svc.ServiceContext
19+
r *http.Request
20+
}
21+
22+
func NewVersion(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request) *Version {
23+
return &Version{
24+
Logger: logx.WithContext(ctx),
25+
ctx: ctx,
26+
svcCtx: svcCtx,
27+
r: r,
28+
}
29+
}
30+
31+
func (l *Version) Version(req *types.VersionRequest) (resp *types.VersionResponse, err error) {
32+
return &types.VersionResponse{
33+
Version: os.Getenv("VERSION"),
34+
GoVersion: runtime.Version(),
35+
Commit: os.Getenv("COMMIT"),
36+
Date: os.Getenv("DATE"),
37+
}, nil
38+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package middleware
33
import (
44
"github.com/zeromicro/go-zero/rest"
55
"github.com/zeromicro/go-zero/rest/httpx"
6-
)
7-
8-
type Middleware struct {}
96

10-
func New() Middleware {
11-
return Middleware{}
12-
}
7+
"{{ .Module }}/internal/svc"
8+
)
139

1410
func Register(server *rest.Server) {
1511
httpx.SetOkHandler(ResponseMiddleware)
1612
httpx.SetErrorHandlerCtx(ErrorMiddleware)
1713
httpx.SetValidator(NewValidator())
1814
}
15+
16+
func NewMiddleware() svc.Middleware {
17+
return svc.Middleware{}
18+
}

cmd/jzero/.template/frame/api/app/internal/svc/e3tGb3JtYXRTdHlsZSAuU3R5bGUgInNlcnZpY2VfY29udGV4dCJ9fS5nby50cGw=

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ import (
44
configurator "github.com/zeromicro/go-zero/core/configcenter"
55

66
"{{ .Module }}/internal/config"
7-
"{{ .Module }}/internal/custom"
87
"{{ .Module }}/internal/middleware"
98
)
109

1110
type ServiceContext struct {
1211
Config configurator.Configurator[config.Config]
13-
middleware.Middleware
14-
Custom *custom.Custom
12+
Middleware
1513
}
1614

1715
func NewServiceContext(cc configurator.Configurator[config.Config]) *ServiceContext {
1816
sc := &ServiceContext{
1917
Config: cc,
20-
Custom: custom.New(),
21-
Middleware: middleware.New(),
2218
}
2319
sc.SetConfigListener()
2420
return sc
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package svc
2+
3+
type Middleware struct {}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ if has "serverless_core" .Features }}// Code generated by jzero. DO NOT EDIT.
1+
{{ if not .Serverless }}// Code generated by jzero. DO NOT EDIT.
22
package plugins
33

44
import (
@@ -7,6 +7,4 @@ import (
77
"github.com/zeromicro/go-zero/rest"
88
)
99

10-
type CoreSvcCtx = svc.ServiceContext
11-
12-
func LoadPlugins(server *rest.Server, svcCtx CoreSvcCtx) {}{{ end }}
10+
func LoadPlugins(server *rest.Server, svcCtx *svc.ServiceContext) {}{{end}}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ if has "serverless" .Features }}package serverless
1+
{{ if .Serverless }}package serverless
22

33
import (
44
"path/filepath"
@@ -9,6 +9,7 @@ import (
99
"github.com/zeromicro/go-zero/rest"
1010

1111
"{{ .Module }}/internal/config"
12+
"{{ .Module }}/internal/global"
1213
"{{ .Module }}/internal/handler"
1314
"{{ .Module }}/internal/svc"
1415
)
@@ -18,13 +19,15 @@ type Serverless struct {
1819
HandlerFunc func(server *rest.Server, svcCtx *svc.ServiceContext) // 服务路由
1920
}
2021

21-
// Serverless please replace coreSvcCtx any type to real CoreSvcCtx
22-
func New(coreSvcCtx any) *Serverless {
22+
// New serverless function
23+
func New() *Serverless {
2324
cc := configurator.MustNewConfigCenter[config.Config](configurator.Config{
2425
Type: "yaml",
25-
}, subscriber.MustNewFsnotifySubscriber(cfgFile, subscriber.WithUseEnv(true)))
26+
}, subscriber.MustNewFsnotifySubscriber(filepath.Join("plugins", "{{ .DirName }}", "etc", "etc.yaml"), subscriber.WithUseEnv(true)))
2627

2728
svcCtx := svc.NewServiceContext(cc)
29+
global.ServiceContext = *svcCtx
30+
2831
return &Serverless{
2932
SvcCtx: svcCtx,
3033
HandlerFunc: handler.RegisterHandlers,

0 commit comments

Comments
 (0)