Browse Source

Added Service Worker, robots.txt, and small fix for mobile.

main
Nichole Mattera 2 years ago
parent
commit
d857382cfa
  1. 87
      main.go
  2. 2
      static/robots.txt
  3. 37
      static/service-worker.js
  4. 12
      templates/main.tmpl

87
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))
}

2
static/robots.txt

@ -0,0 +1,2 @@
User-agent: *
Disallow:

37
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);
}
})());
}
});

12
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 @@
<h1>{{ .Description }}</h1>
<p>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.</p>
</div>
{{ if .IncludeServiceWorker }}
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js');
}
</script>
{{ end }}
</body>
</html>

Loading…
Cancel
Save