feat: complete template

This commit is contained in:
Rogee
2024-11-29 17:05:05 +08:00
parent aa7ad01ecc
commit 7bc082abea
36 changed files with 1336 additions and 252 deletions

View File

@@ -1,42 +1,56 @@
package http
import (
"backend/modules/users"
"backend/providers/app"
"backend/providers/http/fiber"
"backend/providers/http"
"backend/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 Default(providers ...container.ProviderContainer) container.Providers {
func defaultProviders(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
app.DefaultProvider(),
fiber.DefaultProvider(),
http.DefaultProvider(),
postgres.DefaultProvider(),
}, providers...)
}
func Command() atom.Option {
providers := defaultProviders().With(
users.Provide,
)
return atom.Command(
atom.Name("serve"),
atom.Short("run http server"),
atom.RunE(Serve),
atom.Providers(Default()),
atom.Providers(providers),
)
}
type Http struct {
dig.In
Service contracts.HttpService
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 {
for _, route := range http.Routes {
if err := route.Register(); err != nil {
log.Fatal(err)
}
}
return http.Service.Serve()
})
}