Skip to content

trivago/go-bootstrap

Repository files navigation

go-bootstrap

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:

Maintenance and PRs

This repository is in active development but is not our main focus.
PRs are welcome, but will take some time to be reviewed.

License

All files in the repository are subject to the Apache 2.0 License

Builds and Releases

All commits to the main branch need to use conventional commits.
Releases will be generated automatically from these commits using Release Please.

Required tools

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 -- install

We provided a justfile to generate the required .envrc file. Run just init-nix to get started, or run the script directly.

Running unit-tests

After you have set up your environment, run unittests via just test or

go test ./...

Examples

Minimal usage

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

HTTP server

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

HTTPs server

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

About

A golang module to reduce common boilerplate code

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •