feat: complete template
This commit is contained in:
@@ -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()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user