clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/standalone-relay
tinyrelay / tinyclient / assets.go
assets.go 2.3K
1package tinyclient23import (4 "io"5 "net/http"6 "strings"7)89var immutablePNGAssets = map[string][]byte{10 "/icon-192.png": icon192PNG,11 "/icon-512.png": icon512PNG,12 "/icon-maskable-512.png": iconMaskablePNG,13 "/apple-touch-icon.png": appleTouchIconPNG,14 "/badge-96.png": badgePNG,15 "/screenshot-narrow.png": screenshotNarrowPNG,16 "/screenshot-wide.png": screenshotWidePNG,17}1819// serveEmbeddedAsset serves assets that do not depend on the backend20// snapshot. It returns false for unknown paths or unsupported methods.21func serveEmbeddedAsset(w http.ResponseWriter, r *http.Request, path string) bool {22 if r.Method != http.MethodGet && r.Method != http.MethodHead {23 return false24 }25 if strings.HasPrefix(path, "/scripts/") {26 source, ok := scripts[strings.TrimPrefix(path, "/scripts/")]27 if !ok {28 http.NotFound(w, r)29 return true30 }31 w.Header().Set("content-type", "application/javascript; charset=utf-8")32 if r.URL.Query().Get("v") == scriptsVersion {33 w.Header().Set("cache-control", "public, max-age=31536000, immutable")34 } else {35 w.Header().Set("cache-control", "no-cache")36 }37 if r.Method == http.MethodGet {38 _, _ = io.WriteString(w, source)39 }40 return true41 }42 if path == "/signer.js" {43 return writeEmbeddedAsset(w, r, "application/javascript; charset=utf-8", signerJS, "")44 }45 if path == "/fixi.js" {46 return writeEmbeddedAsset(w, r, "application/javascript; charset=utf-8", fixiJS, "")47 }48 if path == "/sw.js" {49 w.Header().Set("service-worker-allowed", "/")50 return writeEmbeddedAsset(w, r, "application/javascript; charset=utf-8", serviceWorkerJS, "")51 }52 if path == "/icon.svg" {53 return writeEmbeddedAsset(w, r, "image/svg+xml; charset=utf-8", iconSVG, "")54 }55 if path == "/icon-mono.svg" {56 return writeEmbeddedAsset(w, r, "image/svg+xml; charset=utf-8", iconMonoSVG, "")57 }58 if png, ok := immutablePNGAssets[path]; ok {59 return writeEmbeddedAsset(w, r, "image/png", png, "public, max-age=86400")60 }61 return false62}6364func writeEmbeddedAsset(w http.ResponseWriter, r *http.Request, contentType string, body []byte, cacheControl string) bool {65 w.Header().Set("content-type", contentType)66 if cacheControl != "" {67 w.Header().Set("cache-control", cacheControl)68 }69 if r.Method == http.MethodGet {70 _, _ = w.Write(body)71 }72 return true73}