feat: update framework
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
package common
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"github.com/atom-providers/jwt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"backend/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func GetJwtToken(ctx *fiber.Ctx) (string, error) {
|
||||
func GetJwtToken(ctx fiber.Ctx) (string, error) {
|
||||
headers, ok := ctx.GetReqHeaders()[jwt.HttpHeader]
|
||||
if !ok {
|
||||
return "", ctx.SendStatus(fiber.StatusUnauthorized)
|
||||
42
backend/common/service/http/http.go
Normal file
42
backend/common/service/http/http.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"backend/providers/app"
|
||||
"backend/providers/http/fiber"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func Default(providers ...container.ProviderContainer) container.Providers {
|
||||
return append(container.Providers{
|
||||
app.DefaultProvider(),
|
||||
fiber.DefaultProvider(),
|
||||
}, providers...)
|
||||
}
|
||||
|
||||
func Command() atom.Option {
|
||||
return atom.Command(
|
||||
atom.Name("serve"),
|
||||
atom.Short("run http server"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(Default()),
|
||||
)
|
||||
}
|
||||
|
||||
type Http struct {
|
||||
dig.In
|
||||
|
||||
Service contracts.HttpService
|
||||
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 {
|
||||
return http.Service.Serve()
|
||||
})
|
||||
}
|
||||
52
backend/common/service/migrate/migrate.go
Normal file
52
backend/common/service/migrate/migrate.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"backend/database"
|
||||
"backend/providers/postgres"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"github.com/pressly/goose/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func Default(providers ...container.ProviderContainer) container.Providers {
|
||||
return append(container.Providers{
|
||||
postgres.DefaultProvider(),
|
||||
}, providers...)
|
||||
}
|
||||
|
||||
func Command() atom.Option {
|
||||
return atom.Command(
|
||||
atom.Name("migrate"),
|
||||
atom.Short("run migrations"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(Default()),
|
||||
)
|
||||
}
|
||||
|
||||
type Migrate struct {
|
||||
dig.In
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(migrate Migrate) error {
|
||||
if len(args) == 0 {
|
||||
args = append(args, "up")
|
||||
}
|
||||
|
||||
action, args := args[0], args[1:]
|
||||
log.Infof("migration action: %s args: %+v", action, args)
|
||||
|
||||
goose.SetBaseFS(database.MigrationFS)
|
||||
goose.SetTableName("migrations")
|
||||
|
||||
return goose.RunContext(context.Background(), action, migrate.DB, "migrations", args...)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user