Use structured logging for demo app
This commit is contained in:
parent
e26cb7ad49
commit
c2666086cb
@ -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")
|
||||
|
@ -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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
|
||||
"github.com/caarlos0/env/v6"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Log LogConfig `yaml:"log"`
|
||||
HTTP HTTPConfig `yaml:"http"`
|
||||
OIDC OIDCConfig `yaml:"oidc"`
|
||||
}
|
||||
@ -49,6 +51,11 @@ type OIDCConfig struct {
|
||||
PostLogoutRedirectURL string `yaml:"postLogoutRedirectURL" env:"OIDC_POST_LOGOUT_REDIRECT_URL"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level logger.Level `yaml:"level" env:"LOG_LEVEL"`
|
||||
Format logger.Format `yaml:"format" env:"LOG_FORMAT"`
|
||||
}
|
||||
|
||||
func NewDumpDefault() *Config {
|
||||
config := NewDefault()
|
||||
return config
|
||||
@ -56,6 +63,10 @@ func NewDumpDefault() *Config {
|
||||
|
||||
func NewDefault() *Config {
|
||||
return &Config{
|
||||
Log: LogConfig{
|
||||
Level: logger.LevelWarn,
|
||||
Format: logger.FormatHuman,
|
||||
},
|
||||
HTTP: HTTPConfig{
|
||||
Address: ":3002",
|
||||
CookieAuthenticationKey: "",
|
||||
|
@ -6,12 +6,14 @@ import (
|
||||
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
||||
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
"gitlab.com/wpetit/goweb/service/session"
|
||||
)
|
||||
|
||||
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
conf := config.Must(ctn)
|
||||
|
||||
sess, err := session.Must(ctn).Get(w, r)
|
||||
@ -25,5 +27,11 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
client := oidc.Must(ctn)
|
||||
|
||||
logger.Info(
|
||||
ctx,
|
||||
"logging out user",
|
||||
logger.F("postLogoutURL", conf.OIDC.PostLogoutRedirectURL),
|
||||
)
|
||||
|
||||
client.Logout(w, r, conf.OIDC.PostLogoutRedirectURL)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user