Skip to content

Commit e31c017

Browse files
committed
Stop using CDNs + Remove bootstrap.min.js because it wasn't necessary
+ Gzip http.FileServer
1 parent ed4ed52 commit e31c017

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

gzip.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"compress/gzip"
5+
"io"
6+
"io/ioutil"
7+
"net/http"
8+
"strings"
9+
"sync"
10+
)
11+
12+
var gzPool = sync.Pool{
13+
New: func() interface{} {
14+
return gzip.NewWriter(ioutil.Discard)
15+
},
16+
}
17+
18+
type gzipResponseWriter struct {
19+
io.Writer
20+
http.ResponseWriter
21+
}
22+
23+
func (w *gzipResponseWriter) WriteHeader(status int) {
24+
w.Header().Del("Content-Length")
25+
w.ResponseWriter.WriteHeader(status)
26+
}
27+
28+
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
29+
return w.Writer.Write(b)
30+
}
31+
32+
func GzipHandler(next http.Handler) http.Handler {
33+
return http.HandlerFunc(func(writer http.ResponseWriter, r *http.Request) {
34+
// If the request doesn't specify that it supports gzip, then don't compress it
35+
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
36+
next.ServeHTTP(writer, r)
37+
return
38+
}
39+
writer.Header().Set("Content-Encoding", "gzip")
40+
gz := gzPool.Get().(*gzip.Writer)
41+
defer gzPool.Put(gz)
42+
gz.Reset(writer)
43+
defer gz.Close()
44+
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: writer, Writer: gz}, r)
45+
})
46+
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525
cfg := loadConfiguration()
2626
http.HandleFunc("/api/v1/results", serviceResultsHandler)
2727
http.HandleFunc("/health", healthHandler)
28-
http.Handle("/", http.FileServer(http.Dir("./static")))
28+
http.Handle("/", GzipHandler(http.FileServer(http.Dir("./static"))))
2929
if cfg.Metrics {
3030
http.Handle("/metrics", promhttp.Handler())
3131
}

static/bootstrap.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/index.html

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>Health Dashboard</title>
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
6-
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
6+
<link rel="stylesheet" href="/bootstrap.min.css" />
77
<style>
88
html, body {
99
background-color: #f7f9fb;
@@ -22,13 +22,6 @@
2222
border-color: #dee2e6;
2323
border-style: solid;
2424
}
25-
.status-ok {
26-
display: inline-block;
27-
width: 1%;
28-
height: 20px;
29-
margin-right: 4px;
30-
background-color: #28a745;
31-
}
3225
.status {
3326
cursor: pointer;
3427
transition: all 500ms ease-in-out;
@@ -93,7 +86,7 @@
9386
<div class="title display-4">Health Status</div>
9487
</div>
9588
<div class="col-4 text-right">
96-
<img src="logo.png" alt="GaTuS" style="position: relative; min-width: 50px; max-width: 200px; width: 20%;"/>
89+
<img src="logo.png" alt="Gatus" style="position: relative; min-width: 50px; max-width: 200px; width: 20%;"/>
9790
</div>
9891
</div>
9992
</div>
@@ -113,8 +106,7 @@
113106
</div>
114107
</div>
115108

116-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
117-
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
109+
<script src="/jquery.min.js"></script>
118110

119111
<script>
120112
let serviceStatuses = {};

static/jquery.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/logo-candidate.png

-59 KB
Binary file not shown.

0 commit comments

Comments
 (0)