router

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router middleware

func New

func New() *Router

New creates new router

Example

Dispatch requests to different inner middleware chains by URL path prefix, then wire the router into a parapet server. Each pattern matches itself and anything beneath it ("/api" also serves "/api/v1/..."); unmatched paths fall through to whatever the server runs after the router.

package main

import (
	"net/http"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/router"
)

// mw is a tiny helper that builds a parapet.Middleware which always responds
// with the given body, standing in for a real backend (e.g. upstream.SingleHost).
func mw(body string) parapet.Middleware {
	return parapet.MiddlewareFunc(func(http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			_, _ = w.Write([]byte(body))
		})
	})
}

func main() {
	r := router.New()
	r.Handle("/api", mw("api"))    // /api and /api/...
	r.Handle("/static", mw("cdn")) // /static and /static/...

	s := parapet.New()
	s.Use(r)
}

func (*Router) Handle

func (m *Router) Handle(pattern string, h parapet.Middleware)

Handle handles middleware

Example (Root)

Registering "/" overrides the fall-through: every request that no other pattern claims is handled by this middleware instead of the server's later handlers.

package main

import (
	"net/http"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/router"
)

// mw is a tiny helper that builds a parapet.Middleware which always responds
// with the given body, standing in for a real backend (e.g. upstream.SingleHost).
func mw(body string) parapet.Middleware {
	return parapet.MiddlewareFunc(func(http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			_, _ = w.Write([]byte(body))
		})
	})
}

func main() {
	r := router.New()
	r.Handle("/healthz", mw("ok"))
	r.Handle("/", mw("default")) // catch-all for everything else

	s := parapet.New()
	s.Use(r)
}

func (Router) ServeHandler

func (m Router) 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