Localise all external resources so that application can be used without requiring clearnet connectivity

Add gzip handler so that assets can be compressed if clients request it
Add cache-control headers so that content can be effectively cached locally
This commit is contained in:
Simon Marsh 2020-10-24 14:47:18 +01:00
parent 547e7fdc04
commit c6a608061e
No known key found for this signature in database
GPG Key ID: 30B29A716A54DBB3
19 changed files with 124 additions and 13 deletions

3
StaticRoot/axios.min.js vendored Normal file

File diff suppressed because one or more lines are too long

7
StaticRoot/bootstrap.bundle.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

BIN
StaticRoot/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -4,8 +4,9 @@
<title>DN42 Registry Explorer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="icon" type="image/png" href="favicon.png"/>
<link rel="stylesheet" href="bootstrap.min.css"/>
<link rel="stylesheet" href="material-icons.css"/>
<!-- Style overrides -->
<style>
.material-icons { display:inline-flex;vertical-align:middle }
@ -205,13 +206,12 @@ body { box-shadow: inset 0 2em 10em rgba(0,0,0,0.4); min-height: 100vh }
</span>
</script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<script src="https://unpkg.com/vue"></script -->
<script src="https://unpkg.com/vue-router"></script -->
<script src="https://unpkg.com/axios@0.18.0/dist/axios.min.js"></script>
<!-- script src="https://unpkg.com/vue@2.6.2/dist/vue.min.js"></script -->
<!-- script src="https://unpkg.com/vue-router@3.0.2/dist/vue-router.js"></script -->
<script src="jquery.slim.min.js"></script>
<script src="bootstrap.bundle.min.js"></script>
<script src="vue.min.js"></script>
<script src="vue-router.min.js"></script>
<script src="axios.min.js"></script>
<script src="anchorme.min.js"></script>
<script src="anchorme.min.js"></script>
<script src="explorer.js"></script>
</body>

2
StaticRoot/jquery.slim.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,20 @@
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(/material-icons.ttf) format('truetype');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
@font-face {
font-family: 'Source Sans Pro';
font-style: normal;
font-weight: 400;
src: local('Source Sans Pro Regular'), local('SourceSansPro-Regular'), url(source-sans-pro.ttf) format('truetype');
}

Binary file not shown.

6
StaticRoot/vue-router.min.js vendored Normal file

File diff suppressed because one or more lines are too long

6
StaticRoot/vue.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@ package main
import (
"context"
"encoding/json"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
@ -143,8 +144,9 @@ func main() {
// initialise router
router := mux.NewRouter()
// log all access
// global handers, log all requests and allow compression
router.Use(requestLogger)
router.Use(handlers.CompressHandler)
// add API routes
subr := router.PathPrefix("/api").Subrouter()

View File

@ -89,6 +89,9 @@ func dnsRZoneHandler(w http.ResponseWriter, r *http.Request) {
format = []string{"json"}
}
// cache for up to a day
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
switch format[0] {
case "bind":
DNSRootZone.WriteBindFormat(w)

1
go.mod
View File

@ -3,6 +3,7 @@ module burble.dn42/dn42regsrv
go 1.14
require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.7.4
github.com/sirupsen/logrus v1.6.0
github.com/spf13/pflag v1.0.5

4
go.sum
View File

@ -1,4 +1,8 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

View File

@ -63,6 +63,8 @@ func regMetaHandler(w http.ResponseWriter, r *http.Request) {
Commit: RegistryData.Commit,
}
// don't cache
w.Header().Set("Cache-Control", "no-store")
ResponseJSON(w, rv)
}
@ -311,6 +313,11 @@ func regRootHandler(w http.ResponseWriter, r *http.Request) {
for _, rType := range RegistryData.Types {
response[rType.Ref] = len(rType.Objects)
}
// cache for up to a day, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
w.Header().Set("ETag", RegistryData.Commit)
ResponseJSON(w, response)
}
@ -344,6 +351,10 @@ func regTypeHandler(w http.ResponseWriter, r *http.Request) {
response[rtype.Ref] = objects
}
// cache for up to a day, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
w.Header().Set("ETag", RegistryData.Commit)
ResponseJSON(w, response)
}
@ -409,6 +420,10 @@ func regObjectHandler(w http.ResponseWriter, r *http.Request) {
}
}
// cache for up to a day, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
w.Header().Set("ETag", RegistryData.Commit)
ResponseJSON(w, response)
} else {
@ -427,6 +442,10 @@ func regObjectHandler(w http.ResponseWriter, r *http.Request) {
}
}
// cache for up to a day, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
w.Header().Set("ETag", RegistryData.Commit)
ResponseJSON(w, response)
}
@ -478,6 +497,10 @@ func regKeyHandler(w http.ResponseWriter, r *http.Request) {
return
}
// cache for up to a day, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
w.Header().Set("ETag", RegistryData.Commit)
ResponseJSON(w, amap)
}
@ -528,6 +551,10 @@ func regAttributeHandler(w http.ResponseWriter, r *http.Request) {
return
}
// cache for up to a day, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=86400, stale-if-error=86400")
w.Header().Set("ETag", RegistryData.Commit)
ResponseJSON(w, amap)
}

View File

@ -130,6 +130,10 @@ func roaFilterHandler(w http.ResponseWriter, r *http.Request) {
filters = fselect(filters, 6)
}
// cache for up to a week, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=604800, stale-if-error=86400")
w.Header().Set("ETag", ROAData.Commit)
ResponseJSON(w, filters)
}
@ -147,6 +151,10 @@ func roaJSONHandler(w http.ResponseWriter, r *http.Request) {
ROAJSONResponse.MetaData.Valid += (ROA_JSON_VALIDITY_PERIOD * 3600)
}
// cache for up to a week, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=604800, stale-if-error=86400")
w.Header().Set("ETag", ROAData.Commit)
ResponseJSON(w, ROAJSONResponse)
}
@ -174,6 +182,10 @@ func roaBirdHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
// cache for up to a week, but set etag to commit to catch changes
w.Header().Set("Cache-Control", "public, max-age=604800, stale-if-error=86400")
w.Header().Set("ETag", ROAData.Commit)
fmt.Fprintf(w, "#\n# dn42regsrv ROA Generator\n# Last Updated: %s\n"+
"# Commit: %s\n#\n", ROAData.CTime.String(), ROAData.Commit)

View File

@ -42,8 +42,7 @@ func InstallStaticRoutes(router *mux.Router, staticPath string) {
}
// install a file server for the static route
router.PathPrefix("/").Handler(http.StripPrefix("/",
http.FileServer(http.Dir(staticPath)))).Methods("GET")
router.PathPrefix("/").Handler(staticHandler(staticPath))
log.WithFields(log.Fields{
"path": staticPath,
@ -51,5 +50,18 @@ func InstallStaticRoutes(router *mux.Router, staticPath string) {
}
//////////////////////////////////////////////////////////////////////////
func staticHandler(path string) http.Handler {
server := http.FileServer(http.Dir(path))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// allow up to a month of caching
w.Header().Set("Cache-Control", "public, max-age=2592000, stale-if-error=86400")
server.ServeHTTP(w, r)
})
}
//////////////////////////////////////////////////////////////////////////
// end of code