rmhttp provides a lightweight wrapper around the Go standard library HTTP server and router provided by net/http that allows for the easy addition of timeouts, groups, headers, and middleware (at the route, group and server level).
This package aims to make it easier to configure your routes and middleware, but then hand off as much as possible to the standard library once the server is running. Standard net/http handlers and middleware functions are used throughout.
Run the following command from your project root directory to install rmhttp into your project.
go get github.com/rmhubbert/rmhttp
The following code will get you up and running quickly with a basic GET endpoint.
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp"
)
func myHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
}
func main() {
// New() creates and intialises the app. You can optionally pass
// in a configuration object.
rmh := rmhttp.New()
// Handle(), HandleFunc(), Post(), Put(), Patch(), Delete() and
// Options() methods are also available.
rmh.Get("/hello", myHandler)
// ListenAndServe() starts the server.
log.Fatal(rmh.ListenAndServe())
}
Configuration options can be set via environment variables or by passing in a Config object to the New() method, See https://github.com/rmhubbert/rmhttp/blob/main/config.go for details.
rmhttp offers a fluent interface for building out your server functionality, allowing you to easily customise your server, groups, and routes. Here are some simple examples of the core functionality to get you started.
You can register your own handlers for 404 and 405 errors. These errors are normally triggered internally by http.ServeMux, and are normally not configurable.
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp"
)
func my404Handler := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "My 404 Message", http.StatusNotFound)
}
func my405Handler := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "My 405 Message", http.StatusMethodNotAllowed)
}
func main() {
rmh := rmhttp.New()
// This handler will replace the default 404 HTTP status code handler.
rmh.StatusNotFoundHandler(my404Handler)
// This handler will replace the default 405 HTTP status code handler.
rmh.StatusMethodNotAllowedHandler(my405Handler)
log.Fatal(rmh.ListenAndServe())
}
Routes can be easily grouped by registering them with a Group object. This allows all of the routes registered this way to inherit the group URL pattern plus any configured headers and middleware.
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp"
)
func myHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
}
func main() {
rmh := rmhttp.New()
// The following creates a Group and then registers a Get route with that Group.
// The route will be accessible at /api/hello.
rmh.Group("/api").Get("/hello", myHandler)
log.Fatal(rmh.ListenAndServe())
}
Headers can be easily added at the global, group, and route level by calling WithHeader() on the desired target.
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp"
)
func myHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
}
func main() {
rmh := rmhttp.New().WithHeader("X-Hello", "World")
rmh.Get("/hello", myHandler).WithHeader("X-My", "Header")
log.Fatal(rmh.ListenAndServe())
}
Timeouts can be easily added at the global, group, and route level by calling WithTimeout() on the desired target. The length of timeout is set in seconds.
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp"
)
func myHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
}
func main() {
rmh := rmhttp.New().WithTimeout(5, "Global timeout message")
rmh.Get("/hello", myHandler).WithTimeout(3, "Route timeout message")
log.Fatal(rmh.ListenAndServe())
}
Middleware can be easily added at the global, group, and route level by calling WithMiddleware() or Use() on the desired target.
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp"
"github.com/rmhubbert/rmhttp/middleware/recoverer"
)
func myHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
}
func main() {
rmh := rmhttp.New().WithMiddleware(recoverer.Middleware())
rmh.Get("/hello", myHandler)
log.Fatal(rmh.ListenAndServe())
}
rmhttp is made available for use via the MIT license.
Contributions are always welcome via Pull Request. Please make sure to add tests and make sure they are passing before submitting. It's also a good idea to lint your code with golintci-lint, using the config in this directory.
Contributors are expected to abide by the guidelines outlined in the Contributor Covenant Code of Conduct