pull/236/head
Henrique Dias 2017-09-03 10:06:25 +01:00
parent 9a9b41dca8
commit 96a5226076
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
3 changed files with 2369 additions and 27 deletions

View File

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
@ -53,11 +54,7 @@ func serve(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
// Check if this request is made to the service worker. If so,
// pass it through a template to add the needed variables.
if r.URL.Path == "/sw.js" {
return renderFile(
c, w,
c.Assets.MustString("sw.js"),
"application/javascript",
)
return renderFile(c, w, "sw.js")
}
// Checks if this request is made to the static assets folder. If so, and
@ -95,11 +92,7 @@ func serve(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
w.Header().Set("x-content-type", "nosniff")
w.Header().Set("x-xss-protection", "1; mode=block")
return renderFile(
c, w,
c.Assets.MustString("index.html"),
"text/html",
)
return renderFile(c, w, "index.html")
}
// staticHandler handles the static assets path.
@ -109,11 +102,7 @@ func staticHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int,
return 0, nil
}
return renderFile(
c, w,
c.Assets.MustString("static/manifest.json"),
"application/json",
)
return renderFile(c, w, "static/manifest.json")
}
// apiHandler is the main entry point for the /api endpoint.
@ -219,8 +208,21 @@ func splitURL(path string) (string, string) {
}
// renderFile renders a file using a template with some needed variables.
func renderFile(c *fm.Context, w http.ResponseWriter, file string, contentType string) (int, error) {
tpl := template.Must(template.New("file").Parse(file))
func renderFile(c *fm.Context, w http.ResponseWriter, file string) (int, error) {
tpl := template.Must(template.New("file").Parse(c.Assets.MustString(file)))
var contentType string
switch filepath.Ext(file) {
case ".html":
contentType = "text/html"
case ".js":
contentType = "application/javascript"
case ".json":
contentType = "application/json"
default:
contentType = "text"
}
w.Header().Set("Content-Type", contentType+"; charset=utf-8")
data := map[string]interface{}{
@ -245,11 +247,8 @@ func renderFile(c *fm.Context, w http.ResponseWriter, file string, contentType s
func sharePage(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
s, err := c.Store.Share.Get(r.URL.Path)
if err == fm.ErrNotExist {
return renderFile(
c, w,
c.Assets.MustString("static/share/404.html"),
"text/html",
)
w.WriteHeader(http.StatusNotFound)
return renderFile(c, w, "static/share/404.html")
}
if err != nil {
@ -258,11 +257,8 @@ func sharePage(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, erro
if s.Expires && s.ExpireDate.Before(time.Now()) {
c.Store.Share.Delete(s.Hash)
return renderFile(
c, w,
c.Assets.MustString("static/share/404.html"),
"text/html",
)
w.WriteHeader(http.StatusNotFound)
return renderFile(c, w, "static/share/404.html")
}
r.URL.Path = s.Path

View File

@ -44,6 +44,10 @@ func shareGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int
}
}
if len(s) == 0 {
return http.StatusNotFound, nil
}
return renderJSON(w, s)
}

2342
rice-box.go Normal file

File diff suppressed because one or more lines are too long