package box
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/labstack/echo/v4"
)
// DefaultConfig is the default Config for a Box.
var DefaultConfig = Config{
ListenAddress: ":8000",
}
// Box is the main struct of this package which should be embedded into other structs.
type Box struct {
Config Config
Context context.Context
cancelContext context.CancelFunc
Logger *slog.Logger
loggerGlobal bool
WebServer *WebServer
}
// WebServer provides the web server functionality of Box by embedding an Echo instance.
type WebServer struct {
*echo.Echo
defaultLivenessProbe func(c echo.Context) error
defaultReadinessProbe func(c echo.Context) error
}
// Config is the configuration struct for a Box.
type Config struct {
LogLevel string `yaml:"logLevel"`
ListenAddress string `yaml:"listenAddress"`
TLSCertFile string `yaml:"tlsCertFile"`
TLSKeyFile string `yaml:"tlsKeyFile"`
}
type configWrapper struct {
Config Config `json:"box" yaml:"box"`
}
// New constructs a new Box with various Option parameters and a Context,
// which is canceled when the SIGINT or SIGTERM signals are received.
func New(options ...Option) *Box {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
box := &Box{
Context: ctx,
cancelContext: cancel,
}
WithConfig(DefaultConfig)(box)
setupBoxWithFlags(box)
for _, option := range options {
option(box)
}
box.Logger = setupLogger(box.Config.LogLevel)
if box.loggerGlobal {
slog.SetDefault(box.Logger)
}
return box
}
func setupLogger(levelStr string) *slog.Logger {
if isRunningInKubernetes() {
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: parseLogLevel(levelStr, slog.LevelInfo),
}))
}
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: parseLogLevel(levelStr, slog.LevelDebug),
}))
}
func parseLogLevel(levelStr string, defaultLevel slog.Level) slog.Level {
switch strings.ToLower(levelStr) {
case "":
return defaultLevel
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
panic(fmt.Errorf("unknown log level: %s", levelStr))
}
}
// CancelContext cancels the Context of the Box.
func (box *Box) CancelContext() {
box.cancelContext()
}
// ListenAndServe starts the listener of the WebServer and blocks until the Context of the Box is canceled.
// If TLSCertFile & TLSKeyFile are set, it starts an HTTPS server, otherwise an HTTP server.
// If the WebServer is not initialized, it returns an error.
func (box *Box) ListenAndServe() error {
if box.WebServer == nil {
return errors.New("web server has not been initialized")
}
defer box.cancelContext()
go func() {
<-box.Context.Done()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
_ = box.WebServer.Echo.Shutdown(shutdownCtx)
}()
if len(box.Config.TLSCertFile) > 0 && len(box.Config.TLSKeyFile) > 0 {
return box.WebServer.Echo.StartTLS(box.Config.ListenAddress, box.Config.TLSCertFile, box.Config.TLSKeyFile)
}
return box.WebServer.Echo.Start(box.Config.ListenAddress)
}
package main
import (
"errors"
"log/slog"
"net/http"
"github.com/labstack/echo/v4"
"github.com/mycreepy/box"
)
type App struct {
*box.Box
}
func (app *App) helloWorld(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func main() {
box.MustRegisterAndParseFlags()
app := App{
Box: box.New(
box.WithWebServer(),
),
}
app.WebServer.GET("/", app.helloWorld)
app.Logger.Info("starting webserver", slog.String("listenAddress", app.Config.ListenAddress))
err := app.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}
package box
import (
"errors"
"flag"
)
var (
logLevel string
listenAddress string
tlsCertFile string
tlsKeyFile string
)
// MustRegisterFlags registers all Config fields as flags with the flag package and calls flag.Parse.
// Panics if a flag has already been registered.
// The registered flags are:
//
// -log-level
// -listen-address
// -tls-cert-file
// -tls-key-file
func MustRegisterFlags() {
flag.StringVar(&logLevel, "log-level", logLevel, "Log level")
flag.StringVar(&listenAddress, "listen-address", listenAddress, "Webserver listen address")
flag.StringVar(&tlsCertFile, "tls-cert-file", tlsCertFile, "Webserver TLS certificate file")
flag.StringVar(&tlsKeyFile, "tls-key-file", tlsKeyFile, "Webserver TLS key file")
}
// ErrFlagsAlreadyParsed indicates that the flag.Parse function has already been called.
var ErrFlagsAlreadyParsed = errors.New("flag.Parse() has already been called")
// MustRegisterAndParseFlags calls MustRegisterFlags & flag.Parse.
// Panics with ErrFlagsAlreadyParsed if flag.Parse has already been called.
func MustRegisterAndParseFlags() {
MustRegisterFlags()
if flag.Parsed() {
panic(ErrFlagsAlreadyParsed)
}
flag.Parse()
}
func setupBoxWithFlags(box *Box) {
if len(logLevel) > 0 {
box.Config.LogLevel = logLevel
}
if len(listenAddress) > 0 {
box.Config.ListenAddress = listenAddress
}
if len(tlsCertFile) > 0 {
box.Config.TLSCertFile = tlsCertFile
}
if len(tlsKeyFile) > 0 {
box.Config.TLSKeyFile = tlsKeyFile
}
}
package box
import (
"net/http"
"os"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"gopkg.in/yaml.v3"
)
// Option is a modifier function which can alter the provided functionality of a Box.
type Option func(*Box)
// WithConfig configures the Box with the given Config.
func WithConfig(config Config) Option {
return func(box *Box) {
box.Config = config
}
}
// WithConfigFromPath reads a configuration file from the given path, decodes it from YAML and calls WithConfig.
// Panics if the file can not be opened or decoded.
func WithConfigFromPath(path string) Option {
return func(box *Box) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
var wrapper configWrapper
err = yaml.NewDecoder(file).Decode(&wrapper)
if err != nil {
panic(err)
}
WithConfig(wrapper.Config)(box)
}
}
// WithGlobalLogger sets the global slog logger to the Box's logger.
func WithGlobalLogger() Option {
return func(box *Box) {
box.loggerGlobal = true
}
}
// WithWebServer enables the web server functionality provided by WebServer.
func WithWebServer() Option {
return func(box *Box) {
box.WebServer = &WebServer{
Echo: echo.New(),
defaultLivenessProbe: func(c echo.Context) error {
return c.NoContent(http.StatusOK)
},
defaultReadinessProbe: func(c echo.Context) error {
return c.NoContent(http.StatusOK)
},
}
box.WebServer.HideBanner = true
box.WebServer.HidePort = true
if box.Config.ListenAddress == "" {
box.Config.ListenAddress = ":8000"
}
box.WebServer.Use(middleware.Recover(), echoprometheus.NewMiddleware("box_webserver"))
box.WebServer.GET("/metrics", echoprometheus.NewHandler())
box.WebServer.GET("/healthz", box.WebServer.defaultLivenessProbe)
box.WebServer.GET("/readyz", box.WebServer.defaultReadinessProbe)
}
}
// WithLivenessProbe allows to override the default liveness probe of the WebServer.
func WithLivenessProbe(probe func(c echo.Context) error) Option {
return func(box *Box) {
if box.WebServer == nil {
WithWebServer()(box)
}
box.WebServer.GET("/healthz", probe)
}
}
// WithReadinessProbe allows to override the default readiness probe of the WebServer.
func WithReadinessProbe(probe func(c echo.Context) error) Option {
return func(box *Box) {
if box.WebServer == nil {
WithWebServer()(box)
}
box.WebServer.GET("/readyz", probe)
}
}
package box
import "os"
func isRunningInKubernetes() bool {
_, exists := os.LookupEnv("KUBERNETES_SERVICE_HOST")
return exists
}