summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-06-28 17:49:14 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-06-28 17:49:14 +0900
commita2abfc596965111c28ea43d9ab986732692e022f (patch)
tree5e60fe2d301a9e9e98af33ed953037db852e7da9
parent5ba68fae053ccabc3161ee4f80963ac0c47ddaf2 (diff)
downloadphylactery-a2abfc596965111c28ea43d9ab986732692e022f.tar.gz
Write response as soon as data is ready
Previously the entire archive had to be read before the template
is executed, thus large archives takes forever to render
from slow backing FS.
-rw-r--r--main.go29
-rw-r--r--templates/archive-foot.html2
-rw-r--r--templates/archive-head.html (renamed from templates/archive.html)5
-rw-r--r--templates/archive-page.html1
4 files changed, 18 insertions, 19 deletions
diff --git a/main.go b/main.go
index b364d20..dfc01a5 100644
--- a/main.go
+++ b/main.go
@@ -48,7 +48,6 @@ type Archive struct {
 	Title   string
 	Prev    string
 	Next    string
-	Entries []Page
 }
 
 // Type Directory represents a library directory in file system.
@@ -147,7 +146,17 @@ func main() {
 		// Render archive page
 		entries, _ := os.ReadDir(path.Join(p, ".."))
 		index := find(entries, stat.Name())
-		var pages []Page
+		prev := ""
+		if index > 0 {
+			prev = entries[index-1].Name()
+		}
+		next := ""
+		if index < len(entries)-1 {
+			next = entries[index+1].Name()
+		}
+		t.ExecuteTemplate(w, "archive-head.html",
+			Archive{ stat.Name(), prev, next })
+
 		for i, f := range cbz.File {
 			image, _ := cbz.File[i].Open()
 			defer image.Close()
@@ -155,21 +164,11 @@ func main() {
 			n, _ := image.Read(buf)
 			mime := http.DetectContentType(buf[:n])
 			if strings.HasPrefix(mime, "image/") {
-				pages = append(pages, Page{i, f.Name})
+				t.ExecuteTemplate(w, "archive-page.html",
+					Page{i, f.Name})
 			}
 		}
-
-		prev := ""
-		if index > 0 {
-			prev = entries[index-1].Name()
-		}
-		next := ""
-		if index < len(entries)-1 {
-			next = entries[index+1].Name()
-		}
-		t.ExecuteTemplate(w, "archive.html", Archive{
-			stat.Name(), prev, next, pages,
-		})
+		t.ExecuteTemplate(w, "archive-foot.html", nil)
 	})
 
 	addr := os.Getenv("PHYLACTERY_ADDRESS")
diff --git a/templates/archive-foot.html b/templates/archive-foot.html
new file mode 100644
index 0000000..1e370b9
--- /dev/null
+++ b/templates/archive-foot.html
@@ -0,0 +1,2 @@
+  </main>
+{{template "base-foot.html" -}}
diff --git a/templates/archive.html b/templates/archive-head.html
index 619a5bc..ed643e3 100644
--- a/templates/archive.html
+++ b/templates/archive-head.html
@@ -9,7 +9,4 @@
     <a id=next href="{{.Next | escape}}">NEXT</a>{{else}}
     <a id=next></a>{{end}}
   </nav>
-  <main>{{range .Entries}}
-    <img src="?entry={{.Index}}" alt="{{.Name}}">{{end}}
-  </main>
-{{template "base-foot.html" -}}
+  <main>
diff --git a/templates/archive-page.html b/templates/archive-page.html
new file mode 100644
index 0000000..8011ece
--- /dev/null
+++ b/templates/archive-page.html
@@ -0,0 +1 @@
+    <img src="?entry={{.Index}}" alt="{{.Name}}">