73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.haelnorr.com/h/golib/hlog"
|
|
"git.haelnorr.com/h/oslstats/internal/config"
|
|
"git.haelnorr.com/h/oslstats/pkg/embedfs"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Initializes and runs the server
|
|
func run(ctx context.Context, w io.Writer, config *config.Config) error {
|
|
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
|
defer cancel()
|
|
|
|
// Setup the logger
|
|
logger, err := hlog.NewLogger(config.HLOG, w)
|
|
if err != nil {
|
|
return errors.Wrap(err, "hlog.NewLogger")
|
|
}
|
|
|
|
// Setup the database connection
|
|
logger.Debug().Msg("Config loaded and logger started")
|
|
logger.Debug().Msg("Connecting to database")
|
|
bun, closedb, err := setupBun(ctx, config)
|
|
if err != nil {
|
|
return errors.Wrap(err, "setupDBConn")
|
|
}
|
|
defer closedb()
|
|
|
|
// Setup embedded files
|
|
logger.Debug().Msg("Getting embedded files")
|
|
staticFS, err := embedfs.GetEmbeddedFS()
|
|
if err != nil {
|
|
return errors.Wrap(err, "getStaticFiles")
|
|
}
|
|
|
|
logger.Debug().Msg("Setting up HTTP server")
|
|
httpServer, err := setupHttpServer(&staticFS, config, logger, bun)
|
|
if err != nil {
|
|
return errors.Wrap(err, "setupHttpServer")
|
|
}
|
|
|
|
// Runs the http server
|
|
logger.Debug().Msg("Starting up the HTTP server")
|
|
err = httpServer.Start(ctx)
|
|
if err != nil {
|
|
return errors.Wrap(err, "httpServer.Start")
|
|
}
|
|
|
|
// Handles graceful shutdown
|
|
var wg sync.WaitGroup
|
|
wg.Go(func() {
|
|
<-ctx.Done()
|
|
shutdownCtx := context.Background()
|
|
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
|
defer cancel()
|
|
err := httpServer.Shutdown(shutdownCtx)
|
|
if err != nil {
|
|
logger.Error().Err(err).Msg("Graceful shutdown failed")
|
|
}
|
|
})
|
|
wg.Wait()
|
|
logger.Info().Msg("Shutting down")
|
|
return nil
|
|
}
|