Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"errors"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/virzz/mulan/app"
"github.com/virzz/mulan/web"
"go.uber.org/zap"
)
type Config struct {
//lint:ignore SA5008 Ignore JSON option "squash"
app.Config `json:",inline,squash" yaml:",inline"`
}
var (
Version string = "1.0.0"
Commit string = "dev"
Conf app.Configer
)
func main() {
meta := &app.Meta{
ID: "com.virzz.mulan.example",
Name: "example",
Description: "ExampleService",
Version: Version,
Commit: Commit,
}
std := app.New(meta)
std.SetPreInit(func(ctx context.Context) error {
return nil
})
std.SetValidate(func() error {
return nil
})
web.SetVersionHandler(meta.Name, meta.Version, meta.Commit)
routers := web.NewRouters()
routers.Handle("GET", "/", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "Hello, World!"})
})
std.SetRouters(routers)
std.SetAction(func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
httpCfg := Conf.GetHTTP().WithRequestID(true)
httpSrv, err := web.New(httpCfg, std.Routers(), nil, nil)
if err != nil {
return err
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
err := httpSrv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
zap.L().Error("Failed to run http server", zap.Error(err))
sig <- os.Interrupt
}
}()
switch <-sig {
case os.Interrupt:
httpSrv.Close()
case syscall.SIGTERM:
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
httpSrv.Shutdown(ctx)
}
return nil
})
if err := std.Execute(context.Background(), Conf); err != nil {
panic(err)
}
}
Output:
Index ¶
- type ActionFunc
- type App
- func (app *App) AddCommand(cmd ...*cobra.Command)
- func (app *App) AddFlagSet(fs ...*pflag.FlagSet)
- func (app *App) AddMaintainCmd(name string, cfg *db.Config)
- func (app *App) Conf() Configer
- func (app *App) DisableConfigCmd()
- func (app *App) EnableRemote(project string, publicKey ...string) error
- func (app *App) Execute(ctx context.Context, cfg Configer) error
- func (app *App) ExecuteE(ctx context.Context) (err error)
- func (app *App) Register(f web.RegisterFunc)
- func (app *App) RootCmd() *cobra.Command
- func (app *App) Routers() *web.Routers
- func (app *App) Run(ctx context.Context, cfg Configer) error
- func (app *App) SetAction(action ActionFunc)
- func (app *App) SetConfig(config Configer)
- func (app *App) SetPreInit(f PreInitFunc)
- func (app *App) SetRouters(routers *web.Routers)
- func (app *App) SetValidate(f ValidateFunc)
- type Config
- type Configer
- type Meta
- type PreInitFunc
- type Remote
- type ValidateFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
*Meta
// contains filtered or unexported fields
}
func (*App) AddCommand ¶ added in v0.2.2
func (*App) AddFlagSet ¶ added in v0.2.2
func (*App) AddMaintainCmd ¶ added in v0.2.4
func (*App) DisableConfigCmd ¶ added in v0.2.4
func (app *App) DisableConfigCmd()
func (*App) EnableRemote ¶ added in v0.2.2
func (*App) Register ¶ added in v0.2.2
func (app *App) Register(f web.RegisterFunc)
func (*App) SetAction ¶ added in v0.2.2
func (app *App) SetAction(action ActionFunc)
func (*App) SetPreInit ¶ added in v0.2.2
func (app *App) SetPreInit(f PreInitFunc)
func (*App) SetRouters ¶ added in v0.2.2
func (*App) SetValidate ¶ added in v0.2.2
func (app *App) SetValidate(f ValidateFunc)
type Config ¶
type Configer ¶
type Configer interface {
Validate() error
GetHTTP() *web.Config
GetDB() *db.Config
GetRDB() *rdb.Config
GetLog() *log.Config
GetToken() *web.Token
}
var (
Conf Configer
)
type PreInitFunc ¶
type ValidateFunc ¶
type ValidateFunc func() error
Click to show internal directories.
Click to hide internal directories.