2016-12-05 07:21:17 +00:00
|
|
|
package main
|
2017-02-10 13:39:13 +00:00
|
|
|
|
2017-06-25 09:56:39 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-03 04:50:31 +00:00
|
|
|
"log"
|
2017-06-25 09:56:39 +00:00
|
|
|
"mime"
|
2017-09-03 04:50:31 +00:00
|
|
|
"strings"
|
2017-06-25 09:56:39 +00:00
|
|
|
//"errors"
|
2017-09-03 04:50:31 +00:00
|
|
|
"compress/gzip"
|
2017-06-25 09:56:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2017-09-03 04:50:31 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-25 09:56:39 +00:00
|
|
|
)
|
2016-12-05 07:21:17 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type SFile struct {
|
|
|
|
Data []byte
|
|
|
|
GzipData []byte
|
|
|
|
Pos int64
|
|
|
|
Length int64
|
|
|
|
GzipLength int64
|
|
|
|
Mimetype string
|
|
|
|
Info os.FileInfo
|
2016-12-05 07:21:17 +00:00
|
|
|
FormattedModTime string
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type CssData struct {
|
2017-06-25 09:56:39 +00:00
|
|
|
ComingSoon string
|
2016-12-05 07:21:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func initStaticFiles() error {
|
|
|
|
return filepath.Walk("./public", func(path string, f os.FileInfo, err error) error {
|
2017-06-25 09:56:39 +00:00
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
2017-06-25 09:56:39 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
path = strings.TrimPrefix(path, "public/")
|
|
|
|
var ext = filepath.Ext("/public/" + path)
|
|
|
|
gzipData := compressBytesGzip(data)
|
2017-06-25 09:56:39 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
staticFiles["/static/"+path] = SFile{data, gzipData, 0, int64(len(data)), int64(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}
|
2017-06-25 09:56:39 +00:00
|
|
|
|
2017-07-17 10:23:42 +00:00
|
|
|
if dev.DebugMode {
|
2017-06-25 09:56:39 +00:00
|
|
|
log.Print("Added the '" + path + "' static file.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2017-01-07 06:31:04 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func addStaticFile(path string, prefix string) error {
|
2017-01-07 06:31:04 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fi, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := fi.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-25 09:56:39 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
var ext = filepath.Ext(path)
|
2017-01-07 06:31:04 +00:00
|
|
|
path = strings.TrimPrefix(path, prefix)
|
2017-09-03 04:50:31 +00:00
|
|
|
gzipData := compressBytesGzip(data)
|
2017-06-25 09:56:39 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
staticFiles["/static"+path] = SFile{data, gzipData, 0, int64(len(data)), int64(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}
|
2017-06-25 09:56:39 +00:00
|
|
|
|
2017-07-17 10:23:42 +00:00
|
|
|
if dev.DebugMode {
|
2017-05-07 08:31:41 +00:00
|
|
|
log.Print("Added the '" + path + "' static file")
|
|
|
|
}
|
2017-01-07 06:31:04 +00:00
|
|
|
return nil
|
2017-02-10 13:39:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func compressBytesGzip(in []byte) []byte {
|
2017-02-10 13:39:13 +00:00
|
|
|
var buff bytes.Buffer
|
2017-06-25 09:56:39 +00:00
|
|
|
gz := gzip.NewWriter(&buff)
|
2017-09-10 16:57:22 +00:00
|
|
|
_, _ = gz.Write(in) // TODO: What if this errors? What circumstances could it error under? Should we add a second return value?
|
2017-09-03 04:50:31 +00:00
|
|
|
_ = gz.Close()
|
2017-02-10 13:39:13 +00:00
|
|
|
return buff.Bytes()
|
|
|
|
}
|