linstrom/main.go
mstar 1527c198e0 Updated cli flags
Better usage message
Flipped the -pretty flag to -jsonlogs. Is now pretty per default
2024-12-24 10:48:00 +01:00

130 lines
3.4 KiB
Go

// TODO: Add EUPL banner everywhere
package main
import (
"embed"
"io"
"os"
"strings"
"time"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/mstarongithub/passkey"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
// "git.mstar.dev/mstar/linstrom/ap"
"git.mstar.dev/mstar/linstrom/config"
"git.mstar.dev/mstar/linstrom/server"
"git.mstar.dev/mstar/linstrom/storage"
"git.mstar.dev/mstar/linstrom/storage/cache"
"git.mstar.dev/mstar/linstrom/util"
)
//go:embed frontend-reactive/dist/* frontend-reactive/dist/assets
var reactiveFS embed.FS
//go:embed frontend-noscript
var nojsFS embed.FS
//go:embed duck.webp
var placeholderFile string
func main() {
setLogger()
setLogLevel()
if err := config.ReadAndWriteToGlobal(*flagConfigFile); err != nil {
log.Fatal().
Err(err).
Str("config-file", *flagConfigFile).
Msg("Failed to read config and couldn't write default")
}
// Request to only check config
if *flagConfigOnly {
return
}
storageCache, err := cache.NewCache(
config.GlobalConfig.Storage.MaxInMemoryCacheSize,
config.GlobalConfig.Storage.RedisUrl,
)
if err != nil {
log.Fatal().Err(err).Msg("Failed to start cache")
}
// var store *storage.Storage
// if config.GlobalConfig.Storage.DbIsPostgres != nil && *config.GlobalConfig.Storage.DbIsPostgres {
// store, err = storage.NewStoragePostgres(config.GlobalConfig.Storage.DatabaseUrl, storageCache)
// } else {
// store, err = storage.NewStorageSqlite(config.GlobalConfig.Storage.DatabaseUrl, storageCache)
// }
//
store, err := storage.NewStorage(config.GlobalConfig.Storage.BuildPostgresDSN(), storageCache)
if err != nil {
log.Fatal().Err(err).Msg("Failed to setup storage")
}
pkey, err := passkey.New(passkey.Config{
WebauthnConfig: &webauthn.Config{
RPDisplayName: "Linstrom",
RPID: "localhost",
RPOrigins: []string{"http://localhost:8000"},
},
UserStore: store,
SessionStore: store,
SessionMaxAge: time.Hour * 24,
}, passkey.WithLogger(&util.ZerologWrapper{}))
if err != nil {
log.Fatal().Err(err).Msg("Failed to setup passkey support")
}
server := server.NewServer(
store,
pkey,
util.NewFSWrapper(reactiveFS, "frontend-reactive/dist/", false),
util.NewFSWrapper(nojsFS, "frontend-noscript/", false),
&placeholderFile,
)
server.Start(":8000")
// TODO: Set up media server
// TODO: Set up queues
// TODO: Set up http server
// TODO: Set up plugins
// TODO: Start everything
}
func setLogLevel() {
switch strings.ToLower(*flagLogLevel) {
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Info().Str("new-level", *flagLogLevel).Msg("New logging level set")
}
func setLogger(extraLogWriters ...io.Writer) {
if !*flagLogJson {
console := zerolog.ConsoleWriter{Out: os.Stderr}
log.Logger = zerolog.New(zerolog.MultiLevelWriter(append([]io.Writer{console}, extraLogWriters...)...)).
With().
Timestamp().
Logger()
} else {
log.Logger = zerolog.New(zerolog.MultiLevelWriter(
append([]io.Writer{log.Logger}, extraLogWriters...)...,
)).With().Timestamp().Logger()
}
}