Documentation
¶
Overview ¶
Package httpserver implements functions to extend the use of the http.Server standard library.
ServeMux ¶
ServeMux is an extension with new methods for http.ServeMux is a multiplexer for HTTP requests. use NewServeMux allocates and returns a new ServeMux.
Stores routing path to register middlewares and handlers.
Methods for helping routing path and registering middlewares:
- ServeMux.Use appends one or more middlewares onto the Router stack.
- ServeMux.With adds inline middlewares for registers the handler.
- ServeMux.Group adds a new inline-Router along the current routing path + /pattern, with middleware stack.
- ServeMux.Route mounts a sub-Router along the current routing path + /pattern, with middleware stack.
- ServeMux.Mount attaches another http.ServeMux along a /pattern/*.
WARNING: Avoid using ServeMux.Mount, because it is slower to resolve the multiplexer for HTTP requests.
Methods for registering handler along the current routing path + /pattern:
- ServeMux.Connect with http method CONNECT
- ServeMux.Delete with http method DELETE
- ServeMux.Get with http method GET
- ServeMux.Head with http method HEAD
- ServeMux.Options with http method OPTIONS
- ServeMux.Patch with http method PATCH
- ServeMux.Post with http method POST
- ServeMux.Put with http method PUT
- ServeMux.Trace with http method TRACE
- ServeMux.Method with custom http method by parameter
Index ¶
- type Handle
- type Router
- type ServeMux
- func (mux *ServeMux) Connect(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Delete(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Get(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Group(pattern string) Router
- func (mux *ServeMux) Head(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Method(method, pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Mount(pattern string, r Router)
- func (mux *ServeMux) Options(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Patch(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Post(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Put(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Route(pattern string, fn func(sub Router))
- func (mux *ServeMux) Trace(pattern string, handlerFn http.HandlerFunc)
- func (mux *ServeMux) Use(middlewares ...func(http.Handler) http.Handler)
- func (mux *ServeMux) With(middlewares ...func(http.Handler) http.Handler) Handle
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handle ¶
type Handle interface {
// Connect registers the handler along the current routing path + /pattern with http method CONNECT. If the given pattern conflicts with one that is already registered, Handle panics.
Connect(pattern string, handlerFn http.HandlerFunc)
// Delete registers the handler along the current routing path + /pattern with http method DELETE. If the given pattern conflicts with one that is already registered, Handle panics.
Delete(pattern string, handlerFn http.HandlerFunc)
// Get registers the handler along the current routing path + /pattern with http method GET. If the given pattern conflicts with one that is already registered, Handle panics.
Get(pattern string, handlerFn http.HandlerFunc)
// Head registers the handler along the current routing path + /pattern with http method HEAD. If the given pattern conflicts with one that is already registered, Handle panics.
Head(pattern string, handlerFn http.HandlerFunc)
// Options registers the handler along the current routing path + /pattern with http method OPTIONS. If the given pattern conflicts with one that is already registered, Handle panics.
Options(pattern string, handlerFn http.HandlerFunc)
// Patch registers the handler along the current routing path + /pattern with http method PATCH. If the given pattern conflicts with one that is already registered, Handle panics.
Patch(pattern string, handlerFn http.HandlerFunc)
// Post registers the handler along the current routing path + /pattern with http method POST. If the given pattern conflicts with one that is already registered, Handle panics.
Post(pattern string, handlerFn http.HandlerFunc)
// Put registers the handler along the current routing path + /pattern with http method PUT. If the given pattern conflicts with one that is already registered, Handle panics.
Put(pattern string, handlerFn http.HandlerFunc)
// Trace registers the handler along the current routing path + /pattern with http method TRACE. If the given pattern conflicts with one that is already registered, Handle panics.
Trace(pattern string, handlerFn http.HandlerFunc)
// Method registers the handler for the given along the current routing path + /pattern with custom http method by parameter. If the given pattern conflicts with one that is already registered, Handle panics.
Method(method, pattern string, handlerFn http.HandlerFunc)
}
Handle allows registers the handler for the given pattern
type Router ¶
type Router interface {
Handle
http.Handler
// Use appends one or more middlewares onto the [Router] stack.
Use(middlewares ...func(http.Handler) http.Handler)
// With adds inline middlewares for registers the handler.
With(middlewares ...func(http.Handler) http.Handler) Handle
// Group adds a new inline-[Router] along the current routing path + /pattern, with middleware stack.
Group(pattern string) Router
// Route mounts a sub-[Router] along the current routing path + /pattern, with middleware stack.
Route(pattern string, fn func(sub Router))
// Mount attaches another [http.ServeMux] along a /pattern/*.
//
// WARNING: Avoid using, because it is slower to resolve the multiplexer for HTTP requests.
Mount(pattern string, router Router)
}
Router stores routing path to register middlewares and handlers.
type ServeMux ¶
ServeMux is an extension with new methods for http.ServeMux is a multiplexer for HTTP requests
Stores routing path to register middlewares and handlers.
func NewServeMux ¶
func NewServeMux() *ServeMux
NewServeMux allocates and returns a new ServeMux.
Example ¶
package main
import (
"fmt"
"log"
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handlerHello(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("hello")); err != nil {
log.Printf("Write failed: %v\n", err)
}
}
func main() {
mux := httpserver.NewServeMux()
mux.Get("/hello", handlerHello)
s := &http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
fmt.Print(s != nil)
}
Output: true
func (*ServeMux) Connect ¶
func (mux *ServeMux) Connect(pattern string, handlerFn http.HandlerFunc)
Connect registers the handler along the current routing path + /pattern with http method CONNECT. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Connect("/pattern", handler)
}
func (*ServeMux) Delete ¶
func (mux *ServeMux) Delete(pattern string, handlerFn http.HandlerFunc)
Delete registers the handler along the current routing path + /pattern with http method DELETE. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Delete("/pattern", handler)
}
func (*ServeMux) Get ¶
func (mux *ServeMux) Get(pattern string, handlerFn http.HandlerFunc)
Get registers the handler along the current routing path + /pattern with http method GET. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Get("/pattern", handler)
}
func (*ServeMux) Group ¶
Group adds a new inline-Router along the current routing path + /pattern, with middleware stack.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
type ctxKey struct {
name string
}
func middlewarePathValue(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ctx := context.WithValue(r.Context(), ctxKey{"id"}, id)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func handlerGetUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func handlerPutUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func main() {
mux := httpserver.NewServeMux()
muxUser := mux.Group("/user")
muxUser.Use(middlewarePathValue)
muxUser.Get("/{id}", handlerGetUser)
muxUser.Put("/{id}", handlerPutUser)
s := &http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
fmt.Print(s != nil)
}
Output: true
func (*ServeMux) Head ¶
func (mux *ServeMux) Head(pattern string, handlerFn http.HandlerFunc)
Head registers the handler along the current routing path + /pattern with http method HEAD. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Head("/pattern", handler)
}
func (*ServeMux) Method ¶
func (mux *ServeMux) Method(method, pattern string, handlerFn http.HandlerFunc)
Method registers the handler for the given along the current routing path + /pattern with custom http method by parameter. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Method("CUSTOM", "/pattern", handler)
}
func (*ServeMux) Mount ¶
Mount attaches another http.ServeMux along a /pattern/*.
WARNING: Avoid using, because it is slower to resolve the multiplexer for HTTP requests.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
type ctxKey struct {
name string
}
func middlewarePathValue(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ctx := context.WithValue(r.Context(), ctxKey{"id"}, id)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func handlerGetUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func handlerPutUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func main() {
muxUser := httpserver.NewServeMux()
muxUser.Use(middlewarePathValue)
muxUser.Get("/{id}", handlerGetUser)
muxUser.Put("/{id}", handlerPutUser)
mux := httpserver.NewServeMux()
mux.Mount("/user", muxUser)
s := &http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
fmt.Print(s != nil)
}
Output: true
func (*ServeMux) Options ¶
func (mux *ServeMux) Options(pattern string, handlerFn http.HandlerFunc)
Options registers the handler along the current routing path + /pattern with http method OPTIONS. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Options("/pattern", handler)
}
func (*ServeMux) Patch ¶
func (mux *ServeMux) Patch(pattern string, handlerFn http.HandlerFunc)
Patch registers the handler along the current routing path + /pattern with http method PATCH. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Patch("/pattern", handler)
}
func (*ServeMux) Post ¶
func (mux *ServeMux) Post(pattern string, handlerFn http.HandlerFunc)
Post registers the handler along the current routing path + /pattern with http method POST. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Post("/pattern", handler)
}
func (*ServeMux) Put ¶
func (mux *ServeMux) Put(pattern string, handlerFn http.HandlerFunc)
Put registers the handler along the current routing path + /pattern with http method PUT. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Put("/pattern", handler)
}
func (*ServeMux) Route ¶
Route mounts a sub-Router along the current routing path + /pattern, with middleware stack.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
type ctxKey struct {
name string
}
func middlewarePathValue(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ctx := context.WithValue(r.Context(), ctxKey{"id"}, id)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func handlerGetUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func handlerPutUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func main() {
mux := httpserver.NewServeMux()
mux.Route("/user", func(muxUser httpserver.Router) {
muxUser.Use(middlewarePathValue)
muxUser.Get("/{id}", handlerGetUser)
muxUser.Put("/{id}", handlerPutUser)
})
s := &http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
fmt.Print(s != nil)
}
Output: true
func (*ServeMux) Trace ¶
func (mux *ServeMux) Trace(pattern string, handlerFn http.HandlerFunc)
Trace registers the handler along the current routing path + /pattern with http method TRACE. If the given pattern conflicts with one that is already registered, Handle panics.
Example ¶
package main
import (
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
func handler(w http.ResponseWriter, r *http.Request) {}
func main() {
mux := httpserver.NewServeMux()
mux.Trace("/pattern", handler)
}
func (*ServeMux) Use ¶
Use appends one or more middlewares onto the Router stack.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
type ctxKey struct {
name string
}
func middlewarePathValue(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ctx := context.WithValue(r.Context(), ctxKey{"id"}, id)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func handlerGetUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func handlerPutUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func main() {
mux := httpserver.NewServeMux()
mux.Use(middlewarePathValue)
mux.Get("/user/{id}", handlerGetUser)
mux.Put("/user/{id}", handlerPutUser)
}
func (*ServeMux) With ¶
With adds inline middlewares for registers the handler.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"github.com/telmoandrade/go-library/httpserver"
)
type ctxKey struct {
name string
}
func middlewarePathValue(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
ctx := context.WithValue(r.Context(), ctxKey{"id"}, id)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func handlerGetUser(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value(ctxKey{"id"}).(string)
fmt.Println(id)
}
func main() {
mux := httpserver.NewServeMux()
mux.With(middlewarePathValue).Get("/user/{id}", handlerGetUser)
}