mux

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package mux implements a HTTP request multiplexer.

Index

Examples

Constants

View Source
const (
	MethodAll     = "ALL"
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
)

Common HTTP methods.

Variables

This section is empty.

Functions

func Param

func Param(ctx context.Context, param string) string

Param gets the path/url parameter from the specified Context. It returns an empty string if the parameter was not found.

Types

type Mux

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

Mux is a HTTP request multiplexer.

It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL. It implements http.Handler

Use New to get a valid Mux.

Example
package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/komuw/ong/log"
	"github.com/komuw/ong/middleware"
	"github.com/komuw/ong/mux"
)

func LoginHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprint(w, "welcome to your favorite website.")
	}
}

func BooksByAuthorHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		author := mux.Param(r.Context(), "author")
		_, _ = fmt.Fprintf(w, "fetching books by author: %s", author)
	}
}

func main() {
	l := log.New(os.Stdout, 1000)
	mux := mux.New(
		l,
		middleware.WithOpts("localhost", 8080, "secretKey", l),
		nil,
		mux.NewRoute(
			"login/",
			mux.MethodGet,
			LoginHandler(),
		),
		mux.NewRoute(
			"/books/:author",
			mux.MethodAll,
			BooksByAuthorHandler(),
		),
	)

	server := &http.Server{
		Handler: mux,
		Addr:    ":8080",
	}
	err := server.ListenAndServe()
	if err != nil {
		panic(err)
	}
}
Output:

func New

func New(l log.Logger, opt middleware.Opts, notFoundHandler http.HandlerFunc, routes ...Route) Mux

New return a HTTP request multiplexer that has the paths in routes.

notFoundHandler is the handler that will be used if a url is not found. If it is nil, http.NotFound is used instead.

All the paths of an application should be added as part of the routes slice argument. Typically, an application should only have one Mux.

It panics with a helpful error message if it detects conflicting routes.

func (Mux) ServeHTTP

func (m Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements a http.Handler

It routes incoming http requests based on method and path extracting path parameters as it goes.

type Route added in v0.0.14

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

Route represents the pattern & http method that will be served by a particular http handler.

Use NewRoute to get a valid Route.

func NewRoute

func NewRoute(
	pattern string,
	method string,
	handler http.HandlerFunc,
) Route

NewRoute creates a new Route.

func (Route) String added in v0.0.14

func (r Route) String() string

Jump to

Keyboard shortcuts

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