Fix #227
parent
9a9b41dca8
commit
96a5226076
50
http/http.go
50
http/http.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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,
|
// Check if this request is made to the service worker. If so,
|
||||||
// pass it through a template to add the needed variables.
|
// pass it through a template to add the needed variables.
|
||||||
if r.URL.Path == "/sw.js" {
|
if r.URL.Path == "/sw.js" {
|
||||||
return renderFile(
|
return renderFile(c, w, "sw.js")
|
||||||
c, w,
|
|
||||||
c.Assets.MustString("sw.js"),
|
|
||||||
"application/javascript",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if this request is made to the static assets folder. If so, and
|
// 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-content-type", "nosniff")
|
||||||
w.Header().Set("x-xss-protection", "1; mode=block")
|
w.Header().Set("x-xss-protection", "1; mode=block")
|
||||||
|
|
||||||
return renderFile(
|
return renderFile(c, w, "index.html")
|
||||||
c, w,
|
|
||||||
c.Assets.MustString("index.html"),
|
|
||||||
"text/html",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// staticHandler handles the static assets path.
|
// 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 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderFile(
|
return renderFile(c, w, "static/manifest.json")
|
||||||
c, w,
|
|
||||||
c.Assets.MustString("static/manifest.json"),
|
|
||||||
"application/json",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// apiHandler is the main entry point for the /api endpoint.
|
// 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.
|
// 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) {
|
func renderFile(c *fm.Context, w http.ResponseWriter, file string) (int, error) {
|
||||||
tpl := template.Must(template.New("file").Parse(file))
|
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")
|
w.Header().Set("Content-Type", contentType+"; charset=utf-8")
|
||||||
|
|
||||||
data := map[string]interface{}{
|
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) {
|
func sharePage(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
s, err := c.Store.Share.Get(r.URL.Path)
|
s, err := c.Store.Share.Get(r.URL.Path)
|
||||||
if err == fm.ErrNotExist {
|
if err == fm.ErrNotExist {
|
||||||
return renderFile(
|
w.WriteHeader(http.StatusNotFound)
|
||||||
c, w,
|
return renderFile(c, w, "static/share/404.html")
|
||||||
c.Assets.MustString("static/share/404.html"),
|
|
||||||
"text/html",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
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()) {
|
if s.Expires && s.ExpireDate.Before(time.Now()) {
|
||||||
c.Store.Share.Delete(s.Hash)
|
c.Store.Share.Delete(s.Hash)
|
||||||
return renderFile(
|
w.WriteHeader(http.StatusNotFound)
|
||||||
c, w,
|
return renderFile(c, w, "static/share/404.html")
|
||||||
c.Assets.MustString("static/share/404.html"),
|
|
||||||
"text/html",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r.URL.Path = s.Path
|
r.URL.Path = s.Path
|
||||||
|
|
|
@ -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)
|
return renderJSON(w, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue