The library that provides HTTP utility functions.
- simplified interface of the
http.Clientstructure for mocking purposes; - wrapper for the
http.ResponseWriterinterface 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);
- functions:
- analogs:
- analog of the
http.Redirect()function with catching writing errors; - analog of the
http.FileServer()function with applied above-mentioned middlewares; - analog of the
http.Error()function with the additional improvements:- additional logging of the error;
- accepting of an error object instead of an error string;
- analog of the
- function to extract the parameter with the specified name from the path part of the request URL and then scan it into the data;
- JSON:
- function to read bytes from the reader and then unmarshal them into the data;
- function to marshal the data and then write it in the writer:
- additional setting of the corresponding content type;
- additional setting of the specified status code;
- function to start a server with support for graceful shutdown by a signal.
- analogs:
Prepare the directory:
$ mkdir --parents "$(go env GOPATH)/src/github.com/thewizardplusplus/"
$ cd "$(go env GOPATH)/src/github.com/thewizardplusplus/"
Clone this repository:
$ git clone https://github.com/thewizardplusplus/go-http-utils.git
$ cd go-http-utils
Install dependencies with the dep tool:
$ dep ensure -vendor-only
httputils.CatchingResponseWriter:
package main
import (
"log"
"net/http"
httputils "github.com/thewizardplusplus/go-http-utils"
)
func main() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
catchingWriter := httputils.NewCatchingResponseWriter(writer)
// use the catchingWriter object as the usual http.ResponseWriter interface
http.Redirect(
catchingWriter,
request,
"http://example.com/",
http.StatusMovedPermanently,
)
if err := catchingWriter.LastError(); err != nil {
log.Printf("unable to write the HTTP response: %v", err)
}
})
}httputils.CatchingMiddleware():
package main
import (
"fmt"
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)
catchingMiddleware := httputils.CatchingMiddleware(
// wrap the standard logger via the github.com/go-log/log package
print.New(logger),
)
var handler http.Handler
handler = http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
// writing error will be handled by the catching middleware
fmt.Fprintln(writer, "Hello, world!")
})
handler = catchingMiddleware(handler)
http.Handle("/", handler)
logger.Fatal(http.ListenAndServe(":8080", nil))
}httputils.SPAFallbackMiddleware():
package main
import (
"log"
"net/http"
httputils "github.com/thewizardplusplus/go-http-utils"
)
func main() {
staticAssetHandler := http.FileServer(http.Dir("/var/www/example.com"))
staticAssetHandler = httputils.SPAFallbackMiddleware()(staticAssetHandler)
http.Handle("/", staticAssetHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}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))
}httputils.RunServer():
package main
import (
"context"
stdlog "log"
"net/http"
"os"
"github.com/go-log/log/print"
httputils "github.com/thewizardplusplus/go-http-utils"
)
func main() {
server := &http.Server{Addr: ":8080"}
// use the standard logger for error handling
logger := stdlog.New(os.Stderr, "", stdlog.LstdFlags)
if ok := httputils.RunServer(
context.Background(),
server,
// wrap the standard logger via the github.com/go-log/log package
print.New(logger),
os.Interrupt,
); !ok {
// the error is already logged, so just end the program with the error status
os.Exit(1)
}
}- Check Errors When Calling
http.ResponseWriter.Write() - Mocking HTTP Requests in Golang
- Proxying API Requests in Development
- https://golang.org/pkg/net/http/#Server.Shutdown
The MIT License (MIT)
Copyright © 2020-2021 thewizardplusplus