feat: update
This commit is contained in:
@@ -14,9 +14,7 @@ import (
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
@@ -31,20 +29,17 @@ func defaultProviders(providers ...container.ProviderContainer) container.Provid
|
||||
}
|
||||
|
||||
func Command() atom.Option {
|
||||
providers := defaultProviders(
|
||||
wechat.DefaultProvider(),
|
||||
storage.DefaultProvider(),
|
||||
).With(
|
||||
users.Provide,
|
||||
medias.Provide,
|
||||
)
|
||||
|
||||
return atom.Command(
|
||||
atom.Name("serve"),
|
||||
atom.Short("run http server"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(providers.With(
|
||||
atom.Providers(defaultProviders(
|
||||
storage.DefaultProvider(),
|
||||
wechat.DefaultProvider(),
|
||||
).With(
|
||||
middlewares.Provide,
|
||||
users.Provide,
|
||||
medias.Provide,
|
||||
)),
|
||||
)
|
||||
}
|
||||
@@ -52,6 +47,7 @@ func Command() atom.Option {
|
||||
type Http struct {
|
||||
dig.In
|
||||
|
||||
App *app.Config
|
||||
Service *http.Service
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
Routes []contracts.HttpRoute `group:"routes"`
|
||||
@@ -60,34 +56,20 @@ type Http struct {
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(http Http) error {
|
||||
if http.App.Mode == app.AppModeDevelopment {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
mid := http.Middlewares
|
||||
http.Service.Engine.Use(mid.DebugMode)
|
||||
http.Service.Engine.Use(mid.WeChatVerify)
|
||||
http.Service.Engine.Use(mid.WeChatAuthUserInfo)
|
||||
http.Service.Engine.Use(mid.WeChatSilentAuth)
|
||||
http.Service.Engine.Use(mid.ParseJWT)
|
||||
|
||||
mounts := map[string][]string{
|
||||
"/t/{tenant}": {"users", "medias"},
|
||||
}
|
||||
group := http.Service.Engine.Group("/v1")
|
||||
for _, route := range http.Routes {
|
||||
r := route.Register()
|
||||
if app, ok := r.(*fiber.App); ok {
|
||||
match := false
|
||||
for prefix, groups := range mounts {
|
||||
log.Infof("mount %s to %s", prefix, route.Name())
|
||||
if lo.Contains(groups, route.Name()) {
|
||||
match = true
|
||||
http.Service.Engine.Use(prefix, app)
|
||||
break
|
||||
}
|
||||
}
|
||||
_ = match
|
||||
|
||||
if !match {
|
||||
log.Infof("mount / to %s", route.Name())
|
||||
http.Service.Engine.Use(app)
|
||||
}
|
||||
}
|
||||
route.Register(group)
|
||||
}
|
||||
|
||||
return http.Service.Serve()
|
||||
|
||||
Reference in New Issue
Block a user