Don't serve every static file
This commit is contained in:
parent
bb5b4a681f
commit
523b199997
32
main.go
32
main.go
|
@ -17,7 +17,6 @@ type kis3 struct {
|
||||||
db *Database
|
db *Database
|
||||||
router *mux.Router
|
router *mux.Router
|
||||||
staticBox *packr.Box
|
staticBox *packr.Box
|
||||||
staticFS http.Handler
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -31,7 +30,6 @@ func init() {
|
||||||
}
|
}
|
||||||
setupRouter()
|
setupRouter()
|
||||||
app.staticBox = packr.New("staticFiles", "./static")
|
app.staticBox = packr.New("staticFiles", "./static")
|
||||||
app.staticFS = http.FileServer(app.staticBox)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -56,7 +54,8 @@ func setupRouter() {
|
||||||
|
|
||||||
staticRouter := app.router.PathPrefix("").Subrouter()
|
staticRouter := app.router.PathPrefix("").Subrouter()
|
||||||
staticRouter.Use(corsHandler)
|
staticRouter.Use(corsHandler)
|
||||||
staticRouter.PathPrefix("").Handler(http.HandlerFunc(serveStaticFile))
|
staticRouter.HandleFunc("/kis3.js", serveTrackingScript)
|
||||||
|
staticRouter.PathPrefix("").Handler(http.HandlerFunc(sendHelloResponse))
|
||||||
}
|
}
|
||||||
|
|
||||||
func startListening() {
|
func startListening() {
|
||||||
|
@ -76,17 +75,14 @@ func trackView(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendHelloResponse(w http.ResponseWriter) {
|
func sendHelloResponse(w http.ResponseWriter, _ *http.Request) {
|
||||||
_, _ = fmt.Fprint(w, "Hello from KISSS")
|
_, _ = fmt.Fprint(w, "Hello from KISSS")
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveStaticFile(w http.ResponseWriter, r *http.Request) {
|
func serveTrackingScript(w http.ResponseWriter, _ *http.Request) {
|
||||||
uPath := r.URL.Path
|
w.Header().Add("Content-Type", "application/javascript")
|
||||||
if uPath != "/" && app.staticBox.Has(uPath) {
|
trackingScriptBytes, _ := app.staticBox.Find("kis3.js")
|
||||||
app.staticFS.ServeHTTP(w, r)
|
_, _ = w.Write(trackingScriptBytes)
|
||||||
} else {
|
|
||||||
sendHelloResponse(w)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestStats(w http.ResponseWriter, r *http.Request) {
|
func requestStats(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -159,12 +155,18 @@ func sendChartResponse(result []*RequestResultRow, w http.ResponseWriter) {
|
||||||
labels[i] = row.First
|
labels[i] = row.First
|
||||||
values[i] = row.Second
|
values[i] = row.Second
|
||||||
}
|
}
|
||||||
|
chartJSString, e := app.staticBox.FindString("Chart.min.js")
|
||||||
|
if e != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
data := struct {
|
data := struct {
|
||||||
Labels []string
|
Labels []string
|
||||||
Values []int
|
Values []int
|
||||||
|
ChartJS template.JS
|
||||||
}{
|
}{
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
Values: values,
|
Values: values,
|
||||||
|
ChartJS: template.JS(chartJSString),
|
||||||
}
|
}
|
||||||
chartTemplateString, e := app.staticBox.FindString("chart.html")
|
chartTemplateString, e := app.staticBox.FindString("chart.html")
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>KISSS</title>
|
<title>KISSS</title>
|
||||||
<script src="Chart.min.js"></script>
|
<script>{{ .ChartJS }}</script>
|
||||||
<style>
|
<style>
|
||||||
canvas {
|
canvas {
|
||||||
-moz-user-select: none;
|
-moz-user-select: none;
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>KISSS</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Stats-URL Generator</h1>
|
|
||||||
<div>
|
|
||||||
<label>
|
|
||||||
View:
|
|
||||||
<select id="view-type">
|
|
||||||
<option value="pages">Pages</option>
|
|
||||||
<option value="referrers">Referrers</option>
|
|
||||||
<option value="hours">Hours</option>
|
|
||||||
<option value="days">Days</option>
|
|
||||||
<option value="weeks">Weeks</option>
|
|
||||||
<option value="months">Months</option>
|
|
||||||
</select>
|
|
||||||
</label><br>
|
|
||||||
<label>
|
|
||||||
Format:
|
|
||||||
<select id="format-type">
|
|
||||||
<option value="plain">Plain</option>
|
|
||||||
<option value="json">JSON</option>
|
|
||||||
<option value="chart">Chart</option>
|
|
||||||
</select>
|
|
||||||
</label><br>
|
|
||||||
<label for="from-time">From:</label>
|
|
||||||
<input type="date" id="from-time"><br>
|
|
||||||
<label for="to-time">To:</label>
|
|
||||||
<input type="date" id="to-time"><br>
|
|
||||||
<button type="button" onclick="openStats()">Open stats</button>
|
|
||||||
</div>
|
|
||||||
<script>
|
|
||||||
function openStats() {
|
|
||||||
let statsUrl = new URL(document.documentURI).origin + "/stats?";
|
|
||||||
let viewType = this.document.getElementById("view-type").value;
|
|
||||||
statsUrl += "view=" + viewType + "&";
|
|
||||||
let formatType = this.document.getElementById("format-type").value;
|
|
||||||
statsUrl += "format=" + formatType + "&";
|
|
||||||
let fromTime = formatDate(this.document.getElementById("from-time").value);
|
|
||||||
if (fromTime.indexOf("NaN") < 0) statsUrl += "from=" + fromTime + "&";
|
|
||||||
let toTime = formatDate(this.document.getElementById("to-time").value);
|
|
||||||
if (toTime.indexOf("NaN") < 0) statsUrl += "to=" + toTime + "&";
|
|
||||||
console.log(statsUrl);
|
|
||||||
this.window.open(statsUrl, "_blank");
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatDate(dateString) {
|
|
||||||
let date = new Date(Date.parse(dateString));
|
|
||||||
let year = date.getFullYear();
|
|
||||||
let months = date.getMonth() + 1;
|
|
||||||
let day = date.getDate();
|
|
||||||
return year + "-" + (months < 10 ? "0" + months : months) + "-" + (day < 10 ? "0" + day : day);
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,11 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>KISSS</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
Welcome to <b>KISSS</b>
|
|
||||||
<script src="http://localhost:8080/kis3.js"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
Reference in New Issue