feat: update http service

This commit is contained in:
rogeecn
2025-02-28 09:20:31 +08:00
parent f9400aff1a
commit 5b5a28b4bb
2 changed files with 32 additions and 6 deletions

View File

@@ -53,7 +53,7 @@ type Service struct {
App *app.Config App *app.Config
Job *job.Job Job *job.Job
Service *http.Service Http *http.Service
Initials []contracts.Initial `group:"initials"` Initials []contracts.Initial `group:"initials"`
Routes []contracts.HttpRoute `group:"routes"` Routes []contracts.HttpRoute `group:"routes"`
} }
@@ -65,18 +65,18 @@ func Serve(cmd *cobra.Command, args []string) error {
if svc.App.Mode == app.AppModeDevelopment { if svc.App.Mode == app.AppModeDevelopment {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
svc.Service.Engine.Get("/swagger/*", swagger.HandlerDefault) svc.Http.Engine.Get("/swagger/*", swagger.HandlerDefault)
} }
svc.Service.Engine.Use(errorx.Middleware) svc.Http.Engine.Use(errorx.Middleware)
svc.Service.Engine.Use(favicon.New(favicon.Config{ svc.Http.Engine.Use(favicon.New(favicon.Config{
Data: []byte{}, Data: []byte{},
})) }))
group := svc.Service.Engine.Group("") group := svc.Http.Engine.Group("")
for _, route := range svc.Routes { for _, route := range svc.Routes {
route.Register(group) route.Register(group)
} }
return svc.Service.Serve() return svc.Http.Serve()
}) })
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
"github.com/pkg/errors"
"github.com/riverqueue/river" "github.com/riverqueue/river"
"github.com/riverqueue/river/riverdriver/riverpgxv5" "github.com/riverqueue/river/riverdriver/riverpgxv5"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -83,3 +84,28 @@ func (q *Job) Client() (*river.Client[pgx.Tx], error) {
return q.client, nil return q.client, nil
} }
func (q *Job) Start(ctx context.Context) error {
client, err := q.Client()
if err != nil {
return errors.Wrap(err, "get client failed")
}
if err := client.Start(ctx); err != nil {
return err
}
defer client.StopAndCancel(ctx)
<-ctx.Done()
return nil
}
func (q *Job) StopAndCancel(ctx context.Context) error {
client, err := q.Client()
if err != nil {
return errors.Wrap(err, "get client failed")
}
return client.StopAndCancel(ctx)
}