func NewFileHandler(isMaster bool, mirror string, cachedir string) func(w http.ResponseWriter, r *http.Request) { cacheGroup = groupcache.NewGroup(mirror, MAX_MEMORY_SIZE*2, groupcache.GetterFunc( func(ctx groupcache.Context, key string, dest groupcache.Sink) error { bytes, err := downloadThumbnail(mirror, cachedir, key) if err != nil { return err } dest.SetBytes(bytes) return nil })) return func(w http.ResponseWriter, r *http.Request) { key := r.URL.RequestURI() // key := r.URL.Path state.addActiveDownload(1) defer state.addActiveDownload(-1) if isMaster { // redirect to slaves if peerAddr, err := peerGroup.PeekPeer(); err == nil { u, _ := url.Parse(peerAddr) u.Path = r.URL.Path u.RawQuery = r.URL.RawQuery http.Redirect(w, r, u.String(), 302) return } } else { sendStats(r) // stats send to master } serveContent(key, cachedir, w, r) } }
"os" "os/signal" "path/filepath" "syscall" "time" "github.com/codeskyblue/groupcache" ) var ( cdnlog *log.Logger thumbNails = groupcache.NewGroup("thumbnail", 512<<20, groupcache.GetterFunc( func(ctx groupcache.Context, key string, dest groupcache.Sink) error { fileName := key bytes, err := generateThumbnail(fileName) if err != nil { return err } dest.SetBytes(bytes) return nil })) ) func generateThumbnail(key string) ([]byte, error) { u, _ := url.Parse(*mirror) u.Path = key resp, err := http.Get(u.String()) if err != nil { return nil, err } defer resp.Body.Close() return ioutil.ReadAll(resp.Body)
"math/rand" "net/http" "net/url" "os" "path/filepath" "time" "github.com/codeskyblue/groupcache" ) var ( thumbNails = groupcache.NewGroup("thumbnail", MAX_MEMORY_SIZE*2, groupcache.GetterFunc( func(ctx groupcache.Context, key string, dest groupcache.Sink) error { bytes, err := downloadThumbnail(key) if err != nil { return err } dest.SetBytes(bytes) return nil })) ) const ( // Error Response Type ER_TYPE_FILE = 1 ER_TYPE_HTML = 2 // Header Type HT_TYPE_JSON = "json" HT_TYPE_TEXT = "text"