netkit

package module
v0.0.0-...-08f880a Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 5 Imported by: 2

README

===== netkit =====

netkit is a collection of tools that help me with networking, particularly HTTP
based applications.

    go get git.jnichols.info/netkit

===== router =====

netkit contains some tools for routing HTTP requests based on traits like path
or method.

    route := netkit.HandleFunc
    router := netkit.Router(
        route("/", handler),
        route("/static/path", handler),
        route("/:param/path", handler),
        route("/open/path/", handler),
    )

Use netkit.Router to use handlers based on a request's URL path. If a path
segment starts with :, it is stored as a parameter and can be accessed with
netkit.URIParam(r *http.Request, key string). If a path ends with /, it will
match the exact route AND any extensions, and the trailing part of the path
can be accessed with netkit.URITrail(r).

    route := netkit.Handle
    method := netkit.MethodFunc
    router := netkit.Router(
        route("/", netkit.Methods(
            method(http.MethodGet, handler),
            method(http.MethodPost, handler),
        ))
    )

netkit.Router doesn't handle methods by default. You can chain it with
netkit.Methods to route based on method after matching a path.

Documentation

Overview

Copyright 2024 Jake Nichols (MIT License)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Router

func Router(routes ...RouteType) (out http.Handler)

Router returns an http.Handler that, for any input request r, forwards to the handler at r.URL.Path.

func URIParam

func URIParam(r *http.Request, k string) (v string)

func URITrail

func URITrail(r *http.Request) (v string)

Types

type Methods

type Methods map[string]http.Handler

func (Methods) ServeHTTP

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

type RouteType

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

func Handle

func Handle(path string, handler http.Handler) (r RouteType)

Handle returns a RouteType with the path and handler.

func HandleFunc

func HandleFunc(path string, handler http.HandlerFunc) (r RouteType)

HandleFunc returns a RouteType with the path and handler function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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