jote

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDummyMetrics

func AddDummyMetrics(mux *http.ServeMux)

Adds a simple /metrics route to the mux that returns the prometheus compatible metric "isupdummy 1". This enables you to add monitoring during the development before you decide on metrics to export.

func AddLoggingToMux

func AddLoggingToMux(next http.Handler, logger *slog.Logger) http.Handler

This wraps the mux (next) to log every request to logger. Recovers panics and ignores the /metrics path. It logs the ip, url, duration, status and error (recovered from panic). The special statuscode logging via the [loggingResponseWriter] type adds a ~50-100ns overhead to every request.

func AddLoggingToMuxNoRC

func AddLoggingToMuxNoRC(next http.Handler, logger *slog.Logger) http.Handler

Same as AddLoggingToMux but this function does not log the return code.

func AddLoggingToMuxWithCounter

func AddLoggingToMuxWithCounter(next http.Handler, logger *slog.Logger, counter *atomic.Uint64) http.Handler

Same as AddLoggingToMux but increases the counter by 1 every request. This should be used together with AddMetrics to have a request counter metric.

func AddMetrics

func AddMetrics(mux *http.ServeMux, name string, counter *atomic.Uint64)

Adds a single-metric /metrics route that returns the number in the counter as a metric. The metric name will be name+"_http_requests_total".

func CreateLogger

func CreateLogger(path string) *slog.Logger

A wrapper for log/slog that creates a slog logger. If path is "stdout" it will create a stdout logger, else it will log into the path file.

func CreateLoggerWithDebug added in v0.1.4

func CreateLoggerWithDebug(path string) *slog.Logger

Same as CreateLogger but will set the loglevel to debug if the DEBUG environment variable is set (to any value)

func CreateLoggerWithLevel

func CreateLoggerWithLevel(path string) (*slog.Logger, *slog.LevelVar)

Same as CreateLogger but returns the loglevel to control the logger.

func ExecuteTemplate

func ExecuteTemplate(tmpl *template.Template, w http.ResponseWriter, name string, tmap H)

Used to execute a template with w. Uses template.ExecuteTemplate.

func Go

func Go(gFunc func())

Small wrapper to start a goroutine and defer recover.

func HttpRequestGetIP

func HttpRequestGetIP(r *http.Request) string

Returns a request's IP, in order of priority: X-Real-IP header, X-Forwarded-For header, r.RemoteAddr, "".

func Must

func Must(err error)

Panics if error is not nil. Can be used to wrap functions that only return error to automatically panic. Mainly used for script startups where errors are unrecoverable.

func Must2

func Must2(obj any, err error)

Can be used with value,error return functions where value is not needed and error must not be nil. For example io/WriteString. Mainly used for script startups where errors are unrecoverable.

func Must2r

func Must2r(obj any, err error) any

The same as Must2 but returns the first value if error is not nil. If error is nil, it panics.

func MustReadFile added in v0.1.2

func MustReadFile(filename string) string

Uses os/ReadFile, panics on error, returns string of the file contents if error is nil.

func ProfilingUntilTimeIfSet added in v0.1.4

func ProfilingUntilTimeIfSet(duration int)

Starts profiling using the pprof package if the PPROF environment variable is set. The target file is the value of the pprof envvar.

func ReadConfigJSON added in v0.1.5

func ReadConfigJSON(path string, config any)

A simple wrapper for calling json.Unmarshal. It reads in the file and parses the content into the config object. Any error that occurs will call panic.

func ReadConfigYAML

func ReadConfigYAML(path string, config any)

A simple wrapper for sigs.k8s.io/yaml to read a yaml file from path and parse it into the 2nd argument. If the file read or parsing returns an error it will panic with the error.

func RenderTemplate

func RenderTemplate(mux *http.ServeMux, path string, tmpl *template.Template, tmplName string, tmap H)

Same as ExecuteTemplate but adds the route directly to mux with the given path. This is useful as a one-line template rendering route where not much logic has to be added. Warning: The map H that is provided will be used for every request as this is a "static" function and not evaluated every request.

func RunMux

func RunMux(addr string, mux http.Handler, logger *slog.Logger)

Creates a net/http.Server that uses the provided mux to run the webserver and shutdown gracefully if Interrupt,SIGINT or SIGTERM signals are received.. Timeouts for read/write/idle are 10 seconds. The shutdown does not have a context deadline, so it should use the IdleTimeout.

func RunMuxSimple

func RunMuxSimple(addr string, mux *http.ServeMux) error

Same as RunMux but without the graceful shutdown.

Types

type H

type H map[string]interface{}

A simple, short alias to refer to a string-interface map.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL