2013-03-18 16:48:50 +00:00
|
|
|
package blob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2013-04-09 10:12:24 +00:00
|
|
|
"fmt"
|
2013-03-18 16:48:50 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2013-03-22 11:03:31 +00:00
|
|
|
"strings"
|
2013-03-18 16:48:50 +00:00
|
|
|
)
|
|
|
|
|
2013-03-19 15:11:55 +00:00
|
|
|
const (
|
|
|
|
TemplateFiles = "templates"
|
2013-03-19 15:28:55 +00:00
|
|
|
StaticFiles = "static"
|
2013-03-19 15:11:55 +00:00
|
|
|
)
|
|
|
|
|
2013-03-22 11:03:31 +00:00
|
|
|
var mimeMap = map[string]string{
|
2013-04-24 16:51:07 +00:00
|
|
|
"css": "text/css",
|
|
|
|
"js": "text/javascript",
|
|
|
|
"descriptor": "application/vnd.google.protobuf;proto=google.protobuf.FileDescriptorSet",
|
2013-03-22 11:03:31 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 16:48:50 +00:00
|
|
|
func GetFile(bucket string, name string) ([]byte, error) {
|
2013-04-09 10:12:24 +00:00
|
|
|
blob, ok := files[bucket][name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Could not find %s/%s. Missing/updated files.go?", bucket, name)
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(blob)
|
2013-03-18 16:48:50 +00:00
|
|
|
gz, err := gzip.NewReader(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
io.Copy(&b, gz)
|
|
|
|
gz.Close()
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Handler struct{}
|
|
|
|
|
|
|
|
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2013-04-09 10:12:24 +00:00
|
|
|
name := r.URL.Path
|
2013-03-18 16:48:50 +00:00
|
|
|
if name == "" {
|
|
|
|
name = "index.html"
|
|
|
|
}
|
|
|
|
|
2013-03-19 15:11:55 +00:00
|
|
|
file, err := GetFile(StaticFiles, name)
|
2013-03-18 16:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("Could not get file: %s", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2013-03-22 11:03:31 +00:00
|
|
|
contentType := http.DetectContentType(file)
|
|
|
|
if strings.Contains(contentType, "text/plain") || strings.Contains(contentType, "application/octet-stream") {
|
|
|
|
parts := strings.Split(name, ".")
|
|
|
|
contentType = mimeMap[parts[len(parts)-1]]
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
2013-03-18 16:48:50 +00:00
|
|
|
w.Write(file)
|
|
|
|
}
|