diff --git a/main.go b/main.go index ce0958d..040359f 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( ) const PORT int = 8080 +var LAST_MODIFIED = time.Now() var mime_types = map[string]string{ "css": "text/css; charset=utf-8", @@ -73,6 +74,10 @@ func handle_request(writer http.ResponseWriter, req *http.Request) { // } // } + writer.Header().Set("Server", "arimelody.me") + writer.Header().Set("Cache-Control", "max-age=86400") + writer.Header().Set("Last-Modified", LAST_MODIFIED.Format(http.TimeFormat)) + code := func(writer http.ResponseWriter, req *http.Request) int { // var root *template.Template // if hx_request { @@ -84,19 +89,19 @@ func handle_request(writer http.ResponseWriter, req *http.Request) { var root = template.Must(base_template.Clone()) if req.URL.Path == "/" { - return index_handler(writer, root) + return handle_index(writer, req, root) } if uri == "/music" || uri == "/music/" { - return music_directory_handler(writer, root) + return handle_music(writer, req, root) } if strings.HasPrefix(uri, "/music/") { - return music_gateway_handler(writer, req, root) + return handle_music_gateway(writer, req, root) } if strings.HasPrefix(uri, "/admin") { - return admin_handler(writer, req, root) + return handle_admin(writer, req, root) } if strings.HasPrefix(uri, "/api") { @@ -109,7 +114,12 @@ func handle_request(writer http.ResponseWriter, req *http.Request) { log_request(req, code, start_time) } -func index_handler(writer http.ResponseWriter, root *template.Template) int { +func handle_index(writer http.ResponseWriter, req *http.Request, root *template.Template) int { + if !was_modified(req, LAST_MODIFIED) { + writer.WriteHeader(304) + return 304 + } + index_template := template.Must(root.ParseFiles("views/index.html")) err := index_template.Execute(writer, nil) if err != nil { @@ -119,7 +129,12 @@ func index_handler(writer http.ResponseWriter, root *template.Template) int { return 200 } -func music_directory_handler(writer http.ResponseWriter, root *template.Template) int { +func handle_music(writer http.ResponseWriter, req *http.Request, root *template.Template) int { + if !was_modified(req, LAST_MODIFIED) { + writer.WriteHeader(304) + return 304 + } + music_template := template.Must(root.ParseFiles("views/music.html")) music := music.QueryAllMusic() err := music_template.Execute(writer, music) @@ -130,7 +145,12 @@ func music_directory_handler(writer http.ResponseWriter, root *template.Template return 200 } -func music_gateway_handler(writer http.ResponseWriter, req *http.Request, root *template.Template) int { +func handle_music_gateway(writer http.ResponseWriter, req *http.Request, root *template.Template) int { + if !was_modified(req, LAST_MODIFIED) { + writer.WriteHeader(304) + return 304 + } + id := req.URL.Path[len("/music/"):] // http.Redirect(writer, req, "https://mellodoot.com/music/"+title, 302) // return @@ -147,7 +167,12 @@ func music_gateway_handler(writer http.ResponseWriter, req *http.Request, root * return 200 } -func admin_handler(writer http.ResponseWriter, req *http.Request, root *template.Template) int { +func handle_admin(writer http.ResponseWriter, req *http.Request, root *template.Template) int { + if !was_modified(req, LAST_MODIFIED) { + writer.WriteHeader(304) + return 304 + } + admin_template := template.Must(root.ParseFiles("views/admin.html")) err := admin_template.Execute(writer, nil) if err != nil { @@ -166,23 +191,13 @@ func static_handler(writer http.ResponseWriter, req *http.Request, root *templat return handle_not_found(writer, req, root) } - if len(req.Header["If-Modified-Since"]) > 0 && req.Header["If-Modified-Since"][0] != "" { - if_modified_since_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0]) - if err != nil { - http.Error(writer, "400 bad request", http.StatusBadRequest) - return 400 - } - if req.Header["If-Modified-Since"][0] == info.ModTime().Format(http.TimeFormat) || if_modified_since_time.After(info.ModTime()) { - writer.WriteHeader(304) // not modified - return 304 - } + if !was_modified(req, info.ModTime()) { + writer.WriteHeader(304) + return 304 } - // set other nice headers - writer.Header().Set("Cache-Control", "max-age=86400") + // set Last-Modified to file modification date writer.Header().Set("Last-Modified", info.ModTime().Format(http.TimeFormat)) - // Last-Modified: , :: GMT - // Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT // read the file body, err := os.ReadFile(filename) @@ -218,6 +233,20 @@ func handle_not_found(writer http.ResponseWriter, req *http.Request, root *templ return 404 } +func was_modified(req *http.Request, last_modified time.Time) bool { + if len(req.Header["If-Modified-Since"]) == 0 || len(req.Header["If-Modified-Since"][0]) == 0 { + return true + } + request_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0]) + if err != nil { + return true + } + if request_time.Before(last_modified) { + return true + } + return false +} + func parse_markdown(md []byte) []byte { extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock p := parser.NewWithExtensions(extensions) diff --git a/public/script/swap.js b/public/script/swap.js index 708764e..ccc834d 100644 --- a/public/script/swap.js +++ b/public/script/swap.js @@ -1,21 +1,31 @@ const swap_event = new Event("swap"); let caches = {}; -window.lmao = caches; -async function check_cache(url) { - if (caches[url]) { - return caches[url]; - } else { - const res = await fetch(url); +async function cached_fetch(url) { + let cached = caches[url]; + console.log("cache: " + cached); - if (res.status !== 200) return; - if (!res.headers.get("content-type").startsWith("text/html")) return; + const res = cached === undefined ? await fetch(url) : await fetch(url, { + headers: { + "If-Modified-Since": cached.last_modified + } + }); - const text = await res.text(); - caches[url] = text; - return text; + if (res.status === 304 && cached !== undefined) { + return cached.content; } + if (res.status !== 200) return; + if (!res.headers.get("content-type").startsWith("text/html")) return; + + const text = await res.text(); + if (res.headers.get("last-modified")) { + caches[url] = { + content: text, + last_modified: res.headers.get("last-modified") + } + } + return text; } async function swap(url, stateful) { @@ -29,7 +39,7 @@ async function swap(url, stateful) { if (stateful && window.location.href.endsWith(url)) return; - const text = await check_cache(url); + const text = await cached_fetch(url); const content = new DOMParser().parseFromString(text, "text/html"); const stylesheets = [...content.querySelectorAll("link[rel='stylesheet']")]; diff --git a/views/index.html b/views/index.html index 73bd61b..0572f8e 100644 --- a/views/index.html +++ b/views/index.html @@ -149,39 +149,39 @@
- ari melody web button + ari melody web button - zaire web button + zaire web button - vimae web button + vimae web button - zvava web button + zvava web button
- powered by debian - girls 4 notepad - this website is GAY - graphic design is my passion - GPLv3 free software - half-life - dis site is hentai FREE - sprunk - go straight to hell - virus alert! click here - wii - www - get mandatory internet explorer - HTML - learn it today! + powered by debian + girls 4 notepad + this website is GAY + graphic design is my passion + GPLv3 free software + half-life + dis site is hentai FREE + sprunk + go straight to hell + virus alert! click here + wii + www + get mandatory internet explorer + HTML - learn it today! - high on SMOKE + high on SMOKE - epic blazed + epic blazed
diff --git a/views/music.html b/views/music.html index 6430aed..cf60e5e 100644 --- a/views/music.html +++ b/views/music.html @@ -28,7 +28,7 @@ {{range $Album := .}}