A golang module to reduce common boilerplate code.
This module is shared between many golang tools in trivago and is very opinionated on the modules used in these tools. More precisely it expect tools to:
- Use zerolog for logging
- Be compatible to Google Cloud logs by providing commonly used fields
- Use Gin for serving HTTP
- Use viper for configuration
This repository is in active development but is not our main focus.
PRs are welcome, but will take some time to be reviewed.
All files in the repository are subject to the Apache 2.0 License
All commits to the main branch need to use conventional commits.
Releases will be generated automatically from these commits using Release Please.
All required tools can be installed locally via nix
and are loaded on demand via direnv.
On MacOS you can install nix via the installer from determinate systems.
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- installWe provided a justfile to generate the required .envrc file.
Run just init-nix to get started, or run the script directly.
After you have set up your environment, run unittests via just test or
go test ./...This allows reading configuration flags via viper, sets up zerolog in a google cloud logging friendly way and makes
the workload CGroup aware.
package main
import (
"github.com/trivago/go-bootstrap/config"
)
func main() {
config.Read("CFG","config.yaml")
}This extends the minimal example to let the workload serve HTTP.
package main
import (
"github.com/trivago/go-bootstrap/config"
"github.com/trivago/go-bootstrap/httpserver"
"github.com/spf13/viper"
)
func main() {
viper.SetDefault("port", 8080)
config.Read("CFG","config.yaml")
port := viper.GetInt("port")
srv := httpserver.New(port, httpserver.AlwaysOk, httpserver.AlwaysOk, nil)
httpserver.Listen(srv, nil)
}This example requires valid TLS certificates to be present as files. The [hack] directory contains some self-signed examples and a generator script for testing purposes.
package main
import (
"github.com/trivago/go-bootstrap/config"
"github.com/trivago/go-bootstrap/httpserver"
"github.com/spf13/viper"
)
func main() {
viper.SetDefault("port", 8443)
viper.SetDefault("tls.cert", "/etc/certs/tls.crt")
viper.SetDefault("tls.key", "/etc/certs/tls.key")
config.Read("CFG","config.yaml")
srv := httpserver.NewWithConfig(httpserver.Config{
Port: viper.GetInt("port"),
PathTLSCert: viper.GetString("tls.cert"),
PathTLSKey: viper.GetString("tls.key"),
})
httpserver.Listen(srv, nil)
}