https://github.com/gyozatech/temaki
Minimal HTTP router based on the net/http standard library
https://github.com/gyozatech/temaki
Last synced: 5 months ago
JSON representation
Minimal HTTP router based on the net/http standard library
- Host: GitHub
- URL: https://github.com/gyozatech/temaki
- Owner: gyozatech
- License: apache-2.0
- Created: 2021-10-28T15:53:46.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-07T16:41:18.000Z (over 1 year ago)
- Last Synced: 2025-04-12T15:59:47.437Z (over 1 year ago)
- Language: Go
- Size: 140 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# temaki
[](https://opensource.org/licenses/Apache-2.0)
[](http://golang.org)
[](https://github.com/ellerbrock/open-source-badges/)

Minimal HTTP router based on the net/http standard library.
## Usage
Creating a _router_ object with **temaki** is very simple:
```golang
package main
import (
"github.com/gyozatech/temaki"
"github.com/gyozatech/temaki/middlewares"
"log"
"net/http"
"fmt"
"strconv"
)
var logger *log.Logger
func init() {
logger = log.New(os.Stdout, "", log.LstdFlags)
}
func main() {
router := temaki.NewRouter()
// passing a custom logger to the middlewares (the default is gyozatch/noodlog)
middlewares.SetLogger(logger)
// provided temaki middlewares
router.UseMiddleware(middlewares.RecoverPanicMiddleware)
router.UseMiddleware(middlewares.RequestLoggerMiddleware)
router.UseMiddleware(middlewares.CORSMiddleware)
// custom middleware
router.UseMiddleware(authMiddleware)
// routes
router.GET("/api/v1/stores/{storeId}/products/{productId}", getProductHandler)
router.POST("/api/v1/stores/{storeId([^/]+)}/products/{productId([0-9]+)}", addProductHandler)
router.PATCH("/api/v1/stores/{storeId([^/]+)}/products/{productId}", updateProductHandler)
router.DELETE("/api/v1/stores/{storeId}/products/{productId([0-9]+)}", deleteProductHandler)
log.Fatal(router.Start(8080))
}
// MIDDLEWARE
func authMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Executing middleware before request phase!")
token, err := temaki.GetBearerToken(r)
if err != nil {
logger.Error(err)
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if token != "IcgFd3FiHwKDM2H" {
http.Error(w, "invalid bearer token provided", http.StatusUnauthorized)
return
}
handler.ServeHTTP(w, r)
fmt.Println("Executing middleware after request phase!")
})
}
// HANDLERS ********
func getProductHandler(w http.ResponseWriter, r *http.Request) {
storeId := temaki.GetPathParam(r, "storeId")
productId, _ := strconv.Atoi(temaki.GetPathParam(r, "productId"))
fmt.Fprintf(w, "getProductHandler %s %d\n", storeId, productId)
}
func addProductHandler(w http.ResponseWriter, r *http.Request) {
storeId := temaki.GetPathParam(r, "storeId")
productId, _ := strconv.Atoi(temaki.GetPathParam(r, "productId"))
fmt.Fprintf(w, "addProductHandler %s %d\n", storeId, productId)
}
func updateProductHandler(w http.ResponseWriter, r *http.Request) {
storeId := temaki.GetPathParam(r, "storeId")
productId, _ := strconv.Atoi(temaki.GetPathParam(r, "productId"))
fmt.Fprintf(w, "updateProductHandler %s %d\n", storeId, productId)
}
func deleteProductHandler(w http.ResponseWriter, r *http.Request) {
storeId := temaki.GetPathParam(r, "storeId")
productId, _ := strconv.Atoi(temaki.GetPathParam(r, "productId"))
fmt.Fprintf(w, "deleteProductHandler %s %d\n", storeId, productId)
}
```
As you have noticed from the _paths_ you can also decide to specify a **regex** pattern to the path parameters.
## Contributing
Any contribution to this project is welcome! Just fork the project, and open a Pull Request.
If you find a problem or a bug, or have some improvement suggestion, please open an issue or a discussion on Github.
## Special thanks
Thanks to [benhoyt](https://github.com/benhoyt/go-routing) who gaves me the idea for this wrapper.