summary refs log tree commit diff homepage
path: root/main.go
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-06-28 15:27:06 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-06-28 15:27:06 +0900
commit5ba68fae053ccabc3161ee4f80963ac0c47ddaf2 (patch)
tree83eb374a71de46ccb8ec17b803976b9a5e11bb2e /main.go
parente405d3f244d074f00658e020795a90ec3f69add7 (diff)
downloadphylactery-fc309c292088a6cbf15de07ac3bacddc75434e2e.tar.gz
Polish up for a release 0.1.0
Diffstat (limited to 'main.go')
-rw-r--r--main.go17
1 files changed, 16 insertions, 1 deletions
diff --git a/main.go b/main.go
index 42db6d2..b364d20 100644
--- a/main.go
+++ b/main.go
@@ -37,11 +37,13 @@ var static embed.FS
 //go:embed templates/*.html
 var templates embed.FS
 
+// Type Page represents a comic page.
 type Page struct {
 	Index int
 	Name  string
 }
 
+// Type Archive represents a comic book zip archive.
 type Archive struct {
 	Title   string
 	Prev    string
@@ -49,15 +51,19 @@ type Archive struct {
 	Entries []Page
 }
 
+// Type Directory represents a library directory in file system.
 type Directory struct {
 	Title   string
 	Entries []string
 }
 
+// Function escape ensures that question marks in path
+// are not recognized as URL parameters.
 func escape(name string) template.URL {
 	return template.URL(strings.Replace(name, "?", "%3f", -1))
 }
 
+// Function find searches for the directory entry of given name.
 func find(entries []os.DirEntry, name string) int {
 	for i, entry := range entries {
 		if entry.Name() == name {
@@ -67,6 +73,8 @@ func find(entries []os.DirEntry, name string) int {
 	return -1
 }
 
+// Function main starts Phylactery serving comics from PHYLACTERY_LIBRARY
+// and listening on PHYLACTERY_ADDRESS.
 func main() {
 	http.Handle("/static/", http.FileServer(http.FS(static)))
 	t, err := template.New("").Funcs(template.FuncMap{
@@ -85,12 +93,15 @@ func main() {
 		}
 
 		if stat.IsDir() {
+			// Redirect URL without a trailing slash
+			// pointing to a non-directory page
 			if !strings.HasSuffix(r.URL.Path, "/") {
 				http.Redirect(w, r, r.URL.Path+"/",
 					http.StatusMovedPermanently)
 				return
 			}
 
+			// Render directory page
 			entries, _ := os.ReadDir(p)
 			var names []string
 			for _, entry := range entries {
@@ -104,12 +115,14 @@ func main() {
 			t.ExecuteTemplate(w, "directory.html", dir)
 			return
 		} else if strings.HasSuffix(r.URL.Path, "/") {
+			// Redirect URL with a trailing slash
+			// pointing to a non-directory page
 			http.Redirect(w, r, r.URL.Path[:len(r.URL.Path)-1],
 				http.StatusMovedPermanently)
 			return
 		}
 
-		// TODO: LRU caching
+		// Check if file is a valid ZIP archive
 		cbz, err := zip.OpenReader(p)
 		if err != nil {
 			http.Error(w, "invalid cbz", http.StatusNotAcceptable)
@@ -117,6 +130,7 @@ func main() {
 		}
 		defer cbz.Close()
 
+		// Respond with an image inside the CBZ
 		r.ParseForm()
 		if entry, isImage := r.Form["entry"]; isImage {
 			i, err := strconv.Atoi(entry[0])
@@ -130,6 +144,7 @@ func main() {
 			return
 		}
 
+		// Render archive page
 		entries, _ := os.ReadDir(path.Join(p, ".."))
 		index := find(entries, stat.Name())
 		var pages []Page