From 22d36d5a895729c3b2944ac611c77264393245f9 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Thu, 18 Mar 2021 23:16:26 +0100 Subject: [PATCH] We are now a webserver with a webhook :P --- cmd/buchdesmonats/main.go | 73 +++++++++++++------ public/comic.html | 27 ------- public/index.html | 14 ---- {public => static}/book.css | 0 {public => static}/comic.css | 0 {public => static}/main.js | 0 .../vendors/imagesloaded-3.1.4.pkgd.min.js | 0 .../vendors/jquery-2.1.0.min.js | 0 .../vendors/masonry-3.1.5.pkgd.min.js | 0 public/book.html => templates/BOOK.mkd.html | 14 ++-- templates/COMIC.mkd.html | 30 ++++++++ 11 files changed, 90 insertions(+), 68 deletions(-) delete mode 100644 public/comic.html delete mode 100644 public/index.html rename {public => static}/book.css (100%) rename {public => static}/comic.css (100%) rename {public => static}/main.js (100%) rename {public => static}/vendors/imagesloaded-3.1.4.pkgd.min.js (100%) rename {public => static}/vendors/jquery-2.1.0.min.js (100%) rename {public => static}/vendors/masonry-3.1.5.pkgd.min.js (100%) rename public/book.html => templates/BOOK.mkd.html (63%) create mode 100644 templates/COMIC.mkd.html diff --git a/cmd/buchdesmonats/main.go b/cmd/buchdesmonats/main.go index bc5f263..f3c0eb9 100644 --- a/cmd/buchdesmonats/main.go +++ b/cmd/buchdesmonats/main.go @@ -1,21 +1,22 @@ package main import ( - "flag" "fmt" + "html/template" "io" "io/ioutil" + "log" "net/http" "os" "regexp" ) type Item struct { - ISBN string + ISBN string Filename string } -func (i Item) imageURL() string { +func (i Item) ImageURL() string { return "https://medien.ubitweb.de/bildzentrale_original/" + i.ISBN[0:3] + "/" + i.ISBN[3:6] + "/" + @@ -29,7 +30,7 @@ func (i Item) targetFilename() string { } func (i Item) downloadCover() error { - resp, err := http.Get(i.imageURL()) + resp, err := http.Get(i.ImageURL()) if err != nil { return err @@ -49,9 +50,6 @@ func (i Item) downloadCover() error { return err } -var filename string -var force bool - func getItems(filename string) []Item { var items []Item // Get all book URLS @@ -79,27 +77,60 @@ func getItems(filename string) []Item { return items } -func main() { +func getHTML(filename string, w http.ResponseWriter) { // Get all items from the git repo items := getItems(filename) - for _, item := range items { - _, err := os.Stat(item.targetFilename()) - if os.IsNotExist(err) || force { - fmt.Printf("Downloading %v ...\n", item.imageURL()) + // Generate the restulting HTML + t, err := template.ParseFiles("templates/" + filename + ".html") + if err != nil { + panic(err) + } + + err = t.Execute(w, items) + if err != nil { + panic(err) + } +} + +func main() { + // All static files (CSS, JS) + fileServer := http.FileServer(http.Dir("./static")) + http.Handle("/static/", http.StripPrefix("/static", fileServer)) + + // Images + imageServer := http.FileServer(http.Dir("./covers/")) + http.Handle("/covers/", http.StripPrefix("/covers", imageServer)) + + // Update "Hook" /update?filename=COMIC.mkd + http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) { + filename := r.URL.Query().Get("filename") + + // Get all items from the git repo + items := getItems(filename) + + for _, item := range items { + fmt.Printf("Downloading %v ...\n", item.ImageURL()) err := item.downloadCover() if err != nil { - fmt.Printf("ERROR: File %s not found\n", item.imageURL()) + fmt.Printf("ERROR: File %s not found\n", item.ImageURL()) } } + }) + + // Spawn webserver /comic + http.HandleFunc("/book", func(w http.ResponseWriter, r *http.Request) { + log.Print("/book") + getHTML("BOOK.mkd", w) + }) + http.HandleFunc("/comic", func(w http.ResponseWriter, r *http.Request) { + log.Print("/comic") + getHTML("COMIC.mkd", w) + }) + + err := http.ListenAndServe(":1337", nil) + if err != nil { + panic(err) } - - // TODO: use template to generate the restulting HTML -} - -func init() { - flag.StringVar(&filename, "filename", "COMIC.mkd", "The filename to use") - flag.BoolVar(&force, "force", false, "Ignore cache, download all covers") - flag.Parse() } diff --git a/public/comic.html b/public/comic.html deleted file mode 100644 index 30690fe..0000000 --- a/public/comic.html +++ /dev/null @@ -1,27 +0,0 @@ - - - - - okoyono.de -- Comic des Monats - - - - - - - -

Comic des Monats

-

Neues Projekt: Eine Sammlung mit Comics die für Empfehlenswert hält:

- Jeden Monat ein neuer Comic aus seiner Sammlung. Die Buchlinks gehen zu Mojoreads, der - Code von Aaron Fischer. Ein økoyono Projekt.

- - *Eine Seite mit Buchempfehlungen findet Ihr [hier](https://buchdesmonats.okoyono.de)* - -
-
- Mojoreads cover -
-
- - diff --git a/public/index.html b/public/index.html deleted file mode 100644 index 1963cdc..0000000 --- a/public/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - okoyono.de -- Buch des Monats - - - -

Buch des Monats

- - Comic des Monats - Buch des Monats - - diff --git a/public/book.css b/static/book.css similarity index 100% rename from public/book.css rename to static/book.css diff --git a/public/comic.css b/static/comic.css similarity index 100% rename from public/comic.css rename to static/comic.css diff --git a/public/main.js b/static/main.js similarity index 100% rename from public/main.js rename to static/main.js diff --git a/public/vendors/imagesloaded-3.1.4.pkgd.min.js b/static/vendors/imagesloaded-3.1.4.pkgd.min.js similarity index 100% rename from public/vendors/imagesloaded-3.1.4.pkgd.min.js rename to static/vendors/imagesloaded-3.1.4.pkgd.min.js diff --git a/public/vendors/jquery-2.1.0.min.js b/static/vendors/jquery-2.1.0.min.js similarity index 100% rename from public/vendors/jquery-2.1.0.min.js rename to static/vendors/jquery-2.1.0.min.js diff --git a/public/vendors/masonry-3.1.5.pkgd.min.js b/static/vendors/masonry-3.1.5.pkgd.min.js similarity index 100% rename from public/vendors/masonry-3.1.5.pkgd.min.js rename to static/vendors/masonry-3.1.5.pkgd.min.js diff --git a/public/book.html b/templates/BOOK.mkd.html similarity index 63% rename from public/book.html rename to templates/BOOK.mkd.html index 0f0219b..2ecdf24 100644 --- a/public/book.html +++ b/templates/BOOK.mkd.html @@ -3,11 +3,11 @@ okoyono.de -- Buch des Monats - - - - - + + + + +

Buch des Monats

@@ -21,9 +21,11 @@ Ein økoyono Projekt.

+ {{ range . }}
- Mojoreads cover + Mojoreads cover
+ {{ end }}
diff --git a/templates/COMIC.mkd.html b/templates/COMIC.mkd.html new file mode 100644 index 0000000..d07b48d --- /dev/null +++ b/templates/COMIC.mkd.html @@ -0,0 +1,30 @@ + + + + + okoyono.de -- Comic des Monats + + + + + + + +

Comic des Monats

+

Neues Projekt: Eine Sammlung mit Comics die für Empfehlenswert hält. Jeden Monat ein neuer Comic aus seiner Sammlung. Die Buchlinks gehen zu Mojoreads, der + Code von Aaron Fischer. Ein økoyono Projekt.

+ + *Eine Seite mit Buchempfehlungen findet Ihr [hier](https://buchdesmonats.okoyono.de)* + +
+
+ {{ range . }} +
+ Mojoreads cover +
+ {{ end }} +
+
+ +