97 lines
1.4 KiB
Markdown
97 lines
1.4 KiB
Markdown
|
|
### starter > starter.go
|
||
|
|
```go
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
var (
|
||
|
|
// Default configuration
|
||
|
|
appName = filepath.Base(os.Args[0])
|
||
|
|
appVersion = "1.0.0"
|
||
|
|
pidPath = ".pid"
|
||
|
|
logPath = ""
|
||
|
|
stopTimeout = 5 * time.Second
|
||
|
|
|
||
|
|
// Internal state
|
||
|
|
commands = make(map[string]*command)
|
||
|
|
onStart func(ctx context.Context)
|
||
|
|
onStop func()
|
||
|
|
onReload func()
|
||
|
|
|
||
|
|
// Flags
|
||
|
|
flagSet = flag.NewFlagSet(appName, flag.ContinueOnError)
|
||
|
|
)
|
||
|
|
|
||
|
|
type command struct {
|
||
|
|
name string
|
||
|
|
desc string
|
||
|
|
fn func()
|
||
|
|
}
|
||
|
|
|
||
|
|
func init()
|
||
|
|
|
||
|
|
|
||
|
|
// SetInfo sets the application name and version.
|
||
|
|
func SetInfo(name, version string)
|
||
|
|
|
||
|
|
|
||
|
|
// SetPidFile sets the default PID file path.
|
||
|
|
func SetPidFile(path string)
|
||
|
|
|
||
|
|
|
||
|
|
// SetLogFile sets the default log file path.
|
||
|
|
func SetLogFile(path string)
|
||
|
|
|
||
|
|
|
||
|
|
// AddCmd adds a custom command.
|
||
|
|
func AddCmd(name, desc string, fn func())
|
||
|
|
|
||
|
|
|
||
|
|
// OnStart sets the function to be called when the service starts.
|
||
|
|
// The context will be canceled when a stop signal is received.
|
||
|
|
func OnStart(fn func(ctx context.Context))
|
||
|
|
|
||
|
|
|
||
|
|
// OnStop sets the function to be called when the service is stopping.
|
||
|
|
func OnStop(fn func())
|
||
|
|
|
||
|
|
|
||
|
|
// OnReload sets the function to be called when SIGHUP is received.
|
||
|
|
func OnReload(fn func())
|
||
|
|
|
||
|
|
|
||
|
|
// Run parses arguments and executes the service.
|
||
|
|
func Run()
|
||
|
|
|
||
|
|
|
||
|
|
func showHelp()
|
||
|
|
|
||
|
|
|
||
|
|
func runForeground()
|
||
|
|
|
||
|
|
|
||
|
|
func startCmd()
|
||
|
|
|
||
|
|
|
||
|
|
func stopCmd()
|
||
|
|
|
||
|
|
|
||
|
|
func restartCmd()
|
||
|
|
|
||
|
|
|
||
|
|
func statusCmd()
|
||
|
|
|
||
|
|
|
||
|
|
func savePid(p int)
|
||
|
|
|
||
|
|
|
||
|
|
func loadPid() int
|
||
|
|
|
||
|
|
|
||
|
|
func removePid()
|
||
|
|
|
||
|
|
|
||
|
|
func isProcessRunning(p int) bool
|
||
|
|
|
||
|
|
```
|
||
|
|
|