home to your local SPACEGIRL 💫 arimelody.space
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

caching improvements

Signed-off-by: ari melody <ari@arimelody.me>

+94 -55
+51 -22
main.go
··· 19 19 ) 20 20 21 21 const PORT int = 8080 22 + var LAST_MODIFIED = time.Now() 22 23 23 24 var mime_types = map[string]string{ 24 25 "css": "text/css; charset=utf-8", ··· 73 74 // } 74 75 // } 75 76 77 + writer.Header().Set("Server", "arimelody.me") 78 + writer.Header().Set("Cache-Control", "max-age=86400") 79 + writer.Header().Set("Last-Modified", LAST_MODIFIED.Format(http.TimeFormat)) 80 + 76 81 code := func(writer http.ResponseWriter, req *http.Request) int { 77 82 // var root *template.Template 78 83 // if hx_request { ··· 84 89 var root = template.Must(base_template.Clone()) 85 90 86 91 if req.URL.Path == "/" { 87 - return index_handler(writer, root) 92 + return handle_index(writer, req, root) 88 93 } 89 94 90 95 if uri == "/music" || uri == "/music/" { 91 - return music_directory_handler(writer, root) 96 + return handle_music(writer, req, root) 92 97 } 93 98 94 99 if strings.HasPrefix(uri, "/music/") { 95 - return music_gateway_handler(writer, req, root) 100 + return handle_music_gateway(writer, req, root) 96 101 } 97 102 98 103 if strings.HasPrefix(uri, "/admin") { 99 - return admin_handler(writer, req, root) 104 + return handle_admin(writer, req, root) 100 105 } 101 106 102 107 if strings.HasPrefix(uri, "/api") { ··· 109 114 log_request(req, code, start_time) 110 115 } 111 116 112 - func index_handler(writer http.ResponseWriter, root *template.Template) int { 117 + func handle_index(writer http.ResponseWriter, req *http.Request, root *template.Template) int { 118 + if !was_modified(req, LAST_MODIFIED) { 119 + writer.WriteHeader(304) 120 + return 304 121 + } 122 + 113 123 index_template := template.Must(root.ParseFiles("views/index.html")) 114 124 err := index_template.Execute(writer, nil) 115 125 if err != nil { ··· 119 129 return 200 120 130 } 121 131 122 - func music_directory_handler(writer http.ResponseWriter, root *template.Template) int { 132 + func handle_music(writer http.ResponseWriter, req *http.Request, root *template.Template) int { 133 + if !was_modified(req, LAST_MODIFIED) { 134 + writer.WriteHeader(304) 135 + return 304 136 + } 137 + 123 138 music_template := template.Must(root.ParseFiles("views/music.html")) 124 139 music := music.QueryAllMusic() 125 140 err := music_template.Execute(writer, music) ··· 130 145 return 200 131 146 } 132 147 133 - func music_gateway_handler(writer http.ResponseWriter, req *http.Request, root *template.Template) int { 148 + func handle_music_gateway(writer http.ResponseWriter, req *http.Request, root *template.Template) int { 149 + if !was_modified(req, LAST_MODIFIED) { 150 + writer.WriteHeader(304) 151 + return 304 152 + } 153 + 134 154 id := req.URL.Path[len("/music/"):] 135 155 // http.Redirect(writer, req, "https://mellodoot.com/music/"+title, 302) 136 156 // return ··· 147 167 return 200 148 168 } 149 169 150 - func admin_handler(writer http.ResponseWriter, req *http.Request, root *template.Template) int { 170 + func handle_admin(writer http.ResponseWriter, req *http.Request, root *template.Template) int { 171 + if !was_modified(req, LAST_MODIFIED) { 172 + writer.WriteHeader(304) 173 + return 304 174 + } 175 + 151 176 admin_template := template.Must(root.ParseFiles("views/admin.html")) 152 177 err := admin_template.Execute(writer, nil) 153 178 if err != nil { ··· 166 191 return handle_not_found(writer, req, root) 167 192 } 168 193 169 - if len(req.Header["If-Modified-Since"]) > 0 && req.Header["If-Modified-Since"][0] != "" { 170 - if_modified_since_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0]) 171 - if err != nil { 172 - http.Error(writer, "400 bad request", http.StatusBadRequest) 173 - return 400 174 - } 175 - if req.Header["If-Modified-Since"][0] == info.ModTime().Format(http.TimeFormat) || if_modified_since_time.After(info.ModTime()) { 176 - writer.WriteHeader(304) // not modified 177 - return 304 178 - } 194 + if !was_modified(req, info.ModTime()) { 195 + writer.WriteHeader(304) 196 + return 304 179 197 } 180 198 181 - // set other nice headers 182 - writer.Header().Set("Cache-Control", "max-age=86400") 199 + // set Last-Modified to file modification date 183 200 writer.Header().Set("Last-Modified", info.ModTime().Format(http.TimeFormat)) 184 - // Last-Modified: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT 185 - // Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT 186 201 187 202 // read the file 188 203 body, err := os.ReadFile(filename) ··· 216 231 return 500 217 232 } 218 233 return 404 234 + } 235 + 236 + func was_modified(req *http.Request, last_modified time.Time) bool { 237 + if len(req.Header["If-Modified-Since"]) == 0 || len(req.Header["If-Modified-Since"][0]) == 0 { 238 + return true 239 + } 240 + request_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0]) 241 + if err != nil { 242 + return true 243 + } 244 + if request_time.Before(last_modified) { 245 + return true 246 + } 247 + return false 219 248 } 220 249 221 250 func parse_markdown(md []byte) []byte {
+22 -12
public/script/swap.js
··· 1 1 const swap_event = new Event("swap"); 2 2 3 3 let caches = {}; 4 - window.lmao = caches; 5 4 6 - async function check_cache(url) { 7 - if (caches[url]) { 8 - return caches[url]; 9 - } else { 10 - const res = await fetch(url); 5 + async function cached_fetch(url) { 6 + let cached = caches[url]; 7 + console.log("cache: " + cached); 11 8 12 - if (res.status !== 200) return; 13 - if (!res.headers.get("content-type").startsWith("text/html")) return; 9 + const res = cached === undefined ? await fetch(url) : await fetch(url, { 10 + headers: { 11 + "If-Modified-Since": cached.last_modified 12 + } 13 + }); 14 14 15 - const text = await res.text(); 16 - caches[url] = text; 17 - return text; 15 + if (res.status === 304 && cached !== undefined) { 16 + return cached.content; 17 + } 18 + if (res.status !== 200) return; 19 + if (!res.headers.get("content-type").startsWith("text/html")) return; 20 + 21 + const text = await res.text(); 22 + if (res.headers.get("last-modified")) { 23 + caches[url] = { 24 + content: text, 25 + last_modified: res.headers.get("last-modified") 26 + } 18 27 } 28 + return text; 19 29 } 20 30 21 31 async function swap(url, stateful) { ··· 29 39 30 40 if (stateful && window.location.href.endsWith(url)) return; 31 41 32 - const text = await check_cache(url); 42 + const text = await cached_fetch(url); 33 43 const content = new DOMParser().parseFromString(text, "text/html"); 34 44 35 45 const stylesheets = [...content.querySelectorAll("link[rel='stylesheet']")];
+20 -20
views/index.html
··· 149 149 150 150 <div id="web-buttons"> 151 151 <a href="https://arimelody.me"> 152 - <img src="/img/buttons/ari melody.gif" alt="ari melody web button"> 152 + <img src="/img/buttons/ari melody.gif" alt="ari melody web button" loading="lazy"> 153 153 </a> 154 154 <a href="https://supitszaire.com" target="_blank"> 155 - <img src="/img/buttons/zaire.gif" alt="zaire web button"> 155 + <img src="/img/buttons/zaire.gif" alt="zaire web button" loading="lazy"> 156 156 </a> 157 157 <a href="https://mae.wtf" target="_blank"> 158 - <img src="/img/buttons/mae.png" alt="vimae web button"> 158 + <img src="/img/buttons/mae.png" alt="vimae web button" loading="lazy"> 159 159 </a> 160 160 <a href="https://zvava.org" target="_blank"> 161 - <img src="/img/buttons/zvava.png" alt="zvava web button"> 161 + <img src="/img/buttons/zvava.png" alt="zvava web button" loading="lazy"> 162 162 </a> 163 163 164 164 <hr> 165 165 166 - <img src="/img/buttons/misc/debian.gif" alt="powered by debian"> 167 - <img src="/img/buttons/misc/girls4notepad.gif" alt="girls 4 notepad"> 168 - <img src="/img/buttons/misc/gaywebring.gif" alt="this website is GAY"> 169 - <img src="/img/buttons/misc/graphicdesign.gif" alt="graphic design is my passion"> 170 - <img src="/img/buttons/misc/gplv3.gif" alt="GPLv3 free software"> 171 - <img src="/img/buttons/misc/hl.gif" alt="half-life"> 172 - <img src="/img/buttons/misc/h-free-anim.gif" alt="dis site is hentai FREE"> 173 - <img src="/img/buttons/misc/sprunk.gif" alt="sprunk"> 174 - <img src="/img/buttons/misc/tohell.gif" alt="go straight to hell"> 175 - <img src="/img/buttons/misc/virusalert.gif" alt="virus alert! click here" onclick="alert('meow :3')"> 176 - <img src="/img/buttons/misc/wii.gif" alt="wii"> 177 - <img src="/img/buttons/misc/www2.gif" alt="www"> 178 - <img src="/img/buttons/misc/iemandatory.gif" alt="get mandatory internet explorer"> 179 - <img src="/img/buttons/misc/learn_html.gif" alt="HTML - learn it today!"> 166 + <img src="/img/buttons/misc/debian.gif" alt="powered by debian" loading="lazy"> 167 + <img src="/img/buttons/misc/girls4notepad.gif" alt="girls 4 notepad" loading="lazy"> 168 + <img src="/img/buttons/misc/gaywebring.gif" alt="this website is GAY" loading="lazy"> 169 + <img src="/img/buttons/misc/graphicdesign.gif" alt="graphic design is my passion" loading="lazy"> 170 + <img src="/img/buttons/misc/gplv3.gif" alt="GPLv3 free software" loading="lazy"> 171 + <img src="/img/buttons/misc/hl.gif" alt="half-life" loading="lazy"> 172 + <img src="/img/buttons/misc/h-free-anim.gif" alt="dis site is hentai FREE" loading="lazy"> 173 + <img src="/img/buttons/misc/sprunk.gif" alt="sprunk" loading="lazy"> 174 + <img src="/img/buttons/misc/tohell.gif" alt="go straight to hell" loading="lazy"> 175 + <img src="/img/buttons/misc/virusalert.gif" alt="virus alert! click here" onclick="alert('meow :3')" loading="lazy"> 176 + <img src="/img/buttons/misc/wii.gif" alt="wii" loading="lazy"> 177 + <img src="/img/buttons/misc/www2.gif" alt="www" loading="lazy"> 178 + <img src="/img/buttons/misc/iemandatory.gif" alt="get mandatory internet explorer" loading="lazy"> 179 + <img src="/img/buttons/misc/learn_html.gif" alt="HTML - learn it today!" loading="lazy"> 180 180 <a href="https://smokepowered.com" target="_blank"> 181 - <img src="/img/buttons/misc/smokepowered.gif" alt="high on SMOKE"> 181 + <img src="/img/buttons/misc/smokepowered.gif" alt="high on SMOKE" loading="lazy"> 182 182 </a> 183 183 <a href="https://epicblazed.com" target="_blank"> 184 - <img src="/img/buttons/misc/epicblazed.png" alt="epic blazed"> 184 + <img src="/img/buttons/misc/epicblazed.png" alt="epic blazed" loading="lazy"> 185 185 </a> 186 186 </div> 187 187 </main>
+1 -1
views/music.html
··· 28 28 {{range $Album := .}} 29 29 <div class="music" id="{{$Album.Id}}" swap-url="/music/{{$Album.Id}}"> 30 30 <div class="music-artwork"> 31 - <img src="{{$Album.ResolveArtwork}}" alt="{{$Album.Title}} artwork" width="128"> 31 + <img src="{{$Album.ResolveArtwork}}" alt="{{$Album.Title}} artwork" width="128" loading="lazy"> 32 32 </div> 33 33 <div class="music-details"> 34 34 <a href="/music/{{$Album.Id}}"><h1 class="music-title">{{$Album.Title}}</h1></a>