World Conquest Chronicles

World Conquest Chronicles

go-http-utils, v1.1

The library that provides HTTP utility functions.

Adding an analog of the http.FileServer() function with applied SPAFallbackMiddleware() and CatchingMiddleware() middlewares.

Change Log

  • analogs:
    • adding an analog of the http.FileServer() function with applied SPAFallbackMiddleware() and CatchingMiddleware() middlewares.

Features

  • wrapper for the http.ResponseWriter interface for catching writing errors;
  • middlewares:
    • middleware for catching writing errors;
    • middleware that fallback of requests to static assets to the index.html file (useful in a SPA);
  • analogs:
    • analog of the http.Redirect() function with catching writing errors;
    • analog of the http.FileServer() function with applied above-mentioned middlewares;
  • function to start a server with support for graceful shutdown by a signal.

Examples

httputils.StaticAssetHandler():

package main

import (
    stdlog "log"
    "net/http"
    "os"

    "github.com/go-log/log/print"
    httputils "github.com/thewizardplusplus/go-http-utils"
)

func main() {
    // use the standard logger for error handling
    logger := stdlog.New(os.Stderr, "", stdlog.LstdFlags)
    staticAssetHandler := httputils.StaticAssetHandler(
        http.Dir("/var/www/example.com"),
        // wrap the standard logger via the github.com/go-log/log package
        print.New(logger),
    )

    http.Handle("/", staticAssetHandler)
    stdlog.Fatal(http.ListenAndServe(":8080", nil))
}

Repository

Link: https://github.com/thewizardplusplus/go-http-utils/tree/v1.1.

Content: code.

License: MIT.