gum: willnorris.com/go/gum Index | Files

package gum

import "willnorris.com/go/gum"

Package gum provides the gum personal short URL resolver.

Index

Package Files

gum.go redirect.go static.go

type Handler Uses

type Handler interface {
    // Register the handler with the provided ServeMux.  This method will be
    // called when the handler is added to the mux, and allows the
    // handler to specify the kinds of short URLs it can handle.
    // Typically, but not always, this will be URLs of the form "/x" and
    // /x/*" where x is a particular content type.
    Register(*http.ServeMux) error

    // Mappings provides a write only channel for the handler to write
    // static Mapping values onto.  These mappings are then registered with
    // and the redirects handled by the Server.
    Mappings(chan<- Mapping) error
}

A Handler serves requests for short URLs. Typically, a handler will register itself for an entire path prefix using the Register func, or provide a list of static mappings using the Mappings func.

type Mapping Uses

type Mapping struct {
    // ShortPath is the path of the short URL (including leading slash) to
    // be mapped.
    ShortPath string

    // Permalink is the destination URL being mapped to.
    Permalink string
}

Mapping represents a mapping between a short URL path and the permalink URL it is for.

type RedirectHandler Uses

type RedirectHandler struct {
    // Prefix is the path component prefix this handler should handle.
    // Prefix should not contain leading or trailing slashes.
    Prefix string

    // Destination is the base URL to redirect requests to.  If Destination
    // includes a path, it should typically include a trailing slash.
    Destination *url.URL

    // Status is the HTTP status to return in redirect responses.
    Status int
}

RedirectHandler redirects requests that match a given path component prefix to a specified destination base URL. For example, given a RedirectHandler:

RedirectHandler{
    Prefix: "x",
    Destination: "http://example/",
}

The following URLs would be redirected:

/x          =>  http://example/
/x/         =>  http://example/
/x/a/b?c=d  =>  http://example/a/b?c=d

The request URL "/x123" would not be handled by this handler.

func NewRedirectHandler Uses

func NewRedirectHandler(prefix, destination string) (*RedirectHandler, error)

NewRedirectHandler constructs a new RedirectHandler with the specified prefix and destination, and a 301 (Moved Permanently) response status.

func (*RedirectHandler) Mappings Uses

func (h *RedirectHandler) Mappings(mappings chan<- Mapping) error

Mappings implements the Handler interface.

func (*RedirectHandler) Register Uses

func (h *RedirectHandler) Register(mux *http.ServeMux) error

Register this handler with the provided ServeMux.

func (*RedirectHandler) ServeHTTP Uses

func (h *RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Server Uses

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

Server is a short URL redirection server.

func NewServer Uses

func NewServer() *Server

NewServer constructs a new Server.

func (*Server) AddHandler Uses

func (s *Server) AddHandler(h Handler) error

AddHandler adds the provided Handler to the server.

func (*Server) ServeHTTP Uses

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type StaticHandler Uses

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

StaticHandler handles short URLs parsed from static HTML files. Files are parsed and searched for rel="shortlink" and rel="canonical" links. If both are found, a redirect is registered for the pair.

func NewStaticHandler Uses

func NewStaticHandler(base string) (*StaticHandler, error)

NewStaticHandler constructs a new StaticHandler with the specified base path of HTML files.

func (*StaticHandler) Mappings Uses

func (h *StaticHandler) Mappings(mappings chan<- Mapping) error

Mappings implements Handler.

func (*StaticHandler) Register Uses

func (h *StaticHandler) Register(mux *http.ServeMux) error

Register is a noop for this handler.

Package gum imports 13 packages (graph). Updated 2018-10-24. Refresh now. Tools for package owners.