redirect

package
v0.18.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 3 Imported by: 1

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.
}
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)
}

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())
}

func (NonWWWRedirector) ServeHandler

func (m NonWWWRedirector) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

type Redirector

type Redirector struct {
	Target     string
	StatusCode int
}

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))
}

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())
}

func (WWWRedirector) ServeHandler

func (m WWWRedirector) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL