Documentation
¶
Index ¶
- func AddDummyMetrics(mux *http.ServeMux)
- func AddLoggingToMux(next http.Handler, logger *slog.Logger) http.Handler
- func AddLoggingToMuxNoRC(next http.Handler, logger *slog.Logger) http.Handler
- func AddLoggingToMuxWithCounter(next http.Handler, logger *slog.Logger, counter *atomic.Uint64) http.Handler
- func AddMetrics(mux *http.ServeMux, name string, counter *atomic.Uint64)
- func CreateLogger(path string) *slog.Logger
- func CreateLoggerWithDebug(path string) *slog.Logger
- func CreateLoggerWithLevel(path string) (*slog.Logger, *slog.LevelVar)
- func ExecuteTemplate(tmpl *template.Template, w http.ResponseWriter, name string, tmap H)
- func Go(gFunc func())
- func HttpRequestGetIP(r *http.Request) string
- func Must(err error)
- func Must2(obj any, err error)
- func Must2r(obj any, err error) any
- func MustReadFile(filename string) string
- func ProfilingUntilTimeIfSet(duration int)
- func ReadConfigJSON(path string, config any)
- func ReadConfigYAML(path string, config any)
- func RenderTemplate(mux *http.ServeMux, path string, tmpl *template.Template, tmplName string, ...)
- func RunMux(addr string, mux http.Handler, logger *slog.Logger)
- func RunMuxSimple(addr string, mux *http.ServeMux) error
- type H
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddDummyMetrics ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
Same as CreateLogger but will set the loglevel to debug if the DEBUG environment variable is set (to any value)
func CreateLoggerWithLevel ¶
Same as CreateLogger but returns the loglevel to control the logger.
func ExecuteTemplate ¶
Used to execute a template with w. Uses template.ExecuteTemplate.
func HttpRequestGetIP ¶
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 ¶
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 ¶
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
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
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 ¶
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 ¶
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.