midway
is a micro Go package for arranging your HTTP middleware.
go get github.com/novrin/midway
A Queue
returns a Middleware
where the slice of given middlewares are applied first-in-first-out. The last middleware in the slice will execute first.
package main
import (
"net/http"
"github.com/novrin/midway"
)
func main() {
// Use Queue to arrange middleware to execute secureHeaders first.
queued := midway.Queue(corsHeaders, secureHeaders)
app := http.HandlerFunc(hello)
http.ListenAndServe(":1313", queued(app))
// serves secureHeaders(corsHeaders(app))
}
A Stack
returns a Middleware
where the slice of given middlewares are applied last-in-first-out. The first middleware in the slice will execute first.
package main
import (
"net/http"
"github.com/novrin/midway"
)
func main() {
// Use Stack to arrange middleware to execute corsHeaders first.
stacked := midway.Stack(corsHeaders, secureHeaders)
app := http.HandlerFunc(hello)
http.ListenAndServe(":1313", stacked(app))
// serves corsHeaders(secureHeaders(app))
}
Copyright (c) 2023-present novrin
Licensed under MIT License