Skip to content

Commit

Permalink
feat(template): update embed frame start logic
Browse files Browse the repository at this point in the history
feat(template): update api frame start logic

feat(template): update rpc/gateway frame start logic
  • Loading branch information
jaronnie committed Oct 19, 2024
1 parent a586a13 commit 629d972
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 139 deletions.
63 changes: 19 additions & 44 deletions .template/frame/api/app/cmd/server.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/core/proc"
"github.com/common-nighthawk/go-figure"
"golang.org/x/sync/errgroup"

"{{ .Module }}/internal/config"
"{{ .Module }}/internal/handler"
Expand All @@ -24,28 +22,24 @@ var serverCmd = &cobra.Command{
Short: "{{ .APP }} server",
Long: "{{ .APP }} server",
Run: func(cmd *cobra.Command, args []string) {
Start(cfgFile)
var c config.Config
conf.MustLoad(cfgFile, &c)
config.C = c
// set up logger
if err := logx.SetUp(c.Log.LogConf); err != nil {
logx.Must(err)
}
if c.Log.LogConf.Mode != "console" {
logx.AddWriter(logx.NewWriter(os.Stdout))
}

ctx := svc.NewServiceContext(c)
run(ctx)
},
}

func Start(cfgFile string) {
var c config.Config
conf.MustLoad(cfgFile, &c)
config.C = c
// set up logger
if err := logx.SetUp(c.Log.LogConf); err != nil {
logx.Must(err)
}
if c.Log.LogConf.Mode != "console" {
logx.AddWriter(logx.NewWriter(os.Stdout))
}

ctx := svc.NewServiceContext(c)
start(ctx)
}

