2015-05-15 23:18:52 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2016-01-25 05:01:40 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
2015-05-15 23:18:52 +00:00
|
|
|
"io/ioutil"
|
2016-01-25 00:01:37 +00:00
|
|
|
"log"
|
2015-05-15 23:18:52 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2015-05-25 02:00:21 +00:00
|
|
|
|
2016-01-25 05:01:40 +00:00
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/viper"
|
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/assets"
|
2015-05-15 23:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var files = []File{
|
2016-01-25 05:01:40 +00:00
|
|
|
File{
|
|
|
|
Path: "bundle.js",
|
|
|
|
Asset: "bundle.js.gz",
|
|
|
|
ContentType: "text/javascript",
|
|
|
|
CacheControl: "max-age=31536000",
|
|
|
|
},
|
|
|
|
File{
|
|
|
|
Path: "bundle.css",
|
|
|
|
Asset: "bundle.css.gz",
|
|
|
|
ContentType: "text/css",
|
|
|
|
CacheControl: "max-age=31536000",
|
|
|
|
},
|
|
|
|
File{
|
|
|
|
Path: "font/fontello.woff",
|
|
|
|
Asset: "font/fontello.woff.gz",
|
|
|
|
ContentType: "application/font-woff",
|
|
|
|
},
|
|
|
|
File{
|
|
|
|
Path: "font/fontello.ttf",
|
|
|
|
Asset: "font/fontello.ttf.gz",
|
|
|
|
ContentType: "application/x-font-ttf",
|
|
|
|
},
|
|
|
|
File{
|
|
|
|
Path: "font/fontello.eot",
|
|
|
|
Asset: "font/fontello.eot.gz",
|
|
|
|
ContentType: "application/vnd.ms-fontobject",
|
|
|
|
},
|
|
|
|
File{
|
|
|
|
Path: "font/fontello.svg",
|
|
|
|
Asset: "font/fontello.svg.gz",
|
|
|
|
ContentType: "image/svg+xml",
|
|
|
|
},
|
2015-05-15 23:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
2016-01-25 05:01:40 +00:00
|
|
|
Path string
|
|
|
|
Asset string
|
|
|
|
ContentType string
|
|
|
|
CacheControl string
|
|
|
|
}
|
|
|
|
|
|
|
|
func initFileServer() {
|
|
|
|
if !viper.GetBool("dev") {
|
|
|
|
data, err := assets.Asset(files[0].Asset)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := md5.Sum(data)
|
|
|
|
files[0].Path = "bundle." + base64.RawURLEncoding.EncodeToString(hash[:]) + ".js"
|
|
|
|
|
|
|
|
data, err = assets.Asset(files[1].Asset)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = md5.Sum(data)
|
|
|
|
files[1].Path = "bundle." + base64.RawURLEncoding.EncodeToString(hash[:]) + ".css"
|
|
|
|
}
|
2015-05-15 23:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serveFiles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
2016-01-25 00:01:37 +00:00
|
|
|
serveIndex(w, r)
|
2015-05-15 23:18:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-15 01:27:30 +00:00
|
|
|
if strings.HasSuffix(r.URL.Path, "favicon.ico") {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:18:52 +00:00
|
|
|
for _, file := range files {
|
|
|
|
if strings.HasSuffix(r.URL.Path, file.Path) {
|
2016-01-25 05:01:40 +00:00
|
|
|
if file.CacheControl != "" {
|
|
|
|
w.Header().Set("Cache-Control", file.CacheControl)
|
|
|
|
}
|
|
|
|
|
|
|
|
serveFile(w, r, file.Asset, file.ContentType)
|
2015-05-15 23:18:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 00:01:37 +00:00
|
|
|
serveIndex(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveIndex(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := handleAuth(w, r)
|
|
|
|
if session == nil {
|
|
|
|
log.Println("[Auth] No session")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:01:40 +00:00
|
|
|
w.Header().Set("Cache-Control", "no-store")
|
2016-01-25 00:01:37 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
gzw := gzip.NewWriter(w)
|
|
|
|
renderIndex(gzw, session)
|
|
|
|
gzw.Close()
|
|
|
|
} else {
|
|
|
|
renderIndex(w, session)
|
|
|
|
}
|
2015-05-15 23:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serveFile(w http.ResponseWriter, r *http.Request, path, contentType string) {
|
2015-05-25 02:00:21 +00:00
|
|
|
info, err := assets.AssetInfo(path)
|
2015-05-15 23:18:52 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !modifiedSince(w, r, info.ModTime()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-25 02:00:21 +00:00
|
|
|
data, err := assets.Asset(path)
|
2015-05-15 23:18:52 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
|
|
|
w.Write(data)
|
|
|
|
} else {
|
|
|
|
gzr, err := gzip.NewReader(bytes.NewReader(data))
|
|
|
|
buf, err := ioutil.ReadAll(gzr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func modifiedSince(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
|
|
|
|
t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since"))
|
|
|
|
|
|
|
|
if err == nil && modtime.Before(t.Add(1*time.Second)) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
|
|
|
|
return true
|
|
|
|
}
|