Use structured logging for demo app
This commit is contained in:
parent
e26cb7ad49
commit
c2666086cb
|
@ -2,9 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"gitlab.com/wpetit/goweb/logger"
|
||||||
"gitlab.com/wpetit/goweb/template/html"
|
"gitlab.com/wpetit/goweb/template/html"
|
||||||
|
|
||||||
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
||||||
|
@ -18,7 +18,7 @@ import (
|
||||||
"gitlab.com/wpetit/goweb/session/gorilla"
|
"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
|
// Initialize and configure service container
|
||||||
ctn := service.NewContainer()
|
ctn := service.NewContainer()
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
|
||||||
|
|
||||||
// Generate random cookie authentication key if none is set
|
// Generate random cookie authentication key if none is set
|
||||||
if conf.HTTP.CookieAuthenticationKey == "" {
|
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)
|
cookieAuthenticationKey, err := gorilla.GenerateRandomBytes(64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -38,7 +38,7 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
|
||||||
|
|
||||||
// Generate random cookie encryption key if none is set
|
// Generate random cookie encryption key if none is set
|
||||||
if conf.HTTP.CookieEncryptionKey == "" {
|
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)
|
cookieEncryptionKey, err := gorilla.GenerateRandomBytes(32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -75,7 +75,6 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
|
||||||
// Create and expose config service provider
|
// Create and expose config service provider
|
||||||
ctn.Provide(config.ServiceName, config.ServiceProvider(conf))
|
ctn.Provide(config.ServiceName, config.ServiceProvider(conf))
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
provider, err := oidc.NewProvider(ctx, conf.OIDC.IssuerURL)
|
provider, err := oidc.NewProvider(ctx, conf.OIDC.IssuerURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "could not create oidc provider")
|
return nil, errors.Wrap(err, "could not create oidc provider")
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
|
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gitlab.com/wpetit/goweb/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint: gochecknoglobals
|
//nolint: gochecknoglobals
|
||||||
|
@ -43,6 +45,8 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if version {
|
if version {
|
||||||
|
@ -54,10 +58,23 @@ func main() {
|
||||||
// Switch to new working directory if defined
|
// Switch to new working directory if defined
|
||||||
if workdir != "" {
|
if workdir != "" {
|
||||||
if err := os.Chdir(workdir); err != nil {
|
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
|
// Load configuration file if defined, use default configuration otherwise
|
||||||
var conf *config.Config
|
var conf *config.Config
|
||||||
|
|
||||||
|
@ -66,7 +83,13 @@ func main() {
|
||||||
if configFile != "" {
|
if configFile != "" {
|
||||||
conf, err = config.NewFromFile(configFile)
|
conf, err = config.NewFromFile(configFile)
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
if dumpConfig {
|
if dumpConfig {
|
||||||
|
@ -80,20 +103,38 @@ func main() {
|
||||||
// Dump configuration if asked
|
// Dump configuration if asked
|
||||||
if dumpConfig {
|
if dumpConfig {
|
||||||
if err := config.Dump(conf, os.Stdout); err != nil {
|
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)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.WithEnvironment(conf); err != nil {
|
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
|
// Create service container
|
||||||
ctn, err := getServiceContainer(conf)
|
ctn, err := getServiceContainer(ctx, conf)
|
||||||
if err != nil {
|
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()
|
r := chi.NewRouter()
|
||||||
|
@ -107,11 +148,20 @@ func main() {
|
||||||
|
|
||||||
// Define routes
|
// Define routes
|
||||||
if err := route.Mount(r, conf); err != nil {
|
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 {
|
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"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gitlab.com/wpetit/goweb/logger"
|
||||||
|
|
||||||
"github.com/caarlos0/env/v6"
|
"github.com/caarlos0/env/v6"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
Log LogConfig `yaml:"log"`
|
||||||
HTTP HTTPConfig `yaml:"http"`
|
HTTP HTTPConfig `yaml:"http"`
|
||||||
OIDC OIDCConfig `yaml:"oidc"`
|
OIDC OIDCConfig `yaml:"oidc"`
|
||||||
}
|
}
|
||||||
|
@ -49,6 +51,11 @@ type OIDCConfig struct {
|
||||||
PostLogoutRedirectURL string `yaml:"postLogoutRedirectURL" env:"OIDC_POST_LOGOUT_REDIRECT_URL"`
|
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 {
|
func NewDumpDefault() *Config {
|
||||||
config := NewDefault()
|
config := NewDefault()
|
||||||
return config
|
return config
|
||||||
|
@ -56,6 +63,10 @@ func NewDumpDefault() *Config {
|
||||||
|
|
||||||
func NewDefault() *Config {
|
func NewDefault() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
|
Log: LogConfig{
|
||||||
|
Level: logger.LevelWarn,
|
||||||
|
Format: logger.FormatHuman,
|
||||||
|
},
|
||||||
HTTP: HTTPConfig{
|
HTTP: HTTPConfig{
|
||||||
Address: ":3002",
|
Address: ":3002",
|
||||||
CookieAuthenticationKey: "",
|
CookieAuthenticationKey: "",
|
||||||
|
|
|
@ -6,12 +6,14 @@ import (
|
||||||
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
||||||
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
|
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gitlab.com/wpetit/goweb/logger"
|
||||||
"gitlab.com/wpetit/goweb/middleware/container"
|
"gitlab.com/wpetit/goweb/middleware/container"
|
||||||
"gitlab.com/wpetit/goweb/service/session"
|
"gitlab.com/wpetit/goweb/service/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
ctn := container.Must(r.Context())
|
ctx := r.Context()
|
||||||
|
ctn := container.Must(ctx)
|
||||||
conf := config.Must(ctn)
|
conf := config.Must(ctn)
|
||||||
|
|
||||||
sess, err := session.Must(ctn).Get(w, r)
|
sess, err := session.Must(ctn).Get(w, r)
|
||||||
|
@ -25,5 +27,11 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
client := oidc.Must(ctn)
|
client := oidc.Must(ctn)
|
||||||
|
|
||||||
|
logger.Info(
|
||||||
|
ctx,
|
||||||
|
"logging out user",
|
||||||
|
logger.F("postLogoutURL", conf.OIDC.PostLogoutRedirectURL),
|
||||||
|
)
|
||||||
|
|
||||||
client.Logout(w, r, conf.OIDC.PostLogoutRedirectURL)
|
client.Logout(w, r, conf.OIDC.PostLogoutRedirectURL)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue