Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/virzz/mulan"
"github.com/virzz/mulan/service"
)
type (
WebConfig struct {
Port int `json:"port" yaml:"port"`
}
WebService struct {
engine *gin.Engine
server *http.Server
cfg *WebConfig
}
)
var _ service.Servicer = (*WebService)(nil)
func (w *WebService) Serve() error {
go w.server.ListenAndServe()
return nil
}
func (w *WebService) Shutdown(ctx context.Context) error {
shutdownCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return w.server.Shutdown(shutdownCtx)
}
func (w *WebService) Close() error {
return w.server.Close()
}
func NewWebService(cfg *WebConfig, route func(gin.IRouter)) *WebService {
engine := gin.Default()
route(engine)
return &WebService{
engine: engine,
server: &http.Server{
Handler: engine,
Addr: fmt.Sprintf(":%d", cfg.Port),
},
cfg: cfg,
}
}
type Config struct {
Web WebConfig `json:"web" yaml:"web"`
}
var (
Version string = "1.0.0"
Commit string = "dev"
BuildAt string = time.Now().Format(time.RFC3339)
Conf = &Config{}
)
func main() {
meta := &mulan.Meta{
ID: "com.virzz.mulan.example",
Name: "example",
Description: "ExampleService",
Version: Version,
Commit: Commit,
BuildAt: BuildAt,
}
std := mulan.New(meta, Conf)
routerFunc := func(api gin.IRouter) {
api.Handle("GET", "/", func(c *gin.Context) {
c.String(200, "Hello, World!")
})
}
webSrv := NewWebService(&Conf.Web, routerFunc)
std.AddService(webSrv)
if err := std.Execute(context.Background()); err != nil {
panic(err)
}
}
Output:
Index ¶
- func UnmarshalConfigOpt(dc *mapstructure.DecoderConfig)
- type ActionFunc
- type App
- func (app *App) AddCommand(cmd ...*cobra.Command) *App
- func (app *App) AddFlagSet(fs ...*pflag.FlagSet) *App
- func (app *App) AddService(srvs ...service.Servicer) *App
- func (app *App) BindFlags() *App
- func (app *App) Close() error
- func (app *App) Conf() any
- func (app *App) Execute(ctx context.Context, action ...ActionFunc) (err error)
- func (app *App) RootCmd() *cobra.Command
- func (app *App) Serve() error
- func (app *App) SetLogger(log *zap.Logger) *App
- func (app *App) SetPreInit(f PreInitFunc) *App
- func (app *App) SetVersionCmd(cmd *cobra.Command) *App
- func (app *App) Shutdown(ctx context.Context) error
- func (app *App) UnmarshalConfig(cfg any) error
- type Meta
- type PreInitFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UnmarshalConfigOpt ¶
func UnmarshalConfigOpt(dc *mapstructure.DecoderConfig)
Types ¶
type App ¶
type App struct {
*Meta
// contains filtered or unexported fields
}
func (*App) AddFlagSet ¶
AddFlagSet 添加 FlagSet 到 rootCmd,可选绑定到 viper
func (*App) Execute ¶
func (app *App) Execute(ctx context.Context, action ...ActionFunc) (err error)
func (*App) SetPreInit ¶
func (app *App) SetPreInit(f PreInitFunc) *App
func (*App) UnmarshalConfig ¶
type PreInitFunc ¶
Click to show internal directories.
Click to hide internal directories.