goweb-oidc/cmd/server/main.go

166 lines
3.3 KiB
Go
Raw Normal View History

2020-05-20 10:43:12 +02:00
package main
import (
2020-05-22 16:03:59 +02:00
"context"
2022-08-10 11:11:22 +02:00
"flag"
"fmt"
"log"
2020-05-20 10:43:12 +02:00
"net/http"
2022-08-10 11:11:22 +02:00
"os"
2020-05-20 10:43:12 +02:00
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
"forge.cadoles.com/wpetit/goweb-oidc/internal/route"
2022-08-10 11:11:22 +02:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
2020-05-20 10:43:12 +02:00
"gitlab.com/wpetit/goweb/middleware/container"
"github.com/pkg/errors"
2020-05-22 16:03:59 +02:00
"gitlab.com/wpetit/goweb/logger"
2020-05-20 10:43:12 +02:00
)
//nolint: gochecknoglobals
var (
configFile = ""
workdir = ""
dumpConfig = false
version = false
)
// nolint: gochecknoglobals
var (
GitRef = "unknown"
ProjectVersion = "unknown"
BuildDate = "unknown"
)
//nolint: gochecknoinits
func init() {
flag.StringVar(&configFile, "config", configFile, "configuration file")
flag.StringVar(&workdir, "workdir", workdir, "working directory")
flag.BoolVar(&dumpConfig, "dump-config", dumpConfig, "dump configuration and exit")
flag.BoolVar(&version, "version", version, "show version and exit")
}
func main() {
2020-05-22 16:03:59 +02:00
ctx := context.Background()
2020-05-20 10:43:12 +02:00
flag.Parse()
if version {
fmt.Printf("%s (%s) - %s\n", ProjectVersion, GitRef, BuildDate)
os.Exit(0)
}
// Switch to new working directory if defined
if workdir != "" {
if err := os.Chdir(workdir); err != nil {
2020-05-22 16:03:59 +02:00
logger.Fatal(
ctx,
"could not change working directory",
logger.E(err),
logger.F("workdir", workdir),
)
2020-05-20 10:43:12 +02:00
}
}
// Load configuration file if defined, use default configuration otherwise
var conf *config.Config
var err error
if configFile != "" {
conf, err = config.NewFromFile(configFile)
if err != nil {
2020-05-22 16:03:59 +02:00
log.Fatalf("%+v", errors.Wrapf(err, " '%s'", configFile))
logger.Fatal(
ctx,
"could not load config file",
logger.E(err),
logger.F("configFile", configFile),
)
2020-05-20 10:43:12 +02:00
}
} else {
if dumpConfig {
conf = config.NewDumpDefault()
} else {
conf = config.NewDefault()
}
}
// Dump configuration if asked
if dumpConfig {
if err := config.Dump(conf, os.Stdout); err != nil {
2020-05-22 16:03:59 +02:00
logger.Fatal(
ctx,
"could not dump config",
logger.E(err),
)
2020-05-20 10:43:12 +02:00
}
os.Exit(0)
}
if err := config.WithEnvironment(conf); err != nil {
2020-05-22 16:03:59 +02:00
logger.Fatal(
ctx,
"could not override config with environment",
logger.E(err),
)
}
2020-05-22 16:10:31 +02:00
logger.Info(
ctx,
"starting",
logger.F("gitRef", GitRef),
logger.F("projectVersion", ProjectVersion),
logger.F("buildDate", BuildDate),
)
2020-05-22 16:03:59 +02:00
logger.Debug(ctx, "setting log format", logger.F("format", conf.Log.Format))
logger.SetFormat(conf.Log.Format)
logger.Debug(ctx, "setting log level", logger.F("level", conf.Log.Level.String()))
logger.SetLevel(conf.Log.Level)
2020-05-20 10:43:12 +02:00
// Create service container
2020-05-22 16:03:59 +02:00
ctn, err := getServiceContainer(ctx, conf)
2020-05-20 10:43:12 +02:00
if err != nil {
2020-05-22 16:03:59 +02:00
logger.Fatal(
ctx,
"could not create service container",
logger.E(err),
)
2020-05-20 10:43:12 +02:00
}
r := chi.NewRouter()
// Define base middlewares
2022-08-10 11:11:22 +02:00
r.Use(middleware.CleanPath)
2020-05-20 10:43:12 +02:00
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// Expose service container on router
r.Use(container.ServiceContainer(ctn))
// Define routes
if err := route.Mount(r, conf); err != nil {
2020-05-22 16:03:59 +02:00
logger.Fatal(
ctx,
"could not mount http routes",
logger.E(err),
)
2020-05-20 10:43:12 +02:00
}
2020-05-22 16:03:59 +02:00
logger.Info(ctx, "listening", logger.F("address", conf.HTTP.Address))
2020-05-20 10:43:12 +02:00
if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil {
2020-05-22 16:03:59 +02:00
logger.Fatal(
ctx,
"could not listen",
logger.E(err),
logger.F("address", conf.HTTP.Address),
)
2020-05-20 10:43:12 +02:00
}
}