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
|
||||
router *mux.Router
|
||||
staticBox *packr.Box
|
||||
staticFS http.Handler
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -31,7 +30,6 @@ func init() {
|
|||
}
|
||||
setupRouter()
|
||||
app.staticBox = packr.New("staticFiles", "./static")
|
||||
app.staticFS = http.FileServer(app.staticBox)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -56,7 +54,8 @@ func setupRouter() {
|
|||
|
||||
staticRouter := app.router.PathPrefix("").Subrouter()
|
||||
staticRouter.Use(corsHandler)
|
||||
staticRouter.PathPrefix("").Handler(http.HandlerFunc(serveStaticFile))
|
||||
staticRouter.HandleFunc("/kis3.js", serveTrackingScript)
|
||||
staticRouter.PathPrefix("").Handler(http.HandlerFunc(sendHelloResponse))
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
func serveStaticFile(w http.ResponseWriter, r *http.Request) {
|
||||
uPath := r.URL.Path
|
||||
if uPath != "/" && app.staticBox.Has(uPath) {
|
||||
app.staticFS.ServeHTTP(w, r)
|
||||
} else {
|
||||
sendHelloResponse(w)
|
||||
}
|
||||
func serveTrackingScript(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/javascript")
|
||||
trackingScriptBytes, _ := app.staticBox.Find("kis3.js")
|
||||
_, _ = w.Write(trackingScriptBytes)
|
||||
}
|
||||
|
||||
func requestStats(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -159,12 +155,18 @@ func sendChartResponse(result []*RequestResultRow, w http.ResponseWriter) {
|
|||
labels[i] = row.First
|
||||
values[i] = row.Second
|
||||
}
|
||||
chartJSString, e := app.staticBox.FindString("Chart.min.js")
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
Labels []string
|
||||
Values []int
|
||||
Labels []string
|
||||
Values []int
|
||||
ChartJS template.JS
|
||||
}{
|
||||
Labels: labels,
|
||||
Values: values,
|
||||
Labels: labels,
|
||||
Values: values,
|
||||
ChartJS: template.JS(chartJSString),
|
||||
}
|
||||
chartTemplateString, e := app.staticBox.FindString("chart.html")
|
||||
if e != nil {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>KISSS</title>
|
||||
<script src="Chart.min.js"></script>
|
||||
<script>{{ .ChartJS }}</script>
|
||||
<style>
|
||||
canvas {
|
||||
-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