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

@ -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: "",

View File

@ -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)
}