Manager: Actually load config, and use in a few places
This commit is contained in:
parent
30fd4e52b0
commit
b1b73de4ee
@ -25,6 +25,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/benbjohnson/clock"
|
"github.com/benbjohnson/clock"
|
||||||
@ -66,6 +67,7 @@ func main() {
|
|||||||
|
|
||||||
// Load configuration.
|
// Load configuration.
|
||||||
configService := config.NewService()
|
configService := config.NewService()
|
||||||
|
configService.Load()
|
||||||
|
|
||||||
if cliArgs.initDB {
|
if cliArgs.initDB {
|
||||||
log.Info().Msg("creating databases")
|
log.Info().Msg("creating databases")
|
||||||
@ -76,27 +78,19 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the database.
|
|
||||||
dbCtx, dbCtxCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer dbCtxCancel()
|
|
||||||
persist, err := persistence.OpenDB(dbCtx)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("error opening database")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: load port number from the configuration in the database.
|
|
||||||
// TODO: enable TLS via Let's Encrypt.
|
// TODO: enable TLS via Let's Encrypt.
|
||||||
listen := ":8080"
|
listen := configService.Get().Listen
|
||||||
_, port, _ := net.SplitHostPort(listen)
|
_, port, _ := net.SplitHostPort(listen)
|
||||||
log.Info().Str("port", port).Msg("listening")
|
log.Info().Str("port", port).Msg("listening")
|
||||||
|
|
||||||
// Construct the services.
|
// Construct the services.
|
||||||
|
persist := openDB(*configService)
|
||||||
timeService := clock.New()
|
timeService := clock.New()
|
||||||
compiler, err := job_compilers.Load(timeService)
|
compiler, err := job_compilers.Load(timeService)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("error loading job compilers")
|
log.Fatal().Err(err).Msg("error loading job compilers")
|
||||||
}
|
}
|
||||||
logStorage := task_logs.NewStorage("./task-logs") // TODO: load job storage path from configuration.
|
logStorage := task_logs.NewStorage(configService.Get().TaskLogsPath)
|
||||||
flamenco := api_impl.NewFlamenco(compiler, persist, logStorage, configService)
|
flamenco := api_impl.NewFlamenco(compiler, persist, logStorage, configService)
|
||||||
e := buildWebService(flamenco, persist)
|
e := buildWebService(flamenco, persist)
|
||||||
|
|
||||||
@ -165,3 +159,23 @@ func parseCliArgs() {
|
|||||||
}
|
}
|
||||||
zerolog.SetGlobalLevel(logLevel)
|
zerolog.SetGlobalLevel(logLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// openDB opens the database or dies.
|
||||||
|
func openDB(configService config.Service) *persistence.DB {
|
||||||
|
dsn := configService.Get().DatabaseDSN
|
||||||
|
if dsn == "" {
|
||||||
|
log.Fatal().Msg("configure the database in flamenco-manager.yaml")
|
||||||
|
}
|
||||||
|
|
||||||
|
dbCtx, dbCtxCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer dbCtxCancel()
|
||||||
|
persist, err := persistence.OpenDB(dbCtx, dsn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().
|
||||||
|
Err(err).
|
||||||
|
Str("dsn", dsn).
|
||||||
|
Msg("error opening database")
|
||||||
|
}
|
||||||
|
|
||||||
|
return persist
|
||||||
|
}
|
||||||
|
@ -26,7 +26,9 @@ type Service struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewService() *Service {
|
func NewService() *Service {
|
||||||
return &Service{}
|
return &Service{
|
||||||
|
config: DefaultConfig(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Load() error {
|
func (s *Service) Load() error {
|
||||||
|
@ -297,6 +297,7 @@ func DefaultConfig() Conf {
|
|||||||
|
|
||||||
// loadConf parses the given file and returns its contents as a Conf object.
|
// loadConf parses the given file and returns its contents as a Conf object.
|
||||||
func loadConf(filename string) (Conf, error) {
|
func loadConf(filename string) (Conf, error) {
|
||||||
|
log.Info().Str("file", filename).Msg("loading configuration")
|
||||||
yamlFile, err := os.ReadFile(filename)
|
yamlFile, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DefaultConfig(), err
|
return DefaultConfig(), err
|
||||||
|
@ -29,16 +29,13 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO : have this configurable from the CLI.
|
|
||||||
const dbDSN = "host=localhost user=flamenco password=flamenco dbname=flamenco TimeZone=Europe/Amsterdam"
|
|
||||||
|
|
||||||
// DB provides the database interface.
|
// DB provides the database interface.
|
||||||
type DB struct {
|
type DB struct {
|
||||||
gormDB *gorm.DB
|
gormDB *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenDB(ctx context.Context) (*DB, error) {
|
func OpenDB(ctx context.Context, dsn string) (*DB, error) {
|
||||||
db, err := openDB(ctx, dbDSN)
|
db, err := openDB(ctx, dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -56,7 +53,7 @@ func openDB(ctx context.Context, uri string) (*DB, error) {
|
|||||||
|
|
||||||
gormDB, err := gorm.Open(postgres.Open(uri), &gorm.Config{})
|
gormDB, err := gorm.Open(postgres.Open(uri), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic().Err(err).Msg("failed to connect database")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
db := DB{
|
db := DB{
|
||||||
|
@ -24,8 +24,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Storage can write data to task logs, rotate logs, etc.
|
// Storage can write data to task logs, rotate logs, etc.
|
||||||
@ -35,6 +37,18 @@ type Storage struct {
|
|||||||
|
|
||||||
// NewStorage creates a new log storage rooted at `basePath`.
|
// NewStorage creates a new log storage rooted at `basePath`.
|
||||||
func NewStorage(basePath string) *Storage {
|
func NewStorage(basePath string) *Storage {
|
||||||
|
if !filepath.IsAbs(basePath) {
|
||||||
|
absPath, err := filepath.Abs(basePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic().Err(err).Str("path", basePath).Msg("cannot resolve relative path to task logs")
|
||||||
|
}
|
||||||
|
basePath = absPath
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info().
|
||||||
|
Str("path", basePath).
|
||||||
|
Msg("task logs")
|
||||||
|
|
||||||
return &Storage{
|
return &Storage{
|
||||||
BasePath: basePath,
|
BasePath: basePath,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user