prohttphandler

package module
v0.0.0-...-7d7ff70 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2014 License: MIT Imports: 4 Imported by: 1

README

ProHttpHandler

A teeny tiny http handler for Go that I wanted to reuse across a couple of projects.

It gives you two things: static asset handling (in a path of your choice) and exact matching of paths to http handler funcs

Example

An example that serves all files in "public" at / and serves up a super boring homepage on port 8080.

package main

import (
  "fmt"
  "github.com/joho/prohttphandler"
  "log"
  "net/http"
)

func main() {
  handler := prohttphandler.New("public")

  handler.ExactMatchFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "This is the homepage at /")
  })

  log.Fatal(http.ListenAndServe(":8080", handler))
}

There are some very poor docs here


Copyright 2013 John Barton (but under MIT Licence)

Documentation

Overview

A totally pro http handler. One that isn't particularly pro at all, it was just handy and I needed it in multiple projects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ProHttpHandler

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

The actuall http handler

func New

func New(publicStaticAssetPath string) (h *ProHttpHandler)

Create a ProHttpHandler You need to give it a path to use as the static assets root

func (*ProHttpHandler) ExactMatchFunc

func (h *ProHttpHandler) ExactMatchFunc(path string, handlerFunc func(http.ResponseWriter, *http.Request))

Registers a http handler func for the path you specify

func (*ProHttpHandler) ServeHTTP

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

ServeHTTP will do one of three things:

  • It will check if there is a handler func registered for the *exact* path of the request
  • If there is no exact match, it will try and find a file in the static asset path to serve (no dir listings though)
  • If neither of the above, it will 404 in an ugly way

If the client sends an Accept-Encoding of gzip, then they'll get it gzipped. Keep an eye out for weird Content-Type issues if you're gzipping, as this will try and auto-guess the content type and may not be 100% accurate.

Jump to

Keyboard shortcuts

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