You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

72 lines
1.7 KiB

package main
import (
"flag"
"os"
"maze.io/gate/pkg/net/web"
"maze.io/gate/cmd"
"maze.io/gate/pkg/core"
"maze.io/gate/pkg/core/logger"
)
func main() {
var (
configFlag = flag.String("config", "gate.hcl", "configuration file")
)
flag.Parse()
log := logger.New(os.Stdout)
config, err := cmd.LoadConfig(*configFlag)
if err != nil {
log.WithError(err).WithField("name", *configFlag).Fatal("failed to load configuration")
}
log = config.Logger()
provider, err := config.IdentityProvider()
if err != nil {
log.WithError(err).WithField("name", *configFlag).Fatal("failed to load identity provider")
}
if err = provider.Setup(log); err != nil {
log.WithError(err).Fatal("failed to setup identity provider")
}
httpd, services := config.Services()
run(log, httpd, services, provider)
}
func run(log *logger.Logger, httpd *web.Server, services []core.Service, provider core.IdentityProvider) {
log.Info("services starting")
errors := make(chan error, 16)
for i, service := range services {
log := log.WithField(logger.Component, service.Component())
log.Info("starting")
if err := service.Setup(provider); err != nil {
log.WithError(err).Error("service setup failed")
} else if err := service.Start(log, errors); err != nil {
log.WithError(err).Error("service start failed")
} else {
log.Info("started")
continue
}
// Service failed to start, rewind the other services.
for j := i; j >= 0; j-- {
log.WithField(logger.Component, services[j].Component()).Info("closing")
_ = services[j].Close()
}
return
}
if httpd != nil {
for _, service := range services {
httpd.AddService(service)
}
}
for err := range errors {
log.WithError(err).Error("service error")
}
log.Info("services stopped")
}