Fragment Description:



HTTP caching in Go.
Setting some Headers:
Etag, Cache-Control.
This example by Santiago Arias.
His introductory post:
http://www.sanarias.com/blog/115LearningHTTPcachinginGo

httpCaching

Last update, on 2015, Fri 9 Oct, 16:15:37

/* ... <== see fragment description ... */

package main

import (
    "bytes"
    "flag"
    "image"
    "image/color"
    "image/draw"
    "image/jpeg"
    "log"
    "net/http"
    "strconv"
    "strings"
)

var root = flag.String("root", ".", "file system path")

func blackHandler(w http.ResponseWriter, r *http.Request) {
    // Caching here: using Etag and max-age
    key := "black"
    e := `"` + key + `"`
    w.Header().Set("Etag", e)
    w.Header().Set("Cache-Control", "max-age=2592000") // 30 days
    if match := r.Header.Get("If-None-Match"); match != "" {
        if strings.Contains(match, e) {
            w.WriteHeader(http.StatusNotModified)
            return
        }
    }
    m := image.NewRGBA(image.Rect(0, 0, 240, 240))
    black := color.RGBA{0, 0, 0, 255}
    draw.Draw(m, m.Bounds(), &image.Uniform{black}, image.ZP, draw.Src)
    var img image.Image = m
    writeImage(w, &img)
}
func main() {
    http.HandleFunc("/black/", blackHandler)
    http.Handle("/", http.FileServer(http.Dir(*root)))
    log.Println("Listening on 3000")
    err := http.ListenAndServe(":3000", nil)
    if err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

// writeImage encodes an image in jpeg format and writes it into
// ResponseWriter.
func writeImage(w http.ResponseWriter, img *image.Image) {
    buffer := new(bytes.Buffer)
    if err := jpeg.Encode(buffer, *img, nil); err != nil {
        log.Println("unable to encode image.")
    }
    w.Header().Set("Content-Type", "image/jpeg")
    w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))
    if _, err := w.Write(buffer.Bytes()); err != nil {
        log.Println("unable to write image.")
    }
}



Comments