fix: structures
This commit is contained in:
58
templates/project/app/service/event/event.go.tpl
Normal file
58
templates/project/app/service/event/event.go.tpl
Normal file
@@ -0,0 +1,58 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"{{.ModuleName}}/app/events"
|
||||
"{{.ModuleName}}/app/service"
|
||||
"{{.ModuleName}}/providers/app"
|
||||
providerEvents "{{.ModuleName}}/providers/events"
|
||||
"{{.ModuleName}}/providers/postgres"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func defaultProviders() container.Providers {
|
||||
return service.Default(container.Providers{
|
||||
postgres.DefaultProvider(),
|
||||
}...)
|
||||
}
|
||||
|
||||
func Command() atom.Option {
|
||||
return atom.Command(
|
||||
atom.Name("event"),
|
||||
atom.Short("start event processor"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(
|
||||
defaultProviders().
|
||||
With(
|
||||
events.Provide,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
dig.In
|
||||
|
||||
App *app.Config
|
||||
PubSub *providerEvents.PubSub
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(ctx context.Context, svc Service) error {
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
|
||||
if svc.App.IsDevMode() {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
return svc.PubSub.Serve(ctx)
|
||||
})
|
||||
}
|
||||
79
templates/project/app/service/http/http.go.tpl
Normal file
79
templates/project/app/service/http/http.go.tpl
Normal file
@@ -0,0 +1,79 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"{{.ModuleName}}/app/errorx"
|
||||
"{{.ModuleName}}/app/jobs"
|
||||
_ "{{.ModuleName}}/docs"
|
||||
"{{.ModuleName}}/pkg/service"
|
||||
"{{.ModuleName}}/providers/app"
|
||||
"{{.ModuleName}}/providers/hashids"
|
||||
"{{.ModuleName}}/providers/http"
|
||||
"{{.ModuleName}}/providers/http/swagger"
|
||||
"{{.ModuleName}}/providers/jwt"
|
||||
"{{.ModuleName}}/providers/postgres"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/gofiber/fiber/v3/middleware/favicon"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func defaultProviders() container.Providers {
|
||||
return service.Default(container.Providers{
|
||||
http.DefaultProvider(),
|
||||
postgres.DefaultProvider(),
|
||||
jwt.DefaultProvider(),
|
||||
hashids.DefaultProvider(),
|
||||
job.DefaultProvider(),
|
||||
}...)
|
||||
}
|
||||
|
||||
func Command() atom.Option {
|
||||
return atom.Command(
|
||||
atom.Name("serve"),
|
||||
atom.Short("run http server"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(
|
||||
defaultProviders().
|
||||
With(
|
||||
jobs.Provide,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
type Http struct {
|
||||
dig.In
|
||||
|
||||
App *app.Config
|
||||
Job *job.Job
|
||||
Service *http.Service
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
Routes []contracts.HttpRoute `group:"routes"`
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(http Http) error {
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
|
||||
if http.App.Mode == app.AppModeDevelopment {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
http.Service.Engine.Get("/swagger/*", swagger.HandlerDefault)
|
||||
}
|
||||
http.Service.Engine.Use(errorx.Middleware)
|
||||
http.Service.Engine.Use(favicon.New(favicon.Config{
|
||||
Data: []byte{},
|
||||
}))
|
||||
|
||||
group := http.Service.Engine.Group("")
|
||||
for _, route := range http.Routes {
|
||||
route.Register(group)
|
||||
}
|
||||
|
||||
return http.Service.Serve()
|
||||
})
|
||||
}
|
||||
24
templates/project/app/service/queue/error.go.tpl
Normal file
24
templates/project/app/service/queue/error.go.tpl
Normal file
@@ -0,0 +1,24 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/riverqueue/river"
|
||||
"github.com/riverqueue/river/rivertype"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type CustomErrorHandler struct{}
|
||||
|
||||
func (*CustomErrorHandler) HandleError(ctx context.Context, job *rivertype.JobRow, err error) *river.ErrorHandlerResult {
|
||||
log.Infof("Job errored with: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*CustomErrorHandler) HandlePanic(ctx context.Context, job *rivertype.JobRow, panicVal any, trace string) *river.ErrorHandlerResult {
|
||||
log.Infof("Job panicked with: %v\n", panicVal)
|
||||
log.Infof("Stack trace: %s\n", trace)
|
||||
return &river.ErrorHandlerResult{
|
||||
SetCancelled: true,
|
||||
}
|
||||
}
|
||||
94
templates/project/app/service/queue/river.go.tpl
Normal file
94
templates/project/app/service/queue/river.go.tpl
Normal file
@@ -0,0 +1,94 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"{{.ModuleName}}/app/jobs"
|
||||
"{{.ModuleName}}/pkg/service"
|
||||
"{{.ModuleName}}/providers/app"
|
||||
"{{.ModuleName}}/providers/job"
|
||||
"{{.ModuleName}}/providers/postgres"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func defaultProviders() container.Providers {
|
||||
return service.Default(container.Providers{
|
||||
postgres.DefaultProvider(),
|
||||
job.DefaultProvider(),
|
||||
}...)
|
||||
}
|
||||
|
||||
func Command() atom.Option {
|
||||
return atom.Command(
|
||||
atom.Name("queue"),
|
||||
atom.Short("start queue processor"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(
|
||||
defaultProviders().
|
||||
With(
|
||||
jobs.Provide,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
dig.In
|
||||
|
||||
App *app.Config
|
||||
Job *job.Job
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
CronJobs []contracts.CronJob `group:"cron_jobs"`
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(ctx context.Context, svc Service) error {
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
|
||||
if svc.App.IsDevMode() {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
client, err := svc.Job.Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cronJob := range svc.CronJobs {
|
||||
log.
|
||||
WithField("module", "cron").
|
||||
WithField("name", cronJob.Description()).
|
||||
WithField("duration", cronJob.Periodic().Seconds()).
|
||||
Info("registering cron job")
|
||||
|
||||
for _, jobArgs := range cronJob.JobArgs() {
|
||||
client.PeriodicJobs().Add(
|
||||
river.NewPeriodicJob(
|
||||
river.PeriodicInterval(cronJob.Periodic()),
|
||||
func() (river.JobArgs, *river.InsertOpts) {
|
||||
return jobArgs, cronJob.InsertOpts()
|
||||
},
|
||||
&river.PeriodicJobOpts{
|
||||
RunOnStart: cronJob.RunOnStart(),
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if err := client.Start(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
defer client.StopAndCancel(ctx)
|
||||
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
15
templates/project/app/service/service.go.tpl
Normal file
15
templates/project/app/service/service.go.tpl
Normal file
@@ -0,0 +1,15 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"{{.ModuleName}}/providers/app"
|
||||
"{{.ModuleName}}/providers/events"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
)
|
||||
|
||||
func Default(providers ...container.ProviderContainer) container.Providers {
|
||||
return append(container.Providers{
|
||||
app.DefaultProvider(),
|
||||
events.DefaultProvider(),
|
||||
}, providers...)
|
||||
}
|
||||
29
templates/project/app/service/testx/testing.go.tpl
Normal file
29
templates/project/app/service/testx/testing.go.tpl
Normal file
@@ -0,0 +1,29 @@
|
||||
package testx
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"github.com/rogeecn/fabfile"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func Default(providers ...container.ProviderContainer) container.Providers {
|
||||
return append(container.Providers{}, providers...)
|
||||
}
|
||||
|
||||
func Serve(providers container.Providers, t *testing.T, invoke any) {
|
||||
Convey("tests boot up", t, func() {
|
||||
file := fabfile.MustFind("config.toml")
|
||||
|
||||
localEnv := os.Getenv("ENV_LOCAL")
|
||||
if localEnv != "" {
|
||||
file = fabfile.MustFind("config." + localEnv + ".toml")
|
||||
}
|
||||
|
||||
So(atom.LoadProviders(file, providers), ShouldBeNil)
|
||||
So(container.Container.Invoke(invoke), ShouldBeNil)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user