Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPSRedirector ¶
type HTTPSRedirector struct {
StatusCode int
}
HTTPSRedirector redirects to https
func HTTPS ¶
func HTTPS() *HTTPSRedirector
HTTPS creates new https redirector
Example ¶
Redirect plain HTTP requests to their HTTPS equivalent. The scheme is read from X-Forwarded-Proto, so place this behind a TLS-terminating load balancer.
package main
import (
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/redirect"
)
func main() {
s := parapet.New()
s.Use(redirect.HTTPS())
// s.Use(upstream.SingleHost(...)) — handles requests that already arrived over HTTPS.
}
Output:
Example (Permanent) ¶
Send a 308 Permanent Redirect instead of the default 301 so the browser preserves the request method and body when retrying.
package main
import (
"net/http"
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/redirect"
)
func main() {
m := redirect.HTTPS()
m.StatusCode = http.StatusPermanentRedirect
s := parapet.New()
s.Use(m)
}
Output:
func (HTTPSRedirector) ServeHandler ¶
func (m HTTPSRedirector) ServeHandler(h http.Handler) http.Handler
ServeHandler implements middleware interface
type NonWWWRedirector ¶
type NonWWWRedirector struct {
StatusCode int
}
NonWWWRedirector redirects to non-www
func NonWWW ¶
func NonWWW() *NonWWWRedirector
NonWWW creates new non www redirector
Example ¶
Normalize "www.example.com" to the bare "example.com" host, keeping the request's scheme, path and query.
package main
import (
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/redirect"
)
func main() {
s := parapet.New()
s.Use(redirect.NonWWW())
}
Output:
func (NonWWWRedirector) ServeHandler ¶
func (m NonWWWRedirector) ServeHandler(h http.Handler) http.Handler
ServeHandler implements middleware interface
type Redirector ¶
Redirector redirects to target
func To ¶
func To(target string, statusCode int) *Redirector
To redirects to target
Example ¶
Redirect every request to a fixed target with an explicit status code, e.g. when retiring a domain.
package main
import (
"net/http"
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/redirect"
)
func main() {
s := parapet.New()
s.Use(redirect.To("https://new.example.com/", http.StatusMovedPermanently))
}
Output:
func (Redirector) ServeHandler ¶
func (m Redirector) ServeHandler(h http.Handler) http.Handler
ServeHandler implements middleware interface
type WWWRedirector ¶
type WWWRedirector struct {
StatusCode int
}
WWWRedirector redirects to www
func WWW ¶
func WWW() *WWWRedirector
WWW creates new www redirector
Example ¶
Normalize the bare host to its "www." variant — the inverse of NonWWW; pick one canonical form for your site.
package main
import (
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/redirect"
)
func main() {
s := parapet.New()
s.Use(redirect.WWW())
}
Output:
func (WWWRedirector) ServeHandler ¶
func (m WWWRedirector) ServeHandler(h http.Handler) http.Handler
ServeHandler implements middleware interface