diff --git a/main.go b/main.go index e536166..d1dd178 100644 --- a/main.go +++ b/main.go @@ -1,51 +1,56 @@ package main import ( - "log" - "net/http" - "text/template" + "log" + "net/http" + "text/template" ) type TemplateData struct { - Title string - Description string - URL string + Title string + Description string + URL string + IncludeServiceWorker bool } func main() { - mainTemplate, err := template.ParseFiles("./templates/main.tmpl") - if err != nil { - log.Fatal("Error: Unable to parse the template file.") - } - - http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - if req.URL.Path == "/smatter" { - data := TemplateData { - Title: "Do Blue Lives Matter?", - Description: "No", - URL: "https://doblue.live/smatter", - } - - mainTemplate.Execute(w, data) - } else if req.URL.Path == "/sreallymatter" { - data := TemplateData { - Title: "Do Blue Lives Really Matter?", - Description: "Still No", - URL: "https://doblue.live/sreallymatter", - } - - mainTemplate.Execute(w, data) - } else { - scheme := "http" - if req.TLS != nil { - scheme += "s" - } - - http.Redirect(w, req, scheme + "://" + req.Host + "/smatter", http.StatusPermanentRedirect) - - return - } - }) - - log.Fatal(http.ListenAndServe(":8081", nil)) + mainTemplate, err := template.ParseFiles("./templates/main.tmpl") + if err != nil { + log.Fatal("Error: Unable to parse the template file.") + } + + http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + if req.URL.Path == "/smatter" || req.URL.Path == "/smatter/offline.html" { + data := TemplateData { + Title: "Do Blue Lives Matter?", + Description: "No", + URL: "https://doblue.live/smatter", + IncludeServiceWorker: req.URL.Path == "/smatter", + } + + mainTemplate.Execute(w, data) + } else if req.URL.Path == "/sreallymatter" || req.URL.Path == "/sreallymatter/offline.html" { + data := TemplateData { + Title: "Do Blue Lives Really Matter?", + Description: "Still No", + URL: "https://doblue.live/sreallymatter", + IncludeServiceWorker: req.URL.Path == "/sreallymatter", + } + + mainTemplate.Execute(w, data) + } else if req.URL.Path == "/robots.txt" { + http.ServeFile(w, req, "./static/robots.txt") + } else if req.URL.Path == "/service-worker.js" { + http.ServeFile(w, req, "./static/service-worker.js") + } else { + scheme := "http" + if req.TLS != nil { + scheme += "s" + } + + http.Redirect(w, req, scheme + "://" + req.Host + "/smatter", http.StatusPermanentRedirect) + } + }) + + log.Fatal(http.ListenAndServe(":8081", nil)) } \ No newline at end of file diff --git a/static/robots.txt b/static/robots.txt new file mode 100644 index 0000000..9e60f97 --- /dev/null +++ b/static/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: diff --git a/static/service-worker.js b/static/service-worker.js new file mode 100644 index 0000000..75b1564 --- /dev/null +++ b/static/service-worker.js @@ -0,0 +1,37 @@ + +const CACHE_NAME = 'offline'; +const SMATTER_OFFLINE_URL = '/smatter/offline.html'; +const SREALLYMATTER_OFFLINE_URL = '/sreallymatter/offline.html'; + +self.addEventListener('install', (event) => { + event.waitUntil((async () => { + const cache = await caches.open(CACHE_NAME); + await cache.add(new Request(SMATTER_OFFLINE_URL, {cache: 'reload'})); + await cache.add(new Request(SREALLYMATTER_OFFLINE_URL, {cache: 'reload'})); + })()); +}); + +self.addEventListener('activate', (event) => { + event.waitUntil((async () => { + if ('navigationPreload' in self.registration) { + await self.registration.navigationPreload.enable(); + } + })()); + + self.clients.claim(); +}); + +self.addEventListener('fetch', (event) => { + if (event.request.mode === 'navigate') { + event.respondWith((async () => { + try { + const preloadResponse = await event.preloadResponse; + return (preloadResponse) ? preloadResponse : await fetch(event.request); + } catch (error) { + console.log(event); + const cache = await caches.open(CACHE_NAME); + return await cache.match((event.request.url.endsWith('/smatter')) ? SMATTER_OFFLINE_URL : SREALLYMATTER_OFFLINE_URL); + } + })()); + } +}); \ No newline at end of file diff --git a/templates/main.tmpl b/templates/main.tmpl index fd622e3..4cf80e9 100644 --- a/templates/main.tmpl +++ b/templates/main.tmpl @@ -31,14 +31,14 @@ h1 { font-size: 5em; font-weight: bold; - margin: 0; + margin: 0 16px; text-align: center; } p { font-size: 0.875em; line-height: 1.5em; - margin: 16px 0 0 0; + margin: 16px 16px 0; text-align: center; max-width: 500px; } @@ -49,5 +49,13 @@

{{ .Description }}

Blue lives do not exist. Police officers willingly chose their profession. When they finish their shift, they clock out, go home and take their badge off. It's a job, not a race. People of color cannot go home and stop being their race; they did not willingly choose to be their race in this world. Blue lives matter is a mockery of an actual, valuable movement.

+ {{ if .IncludeServiceWorker }} + + + {{ end }}