httprouter is a framework used for HTTP request routing in golang
Find a file
Martin Riedl 0a0fc645fc
All checks were successful
Release / Semantic Release (push) Successful in 58s
Build / Checks (push) Successful in 38s
Build / Code Coverage (push) Successful in 47s
Build / Build (push) Successful in 31s
Merge branch 'beta'
2025-03-01 13:50:43 +01:00
.forgejo/workflows ci: new build pipeline 2025-02-27 20:41:08 +01:00
.gitignore fix: ignore intellij project files 2024-07-01 22:55:50 +02:00
.releaserc ci: new release workflow 2025-02-27 20:39:15 +01:00
fs.go feat: renamed goHTTPRouter to httprouter 2025-02-27 20:34:47 +01:00
fs_test.go feat: renamed goHTTPRouter to httprouter 2025-02-27 20:34:47 +01:00
go.mod feat: new release of version 4 2025-02-28 15:46:06 +01:00
LICENSE.txt initial upload 2019-08-03 18:52:58 +02:00
README.md feat: new release of version 4 2025-02-28 15:46:06 +01:00
renovate.json ci: merge dependency updates into develop branch 2025-03-01 13:26:56 +01:00
route.go style: removed unnecessary braces and else if statements 2025-02-27 20:43:46 +01:00
route_test.go style: fixed typo 2025-02-27 20:42:57 +01:00
router.go style: removed unnecessary braces and else if statements 2025-02-27 20:43:46 +01:00
router_test.go feat: renamed goHTTPRouter to httprouter 2025-02-27 20:34:47 +01:00

httprouter

release pipeline status GoDoc Go Report Card

httprouter is a framework used for HTTP request routing.

Examples

Simple Routing

Just replace the standard router of golang with this one:

import "git.martin-riedl.de/golang/httprouter/v4"
router := httprouter.New()
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
    response.Write([]byte("Home"))
})
err := http.ListenAndServe("localhost:8080", router)

Routing with placeholder

A path can also contain placeholder (like :id). If then a request is sent to the url "/user/123" the method gets executed and the URL part (in this case "123") is passed as parameter into your handler function.

import "git.martin-riedl.de/golang/httprouter/v4"
router := httprouter.New()
router.HandleFunc(http.MethodGet, "/user/:id", handleUserPages)
router.HandleFunc(http.MethodGet, "/user/:id/settings", handleUserPages)
func handleUserPages(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
    id := info.Parameters["id"]
    response.Write([]byte(id))
}
err := http.ListenAndServe("localhost:8080", router)

Serve Static Content

Static files (like JavaScript or CSS) can be served automatically (including caching header and MIME type). It uses the embed.FS (since go 1.16) to serve static content.

import "git.martin-riedl.de/golang/httprouter/v4"

//go:embed files/static/*
var staticFiles embed.FS
staticFS := httprouter.NewFS(&staticFiles)
router := httprouter.New()
router.Handle(http.MethodGet, "/static/*", staticFS)

For development purpose you can enable the local file serving additionally. The framework checks first, if the file exists locally and serves it directly. If not, the file is served from the embed.FS. This helps you during local development so you can modify a CSS file without recompiling everything.

import "git.martin-riedl.de/golang/httprouter/v4"
staticFS := httprouter.NewFS(&staticFiles)
staticFS.UseLocalFolder = true
staticFS.LocalFolderPrefix = "some/folder" // optional

License

Copyright 2018-2025 Martin Riedl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.