Documentation ¶
Overview ¶
Package k(atalysator) is a bag of small, uncategorized code supposed to ease the development of applications in go.
As a side-effect, import package k will change the log package’s default flag to log.LstdFlags | log.Lshortfile.
Index ¶
- Constants
- func DefaultEnv(key, def string) string
- func HTTPLogger(h http.Handler, logFunc HTTPLoggerFunc) http.Handler
- func Hostname(def string) string
- func JsonRemarshal(new interface{}, old interface{}) error
- func MustBytes(data []byte, err error) []byte
- func MustTCPAddr(rawaddr string) *net.TCPAddr
- func MustURL(rawurl string) *url.URL
- func NewSingleHostReverseProxy(url *url.URL) *httputil.ReverseProxy
- func RandomString(n int) string
- type Cache
- type GridDir
- type HTTPLoggerFunc
- type StringArray
Constants ¶
const (
// Per default items up to 20 Mibibytes in size will be cached
DEFAULT_MAX_CACHEABLE_SIZE = 20 * (1 << (10 * 2))
)
const (
VERSION = "0.7.2"
)
Variables ¶
This section is empty.
Functions ¶
func DefaultEnv ¶
DefaultEnv returns the value of an environment variable, provided it has been set. If it is unset (i.e. empty), the specified default value is returned.
func HTTPLogger ¶
func HTTPLogger(h http.Handler, logFunc HTTPLoggerFunc) http.Handler
HTTPLogger wraps an http.Handler and calls a function logFunc with a line that can be written to an HTTP access log when a request to the handler has been completed.
func Hostname ¶
Hostname returns def if no hostname could be determined for this machine, the machine's hostname otherwise.
func JsonRemarshal ¶
func JsonRemarshal(new interface{}, old interface{}) error
JsonRemarshal takes old, marshals it into json and unmarshals it into new. If an error occurs along the way, it is returned.
func MustBytes ¶
MustBytes is a function which usually wraps functions which return a byte slice and an error. It panics, if the given error is not nil.
func MustTCPAddr ¶
MustTCPAddr calls net.ResolveTCPAddr and panics if it returns a non-nil error.
func NewSingleHostReverseProxy ¶
func NewSingleHostReverseProxy(url *url.URL) *httputil.ReverseProxy
NewSingleHostReverseProxy is an augmentation of `net/http/httputil.NewSingleHostReverseProxy` which additionally sets the `Host` header.
func RandomString ¶
RandomString generates a random alpha-numeric string of length n
Types ¶
type Cache ¶
type Cache struct { // Time for which a answer is held im memory. A TTL <= 0 // makes the Cache pass all requests directly to the handler. TTL time.Duration // Handler, whose responses are supposed to be cached Handler http.Handler // Maximum number of bytes, with which an response is eligible // for caching (default: 20MiB) MaxCacheableSize int64 // contains filtered or unexported fields }
Cache is a `net/http.Handler`, which uses `net/http/httptest.ResponseRecorder` to record and recall the responses of the wrapped handler from memory if the same URL is requested again. The `net/http.Request.URL.Path` is used as the caching key.
type GridDir ¶
type GridDir struct { GridFS *mgo.GridFS // If true, the leading slash will be stripped from requests StripSlash bool }
GridDir implements net/http.FileSystem to serve contents directly from MongoDB's GridFS.
To serve all files from a database containing a GridFS called `fs` via http:
session, err := mgo.Dial("mongodb://localhost/database") if err != nil { log.Fatalf("Could not connect to mongodb: %s", err) } gfs := session.DB("").GridFS("fs") http.Handle("/", http.FileServer(sabercat.GridDir{gfs})
Directory listing has not been implemented.
type HTTPLoggerFunc ¶
type HTTPLoggerFunc func(logLine string)
type StringArray ¶
type StringArray []string
StringArray is an alias for an array of strings which has common operations defined as methods.
func (StringArray) Contains ¶
func (s StringArray) Contains(needle string) bool
Contains returns true if the receiver array contains an element equivalent to needle.
func (StringArray) String ¶
func (s StringArray) String() string
String returns the JSON representation of the array.