cdncache

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2015 License: MIT Imports: 7 Imported by: 0

README

cdncache

GoDoc

a cache for CDN files

Example

package main

import (
    "fmt"
    "gopkg.in/go-on/cdncache.v1"
    "net/http"
)

// mounts the cached files at /cdn-cache/
var cdn1 = cdncache.CDN("/cdn-cache/")

// does no caching (empty mountpoint)
var cdn2 = cdncache.CDN("")

func serve(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w,
        `<html>
        <body>
        <a href="%s">local (cached)</a><br>
        <a href="%s">remote (CDN)</a><br>
        </body>
        </html>`,
        cdn1("//code.jquery.com/jquery-1.11.0.min.js"),
        cdn2("//code.jquery.com/jquery-1.11.0.min.js"),
    )
}

func main() {
    http.Handle("/", http.HandlerFunc(serve))
    http.ListenAndServe(":8383", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CACHE_DIR = os.Getenv("CDN_CACHE_DIR")

Functions

func CDN

func CDN(mountPoint string) func(cdnUrl string) string

CDN returns a function that may be used to return either the cdnUrl or a local relative url, depending on mountPoint.

If mountPoint is empty, nothing is cached and the cdnUrl is returned.

Otherwise a fileserver is mounted at mountPoint, serving the files from cache directory located at CDN_CACHE_DIR or - if not set - at tmp/cdn_cache. Any call to the returned function will then ensure that the corresponding file was downloaded to the cache directory and thus can be served by the fileserver.

mountPoint must either be empty or begin and end with /

An invalid mountPoint or an invalid cdnUrl results in a panic.

func MuxCDN

func MuxCDN(m Muxer, mountPoint string) func(cdnUrl string) string

Types

type Muxer

type Muxer interface {
	Handle(mountpoint string, h http.Handler)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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