| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | var 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 | } |
| 18 | |
| 19 | // serveEmbeddedAsset serves assets that do not depend on the backend |
| 20 | // snapshot. It returns false for unknown paths or unsupported methods. |
| 21 | func serveEmbeddedAsset(w http.ResponseWriter, r *http.Request, path string) bool { |
| 22 | if r.Method != http.MethodGet && r.Method != http.MethodHead { |
| 23 | return false |
| 24 | } |
| 25 | if strings.HasPrefix(path, "/scripts/" ) { |
| 26 | source, ok := scripts[strings.TrimPrefix(path, "/scripts/" )] |
| 27 | if !ok { |
| 28 | http.NotFound(w, r) |
| 29 | return true |
| 30 | } |
| 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 true |
| 41 | } |
| 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 false |
| 62 | } |
| 63 | |
| 64 | func 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 true |
| 73 | } |