func start(svcCtx *svc.ServiceContext) {
func run(svcCtx *svc.ServiceContext) {
server := rest.MustNewServer(svcCtx.Config.Rest.RestConf)
middleware.Register(server)
Expand All @@ -57,36 +51,17 @@ func start(svcCtx *svc.ServiceContext) {
group := service.NewServiceGroup()
group.Add(server)
group.Add(svcCtx.Custom)
// shutdown listener
waitExit := proc.AddShutdownListener(svcCtx.Custom.Stop)
eg := errgroup.Group{}
eg.Go(func() error {
printBanner(svcCtx.Config)
logx.Infof("Starting rest server at %s:%d...", svcCtx.Config.Rest.Host, svcCtx.Config.Rest.Port)
group.Start()
return nil
})

// add custom start logic
eg.Go(func() error {
svcCtx.Custom.Start()
return nil
})

if err := eg.Wait(); err != nil {
panic(err)
}

waitExit()
printBanner(svcCtx.Config)
logx.Infof("Starting rest server at %s:%d...", svcCtx.Config.Rest.Host, svcCtx.Config.Rest.Port)
group.Start()
}

func printBanner(c config.Config) {
figure.NewColorFigure(c.Banner.Text, c.Banner.FontName, c.Banner.Color, true).Print()
}


func init() {
rootCmd.AddCommand(serverCmd)
}
82 changes: 30 additions & 52 deletions .template/frame/gateway/app/cmd/server.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/gateway"
"github.com/zeromicro/go-zero/core/proc"
"golang.org/x/sync/errgroup"

"{{ .Module }}/desc/pb"
"{{ .Module }}/internal/config"
"{{ .Module }}/internal/middleware"
Expand All @@ -25,71 +24,50 @@ var serverCmd = &cobra.Command{
Short: "{{ .APP }} server",
Long: "{{ .APP }} server",
Run: func(cmd *cobra.Command, args []string) {
Start(cfgFile)
var c config.Config
conf.MustLoad(cfgFile, &c)
config.C = c
// write pb to local
var err error
c.Gateway.Upstreams[0].ProtoSets, err = gwx.WritePbToLocal(pb.Embed)
if err != nil {
logx.Must(err)
}

// set up logger
if err = logx.SetUp(c.Log.LogConf); err != nil {
logx.Must(err)
}
if c.Log.LogConf.Mode != "console" {
logx.AddWriter(logx.NewWriter(os.Stdout))
}

ctx := svc.NewServiceContext(c)
run(ctx)
},
}

func Start(cfgFile string) {
var c config.Config
conf.MustLoad(cfgFile, &c)
config.C = c
// write pb to local
var err error
c.Gateway.Upstreams[0].ProtoSets, err = gwx.WritePbToLocal(pb.Embed)
if err != nil {
logx.Must(err)
}

// set up logger
if err = logx.SetUp(c.Log.LogConf); err != nil {
logx.Must(err)
}
if c.Log.LogConf.Mode != "console" {
logx.AddWriter(logx.NewWriter(os.Stdout))
}

ctx := svc.NewServiceContext(c)
start(ctx)
}

func start(svcCtx *svc.ServiceContext) {
func run(svcCtx *svc.ServiceContext) {
zrpc := server.RegisterZrpc(svcCtx.Config, svcCtx)
gw := gateway.MustNewServer(svcCtx.Config.Gateway.GatewayConf, middleware.WithHeaderProcessor())
// register middleware
middleware.Register(zrpc, gw)
// gw add custom routes
svcCtx.Custom.AddRoutes(gw)
// gw add custom routes
svcCtx.Custom.AddRoutes(gw)
group := service.NewServiceGroup()
group.Add(zrpc)
group.Add(gw)
group.Add(svcCtx.Custom)
// shutdown listener
waitExit := proc.AddShutdownListener(svcCtx.Custom.Stop)
eg := errgroup.Group{}
eg.Go(func() error {
printBanner(svcCtx.Config)
logx.Infof("Starting rpc server at %s...", svcCtx.Config.Zrpc.ListenOn)
logx.Infof("Starting gateway server at %s:%d...", svcCtx.Config.Gateway.Host, svcCtx.Config.Gateway.Port)
group.Start()
return nil
})

// add custom start logic
eg.Go(func() error {
svcCtx.Custom.Start()
return nil
})

if err := eg.Wait(); err != nil {
panic(err)
}
printBanner(svcCtx.Config)
logx.Infof("Starting rpc server at %s...", svcCtx.Config.Zrpc.ListenOn)
logx.Infof("Starting gateway server at %s:%d...", svcCtx.Config.Gateway.Host, svcCtx.Config.Gateway.Port)
waitExit()
group.Start()
}

func printBanner(c config.Config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"context"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down
62 changes: 19 additions & 43 deletions .template/frame/rpc/app/cmd/server.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/core/proc"
"golang.org/x/sync/errgroup"
"github.com/common-nighthawk/go-figure"

"{{ .Module }}/internal/config"
Expand All @@ -23,56 +21,34 @@ var serverCmd = &cobra.Command{
Short: "{{ .APP }} server",
Long: "{{ .APP }} server",
Run: func(cmd *cobra.Command, args []string) {
Start(cfgFile)
var c config.Config
conf.MustLoad(cfgFile, &c)
config.C = c
// set up logger
if err := logx.SetUp(c.Log.LogConf); err != nil {
logx.Must(err)
}
if c.Log.LogConf.Mode != "console" {
logx.AddWriter(logx.NewWriter(os.Stdout))
}

ctx := svc.NewServiceContext(c)
run(ctx)
},
}

func Start(cfgFile string) {
var c config.Config
conf.MustLoad(cfgFile, &c)
config.C = c
// set up logger
if err := logx.SetUp(c.Log.LogConf); err != nil {
logx.Must(err)
}
if c.Log.LogConf.Mode != "console" {
logx.AddWriter(logx.NewWriter(os.Stdout))
}

ctx := svc.NewServiceContext(c)
start(ctx)
}

func start(svcCtx *svc.ServiceContext) {
func run(svcCtx *svc.ServiceContext) {
zrpc := server.RegisterZrpc(svcCtx.Config, svcCtx)
middleware.Register(zrpc)
group := service.NewServiceGroup()
group.Add(zrpc)
group.Add(svcCtx.Custom)
// shutdown listener
waitExit := proc.AddShutdownListener(svcCtx.Custom.Stop)
eg := errgroup.Group{}
eg.Go(func() error {
printBanner(svcCtx.Config)
logx.Infof("Starting rpc server at %s...", svcCtx.Config.Zrpc.ListenOn)
group.Start()
return nil
})

// add custom start logic
eg.Go(func() error {
svcCtx.Custom.Start()
return nil
})

if err := eg.Wait(); err != nil {
panic(err)
}

waitExit()
printBanner(svcCtx.Config)
logx.Infof("Starting rpc server at %s...", svcCtx.Config.Zrpc.ListenOn)
group.Start()
}

func printBanner(c config.Config) {
Expand Down

0 comments on commit 629d972

Please sign in to comment.