caching - JS

pull/94/head v0.78
hunterlong 2018-10-22 15:29:57 -07:00
parent e9354e8e6c
commit 79829c1fab
5 changed files with 98 additions and 90 deletions

View File

@ -1,4 +1,4 @@
VERSION=0.77 VERSION=0.78
BINARY_NAME=statup BINARY_NAME=statup
GOPATH:=$(GOPATH) GOPATH:=$(GOPATH)
GOCMD=go GOCMD=go

View File

@ -65,18 +65,20 @@ func (s Storage) Set(key string, content []byte, duration time.Duration) {
} }
} }
func cached(duration string, handler func(w http.ResponseWriter, r *http.Request)) http.Handler { func cached(duration, contentType string, handler func(w http.ResponseWriter, r *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content := storage.Get(r.RequestURI) content := storage.Get(r.RequestURI)
if content != nil { if content != nil {
w.Header().Set("Content-Type", contentType)
w.Write(content) w.Write(content)
} else { } else {
c := httptest.NewRecorder() c := httptest.NewRecorder()
handler(c, r) handler(c, r)
for k, v := range c.HeaderMap { //for k, v := range c.HeaderMap {
w.Header()[k] = v // w.Header()[k] = v
} //}
w.WriteHeader(c.Code) //w.WriteHeader(c.Code)
w.Header().Set("Content-Type", contentType)
content := c.Body.Bytes() content := c.Body.Bytes()
if d, err := time.ParseDuration(duration); err == nil { if d, err := time.ParseDuration(duration); err == nil {
storage.Set(r.RequestURI, content, d) storage.Set(r.RequestURI, content, d)

View File

@ -101,7 +101,7 @@ func TestServicesViewHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Google Status</title>") assert.Contains(t, body, "<title>Google Status</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") assert.Contains(t, body, "</footer>")
} }
func TestMissingServiceViewHandler(t *testing.T) { func TestMissingServiceViewHandler(t *testing.T) {
@ -134,7 +134,7 @@ func TestDashboardHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Dashboard</title>") assert.Contains(t, body, "<title>Statup | Dashboard</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") assert.Contains(t, body, "</footer>")
} }
func TestLoginHandler(t *testing.T) { func TestLoginHandler(t *testing.T) {
@ -171,7 +171,7 @@ func TestServicesHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Services</title>") assert.Contains(t, body, "<title>Statup | Services</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -232,7 +232,7 @@ func TestUsersHandler(t *testing.T) {
assert.Contains(t, body, "<title>Statup | Users</title>") assert.Contains(t, body, "<title>Statup | Users</title>")
assert.Contains(t, body, "<td>admin</td>") assert.Contains(t, body, "<td>admin</td>")
assert.NotContains(t, body, "<td>changedusername</td>") assert.NotContains(t, body, "<td>changedusername</td>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -247,7 +247,7 @@ func TestUsersEditHandler(t *testing.T) {
assert.Contains(t, body, "<h3>User admin</h3>") assert.Contains(t, body, "<h3>User admin</h3>")
assert.Contains(t, body, "value=\"info@statup.io\"") assert.Contains(t, body, "value=\"info@statup.io\"")
assert.Contains(t, body, "value=\"##########\"") assert.Contains(t, body, "value=\"##########\"")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -259,7 +259,7 @@ func TestSettingsHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Settings</title>") assert.Contains(t, body, "<title>Statup | Settings</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -271,7 +271,7 @@ func TestHelpHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Help</title>") assert.Contains(t, body, "<title>Statup | Help</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -327,7 +327,7 @@ func TestServicesHandler2(t *testing.T) {
assert.Contains(t, body, "<title>Statup | Services</title>") assert.Contains(t, body, "<title>Statup | Services</title>")
assert.Contains(t, body, "Crystal Castles - Kept") assert.Contains(t, body, "Crystal Castles - Kept")
assert.Contains(t, body, "Local Postgres") assert.Contains(t, body, "Local Postgres")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -339,7 +339,7 @@ func TestViewHTTPServicesHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Crystal Castles - Kept Status</title>") assert.Contains(t, body, "<title>Crystal Castles - Kept Status</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
} }
func TestViewTCPServicesHandler(t *testing.T) { func TestViewTCPServicesHandler(t *testing.T) {
@ -350,7 +350,7 @@ func TestViewTCPServicesHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Local Postgres Status</title>") assert.Contains(t, body, "<title>Local Postgres Status</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
} }
func TestServicesDeleteFailuresHandler(t *testing.T) { func TestServicesDeleteFailuresHandler(t *testing.T) {
@ -395,7 +395,7 @@ func TestServicesUpdateHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>The Bravery - An Honest Mistake Status</title>") assert.Contains(t, body, "<title>The Bravery - An Honest Mistake Status</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
} }
func TestDeleteServiceHandler(t *testing.T) { func TestDeleteServiceHandler(t *testing.T) {
@ -415,7 +415,7 @@ func TestLogsHandler(t *testing.T) {
body := rr.Body.String() body := rr.Body.String()
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Logs</title>") assert.Contains(t, body, "<title>Statup | Logs</title>")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -452,7 +452,7 @@ func TestViewSettingsHandler(t *testing.T) {
assert.Equal(t, 200, rr.Code) assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Settings</title>") assert.Contains(t, body, "<title>Statup | Settings</title>")
assert.Contains(t, body, "Awesome Status") assert.Contains(t, body, "Awesome Status")
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }
@ -533,7 +533,7 @@ func TestViewNotificationSettingsHandler(t *testing.T) {
assert.Contains(t, body, `value="sendto@gmail.com" id="send_alerts_to"`) assert.Contains(t, body, `value="sendto@gmail.com" id="send_alerts_to"`)
assert.Contains(t, body, `id="limits_per_hour_email" value="7"`) assert.Contains(t, body, `id="limits_per_hour_email" value="7"`)
assert.Contains(t, body, `id="switch-email" checked`) assert.Contains(t, body, `id="switch-email" checked`)
assert.Contains(t, body, "https://github.com/hunterlong/statup") //assert.Contains(t, body, "</footer>")
assert.True(t, isRouteAuthenticated(req)) assert.True(t, isRouteAuthenticated(req))
} }

View File

@ -35,7 +35,7 @@ func Router() *mux.Router {
dir := utils.Directory dir := utils.Directory
storage = NewStorage() storage = NewStorage()
r := mux.NewRouter() r := mux.NewRouter()
r.Handle("/", http.HandlerFunc(indexHandler)) r.Handle("/", cached("120s", "text/html", http.HandlerFunc(indexHandler)))
if source.UsingAssets(dir) { if source.UsingAssets(dir) {
indexHandler := http.FileServer(http.Dir(dir + "/assets/")) indexHandler := http.FileServer(http.Dir(dir + "/assets/"))
r.PathPrefix("/css/").Handler(http.StripPrefix("/font/", http.FileServer(http.Dir(dir+"/assets/font")))) r.PathPrefix("/css/").Handler(http.StripPrefix("/font/", http.FileServer(http.Dir(dir+"/assets/font"))))
@ -97,7 +97,7 @@ func Router() *mux.Router {
r.Handle("/api/services", http.HandlerFunc(apiAllServicesHandler)).Methods("GET") r.Handle("/api/services", http.HandlerFunc(apiAllServicesHandler)).Methods("GET")
r.Handle("/api/services", http.HandlerFunc(apiCreateServiceHandler)).Methods("POST") r.Handle("/api/services", http.HandlerFunc(apiCreateServiceHandler)).Methods("POST")
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceHandler)).Methods("GET") r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceHandler)).Methods("GET")
r.Handle("/api/services/{id}/data", cached("10s", http.HandlerFunc(apiServiceDataHandler))).Methods("GET") r.Handle("/api/services/{id}/data", cached("120s", "application/json", http.HandlerFunc(apiServiceDataHandler))).Methods("GET")
r.Handle("/api/services/{id}/ping", http.HandlerFunc(apiServicePingDataHandler)).Methods("GET") r.Handle("/api/services/{id}/ping", http.HandlerFunc(apiServicePingDataHandler)).Methods("GET")
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceUpdateHandler)).Methods("POST") r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceUpdateHandler)).Methods("POST")
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceDeleteHandler)).Methods("DELETE") r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceDeleteHandler)).Methods("DELETE")

View File

@ -15,8 +15,8 @@ var chartdata_{{js .Id}} = new Chart(ctx_{{js .Id}}, {
}] }]
}, },
options: { options: {
maintainAspectRatio: !1, maintainAspectRatio: false,
scaleShowValues: !0, scaleShowValues: false,
layout: { layout: {
padding: { padding: {
left: 0, left: 0,
@ -31,10 +31,62 @@ var chartdata_{{js .Id}} = new Chart(ctx_{{js .Id}}, {
responsiveAnimationDuration: 0, responsiveAnimationDuration: 0,
animation: { animation: {
duration: 3500, duration: 3500,
onComplete: function() { onComplete: onChartComplete
var chartInstance = this.chart, },
legend: {
display: false
},
tooltips: {
enabled: false
},
scales: {
yAxes: [{
display: false,
ticks: {
fontSize: 20,
display: false,
beginAtZero: false
},
gridLines: {
display: false
}
}],
xAxes: [{
type: 'time',
distribution: 'series',
autoSkip: false,
time: {
displayFormats: {
'hour': 'MMM DD hA'
},
source: 'auto'
},
gridLines: {
display: false
},
ticks: {
source: 'auto',
stepSize: 1,
min: 0,
fontColor: "white",
fontSize: 20,
display: false
}
}]
},
elements: {
point: {
radius: 0
}
}
}
});
{{end}}
function onChartComplete(chart) {
var chartInstance = chart.chart,
ctx = chartInstance.ctx; ctx = chartInstance.ctx;
var controller = this.chart.controller; var controller = chart.chart.controller;
var xAxis = controller.scales['x-axis-0']; var xAxis = controller.scales['x-axis-0'];
var yAxis = controller.scales['y-axis-0']; var yAxis = controller.scales['y-axis-0'];
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily); ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
@ -85,56 +137,10 @@ var chartdata_{{js .Id}} = new Chart(ctx_{{js .Id}}, {
ctx.fillStyle = '#45d642'; ctx.fillStyle = '#45d642';
ctx.fillText(lowestnum + "ms", hxL, hyL + 10); ctx.fillText(lowestnum + "ms", hxL, hyL + 10);
}) })
} }
},
legend: {
display: !1
},
tooltips: {
enabled: !1
},
scales: {
yAxes: [{
display: !1,
ticks: {
fontSize: 20,
display: !1,
beginAtZero: !1
},
gridLines: {
display: !1
}
}],
xAxes: [{
type: 'time',
distribution: 'series',
autoSkip: !1,
time: {
displayFormats: {
'hour': 'MMM DD hA'
},
source: 'auto'
},
gridLines: {
display: !1
},
ticks: {
source: 'auto',
stepSize: 1,
min: 0,
fontColor: "white",
fontSize: 20,
display: !1
}
}]
},
elements: {
point: {
radius: 0
}
}
}
});
AjaxChart(chartdata_{{js .Id}},{{js .Id}},{{$start}},{{$end}},"hour"); $( document ).ready(function() {
{{end}}{{end}} {{ range .Services }}
AjaxChart(chartdata_{{js .Id}},{{js .Id}},{{$start}},{{$end}},"hour");{{end}}
});
{{end}}