64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package startup
|
|
|
|
import "github.com/julienschmidt/httprouter"
|
|
|
|
// Pre-defined queues...
|
|
var (
|
|
// Configure loggers
|
|
Logger Phase
|
|
// Post-process flags, applying computed defaults.
|
|
PostFlags Phase
|
|
// Make external connections
|
|
Startup Phase
|
|
// Gather HTTP routes
|
|
Routes ParameterizedPhase[*httprouter.Router]
|
|
)
|
|
|
|
type Phase struct {
|
|
items []func()
|
|
hasRun bool
|
|
}
|
|
|
|
func (q *Phase) Add(initFn ...func()) {
|
|
if q.hasRun {
|
|
panic("Added init function after startup")
|
|
}
|
|
for _, fn := range initFn {
|
|
q.items = append(q.items, fn)
|
|
}
|
|
}
|
|
|
|
func (q *Phase) Run() {
|
|
if q.hasRun {
|
|
panic("Attempted to run init function twice")
|
|
}
|
|
q.hasRun = true
|
|
for _, fn := range q.items {
|
|
fn()
|
|
}
|
|
}
|
|
|
|
type ParameterizedPhase[Param interface{}] struct {
|
|
items []func(Param)
|
|
hasRun bool
|
|
}
|
|
|
|
func (q *ParameterizedPhase[Param]) Add(initFn ...func(Param)) {
|
|
if q.hasRun {
|
|
panic("Added init function after startup")
|
|
}
|
|
for _, fn := range initFn {
|
|
q.items = append(q.items, fn)
|
|
}
|
|
}
|
|
|
|
func (q *ParameterizedPhase[Param]) Run(param Param) {
|
|
if q.hasRun {
|
|
panic("Attempted to run init function twice")
|
|
}
|
|
q.hasRun = true
|
|
for _, fn := range q.items {
|
|
fn(param)
|
|
}
|
|
}
|