Skip to content

acoshift/methodmux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

methodmux

Build Status Coverage Status Go Report Card GoDoc

Method Multiplexer for http.ServeMux

Example

package main

import (
    "io"
    "net/http"

    "github.com/acoshift/methodmux"
)

func main() {
    mux := http.NewServeMux()
    mux.Handle("/", methodmux.Get(http.HandlerFunc(index)))
    mux.Handle("/about", methodmux.Mux{
        http.MethodGet:  http.HandlerFunc(aboutGet),
        http.MethodPost: http.HandlerFunc(aboutPost),
        "":              http.HandlerFunc(aboutOther),
    })
    http.ListenAndServe(":8080", mux)
}

func index(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello, Method Mux!")
}

func aboutGet(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "About Get")
}

func aboutPost(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "About Post")
}

func aboutOther(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "About does not support method "+r.Method)
}