Use structured logging for demo app

This commit is contained in:
2020-05-22 16:03:59 +02:00
parent e26cb7ad49
commit c2666086cb
4 changed files with 83 additions and 15 deletions

View File

@ -2,9 +2,9 @@ package main
import (
"context"
"log"
"net/http"
"gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/template/html"
oidc "forge.cadoles.com/wpetit/goweb-oidc"
@ -18,7 +18,7 @@ import (
"gitlab.com/wpetit/goweb/session/gorilla"
)
func getServiceContainer(conf *config.Config) (*service.Container, error) {
func getServiceContainer(ctx context.Context, conf *config.Config) (*service.Container, error) {
// Initialize and configure service container
ctn := service.NewContainer()
@ -26,7 +26,7 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
// Generate random cookie authentication key if none is set
if conf.HTTP.CookieAuthenticationKey == "" {
log.Println("could not find cookie authentication key. generating one...")
logger.Info(ctx, "could not find cookie authentication key. generating one...")
cookieAuthenticationKey, err := gorilla.GenerateRandomBytes(64)
if err != nil {
@ -38,7 +38,7 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
// Generate random cookie encryption key if none is set
if conf.HTTP.CookieEncryptionKey == "" {
log.Println("could not find cookie encryption key. generating one...")
logger.Info(ctx, "could not find cookie encryption key. generating one...")
cookieEncryptionKey, err := gorilla.GenerateRandomBytes(32)
if err != nil {
@ -75,7 +75,6 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
// Create and expose config service provider
ctn.Provide(config.ServiceName, config.ServiceProvider(conf))
ctx := context.Background()
provider, err := oidc.NewProvider(ctx, conf.OIDC.IssuerURL)
if err != nil {
return nil, errors.Wrap(err, "could not create oidc provider")

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"net/http"
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
@ -17,6 +18,7 @@ import (
"os"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
//nolint: gochecknoglobals
@ -43,6 +45,8 @@ func init() {
}
func main() {
ctx := context.Background()
flag.Parse()
if version {
@ -54,10 +58,23 @@ func main() {
// Switch to new working directory if defined
if workdir != "" {
if err := os.Chdir(workdir); err != nil {
log.Fatalf("%+v", errors.Wrapf(err, "could not change working directory to '%s'", workdir))
logger.Fatal(
ctx,
"could not change working directory",
logger.E(err),
logger.F("workdir", workdir),
)
}
}
logger.Info(
ctx,
"starting",
logger.F("gitRef", GitRef),
logger.F("projectVersion", ProjectVersion),
logger.F("buildDate", BuildDate),
)
// Load configuration file if defined, use default configuration otherwise
var conf *config.Config
@ -66,7 +83,13 @@ func main() {
if configFile != "" {
conf, err = config.NewFromFile(configFile)
if err != nil {
log.Fatalf("%+v", errors.Wrapf(err, "could not load config file '%s'", configFile))
log.Fatalf("%+v", errors.Wrapf(err, " '%s'", configFile))
logger.Fatal(
ctx,
"could not load config file",
logger.E(err),
logger.F("configFile", configFile),
)
}
} else {
if dumpConfig {
@ -80,20 +103,38 @@ func main() {
// Dump configuration if asked
if dumpConfig {
if err := config.Dump(conf, os.Stdout); err != nil {
log.Fatalf("%+v", errors.Wrap(err, "could not dump config"))
logger.Fatal(
ctx,
"could not dump config",
logger.E(err),
)
}
os.Exit(0)
}
if err := config.WithEnvironment(conf); err != nil {
log.Fatalf("%+v", errors.Wrap(err, "could not override config with environment"))
logger.Fatal(
ctx,
"could not override config with environment",
logger.E(err),
)
}
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)
// Create service container
ctn, err := getServiceContainer(conf)
ctn, err := getServiceContainer(ctx, conf)
if err != nil {
log.Fatalf("%+v", errors.Wrap(err, "could not create service container"))
logger.Fatal(
ctx,
"could not create service container",
logger.E(err),
)
}
r := chi.NewRouter()
@ -107,11 +148,20 @@ func main() {
// Define routes
if err := route.Mount(r, conf); err != nil {
log.Fatalf("%+v", errors.Wrap(err, "could not mount http routes"))
logger.Fatal(
ctx,
"could not mount http routes",
logger.E(err),
)
}
log.Printf("listening on '%s'", conf.HTTP.Address)
logger.Info(ctx, "listening", logger.F("address", conf.HTTP.Address))
if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil {
log.Fatalf("%+v", errors.Wrapf(err, "could not listen on '%s'", conf.HTTP.Address))
logger.Fatal(
ctx,
"could not listen",
logger.E(err),
logger.F("address", conf.HTTP.Address),
)
}
}