https://github.com/rmhubbert/rmhttp
rmhttp is a lightweight wrapper around the Go standard library HTTP server & router with a fluent interface for adding routes, groups, timeouts, headers & middleware.
https://github.com/rmhubbert/rmhttp
go golang golang-library http-server http-server-library
Last synced: 7 days ago
JSON representation
rmhttp is a lightweight wrapper around the Go standard library HTTP server & router with a fluent interface for adding routes, groups, timeouts, headers & middleware.
- Host: GitHub
- URL: https://github.com/rmhubbert/rmhttp
- Owner: rmhubbert
- License: mit
- Created: 2024-08-13T05:53:15.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-03-21T21:45:54.000Z (about 1 month ago)
- Last Synced: 2026-03-22T09:57:54.260Z (about 1 month ago)
- Topics: go, golang, golang-library, http-server, http-server-library
- Language: Go
- Homepage:
- Size: 285 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Agents: AGENTS.md
Awesome Lists containing this project
README
# rmhttp
[](https://pkg.go.dev/github.com/rmhubbert/rmhttp) 

 [](CONTRIBUTING.md)
**rmhttp** provides a lightweight wrapper around the Go standard library HTTP server and router provided by [net/http](https://pkg.go.dev/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.
## Installation
Run the following command from your project root directory to install **rmhttp** into your project.
```bash
go get github.com/rmhubbert/rmhttp/v5
```
## Quickstart
The following code will get you up and running quickly with a basic GET endpoint.
```go
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp/v5"
)
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
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](config.go) for details.
## Usage
**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.
### Error Handling
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.
```go
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp/v5"
)
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())
}
```
### Groups
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.
```go
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp/v5"
)
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
Headers can be easily added at the global, group, and route level by calling WithHeader() on the desired target.
```go
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp/v5"
)
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
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.
```go
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp/v5"
)
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
Middleware can be easily added at the global, group, and route level by calling WithMiddleware() or Use() on the desired target.
```go
package main
import (
"log"
"net/http"
"github.com/rmhubbert/rmhttp/v5"
"github.com/rmhubbert/rmhttp/v5/pkg/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())
}
```
## License
**rmhttp** is made available for use via the [MIT license](LICENSE).
## Contributing
Contributions are always welcome via [Pull Request](https://github.com/rmhubbert/rmhttp/pulls). 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](CONTRIBUTING.md)