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 (
|
|
|
|
"log"
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"mime"
|
|
|
|
//"errors"
|
|
|
|
"os"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"net/http"
|
|
|
|
"compress/gzip"
|
|
|
|
)
|
2016-12-05 07:21:17 +00:00
|
|
|
|
|
|
|
type SFile struct
|
|
|
|
{
|
|
|
|
Data []byte
|
2017-02-10 13:39:13 +00:00
|
|
|
GzipData []byte
|
2016-12-05 07:21:17 +00:00
|
|
|
Pos int64
|
|
|
|
Length int64
|
2017-02-16 06:47:55 +00:00
|
|
|
GzipLength int64
|
2016-12-05 07:21:17 +00:00
|
|
|
Mimetype string
|
|
|
|
Info os.FileInfo
|
|
|
|
FormattedModTime string
|
|
|
|
}
|
|
|
|
|
2017-06-25 09:56:39 +00:00
|
|
|
type CssData struct
|
|
|
|
{
|
|
|
|
ComingSoon string
|
2016-12-05 07:21:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:56:39 +00:00
|
|
|
func init_static_files() {
|
|
|
|
log.Print("Loading the static files.")
|
|
|
|
err := filepath.Walk("./public", func(path string, f os.FileInfo, err error) error {
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path = strings.Replace(path,"\\","/",-1)
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path = strings.TrimPrefix(path,"public/")
|
|
|
|
var ext string = filepath.Ext("/public/" + path)
|
|
|
|
gzip_data := compress_bytes_gzip(data)
|
|
|
|
|
|
|
|
static_files["/static/" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(ext),f,f.ModTime().UTC().Format(http.TimeFormat)}
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
log.Print("Added the '" + path + "' static file.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2016-12-05 07:21:17 +00:00
|
|
|
}
|
2017-06-25 09:56:39 +00:00
|
|
|
}
|
2017-01-07 06:31:04 +00:00
|
|
|
|
|
|
|
func add_static_file(path string, prefix string) error {
|
|
|
|
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
|
|
|
|
|
|
|
var ext string = filepath.Ext(path)
|
2017-01-07 06:31:04 +00:00
|
|
|
path = strings.TrimPrefix(path, prefix)
|
2017-06-25 09:56:39 +00:00
|
|
|
gzip_data := compress_bytes_gzip(data)
|
|
|
|
|
|
|
|
static_files["/static" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(ext),f,f.ModTime().UTC().Format(http.TimeFormat)}
|
|
|
|
|
2017-05-07 08:31:41 +00:00
|
|
|
if debug {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func compress_bytes_gzip(in []byte) []byte {
|
|
|
|
var buff bytes.Buffer
|
2017-06-25 09:56:39 +00:00
|
|
|
gz := gzip.NewWriter(&buff)
|
2017-02-10 13:39:13 +00:00
|
|
|
gz.Write(in)
|
|
|
|
gz.Close()
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|