simplemux

package module
v0.0.0-...-8089a7a Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 4 Imported by: 1

README

simplemux

A simple HTTP mux library for Go.

Installation

go get github.com/joeychilson/simplemux

Example

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/joeychilson/simplemux"
)

func loggingMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Println("Before handling the request")
		next.ServeHTTP(w, r)
		log.Println("After handling the request")
	})
}

func main() {
	mux := simplemux.New()

	mux.Use(loggingMiddleware)

	mux.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello World")
	})
	mux.Get("/:name", func(w http.ResponseWriter, r *http.Request) {
		name := simplemux.Param(r, "name")
		fmt.Fprintln(w, "Hello", name)
	})
	mux.Get("/users/:id", func(w http.ResponseWriter, r *http.Request) {
		id := simplemux.Param(r, "id")
		fmt.Fprintln(w, "Get user", id)
	})
	mux.Delete("/users/:id", func(w http.ResponseWriter, r *http.Request) {
		id := simplemux.Param(r, "id")
		fmt.Fprintln(w, "Delete user", id)
	})
	mux.Put("/users/:id", func(w http.ResponseWriter, r *http.Request) {
		id := simplemux.Param(r, "id")
		fmt.Fprintln(w, "Replace user", id)
	})
	mux.Patch("/users/:id", func(w http.ResponseWriter, r *http.Request) {
		id := simplemux.Param(r, "id")
		fmt.Fprintln(w, "Update user", id)
	})

	log.Printf("Listening on :8080")
	http.ListenAndServe(":8080", mux)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Param

func Param(r *http.Request, param string) string

Param retrieves the value of a named path parameter from the request context

func ParamInt

func ParamInt(r *http.Request, param string) int

ParamInt retrieves the value of a named path parameter from the request context as an integer If the value cannot be converted to an integer, 0 is returned

Types

type Key

type Key string

Key is a type alias for string

var ParamKey Key = "params"

ParamKey is the key used to store the path parameters in the request context

type Mux

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

Mux is a simple HTTP request multiplexer

func New

func New() *Mux

New creates a new instance of Mux

func (*Mux) Connect

func (m *Mux) Connect(path string, handler http.HandlerFunc)

Connect adds a new route for the CONNECT HTTP method

func (*Mux) Delete

func (m *Mux) Delete(path string, handler http.HandlerFunc)

Delete adds a new route for the DELETE HTTP method

func (*Mux) Get

func (m *Mux) Get(path string, handler http.HandlerFunc)

Get adds a new route for the GET HTTP method

func (*Mux) Head

func (m *Mux) Head(path string, handler http.HandlerFunc)

Head adds a new route for the HEAD HTTP method

func (*Mux) NotFound

func (m *Mux) NotFound(handler http.HandlerFunc)

NotFound sets the handler to be called when no matching route is found

func (*Mux) NotFoundHandler

func (m *Mux) NotFoundHandler() http.HandlerFunc

NotFoundHandler returns the handler to be called when no matching route is found

func (*Mux) Options

func (m *Mux) Options(path string, handler http.HandlerFunc)

Options adds a new route for the OPTIONS HTTP method

func (*Mux) Patch

func (m *Mux) Patch(path string, handler http.HandlerFunc)

Patch adds a new route for the PATCH HTTP method

func (*Mux) Post

func (m *Mux) Post(path string, handler http.HandlerFunc)

Post adds a new route for the POST HTTP method

func (*Mux) Put

func (m *Mux) Put(path string, handler http.HandlerFunc)

Put adds a new route for the PUT HTTP method

func (*Mux) ServeHTTP

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

ServeHTTP implements the http.Handler interface and handles incoming requests

func (*Mux) Trace

func (m *Mux) Trace(path string, handler http.HandlerFunc)

Trace adds a new route for the TRACE HTTP method

func (*Mux) Use

func (m *Mux) Use(middleware func(http.Handler) http.Handler)

Use adds a new middleware function to the Mux

func (*Mux) With

func (m *Mux) With(middleware func(http.Handler) http.Handler) *Mux

With adds a new middleware function to the Mux

Jump to

Keyboard shortcuts

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