We are now a webserver with a webhook :P

This commit is contained in:
Aaron Fischer 2021-03-18 23:16:26 +01:00
parent 646659afb8
commit 22d36d5a89
11 changed files with 90 additions and 68 deletions

View file

@ -1,10 +1,11 @@
package main
import (
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
@ -15,7 +16,7 @@ type Item struct {
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 getHTML(filename string, w http.ResponseWriter) {
// Get all items from the git repo
items := getItems(filename)
// 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 {
_, err := os.Stat(item.targetFilename())
if os.IsNotExist(err) || force {
fmt.Printf("Downloading %v ...\n", item.imageURL())
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())
}
}
})
// TODO: use template to generate the restulting HTML
}
// 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)
})
func init() {
flag.StringVar(&filename, "filename", "COMIC.mkd", "The filename to use")
flag.BoolVar(&force, "force", false, "Ignore cache, download all covers")
flag.Parse()
err := http.ListenAndServe(":1337", nil)
if err != nil {
panic(err)
}
}

View file

@ -1,27 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>okoyono.de -- Comic des Monats</title>
<link rel="stylesheet" href="comic.css" />
<script src="vendors/jquery-2.1.0.min.js"></script>
<script src="vendors/masonry-3.1.5.pkgd.min.js"></script>
<script src="vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
<script src="main.js"></script>
</head>
<body>
<h1>Comic des Monats</h1>
<p>Neues Projekt: Eine Sammlung mit Comics die <a href="https://social.okoyono.de/@mezzo" rel="author">Michael
Reutter</a> für Empfehlenswert hält:</p><p>
Jeden Monat ein neuer Comic aus seiner Sammlung. Die Buchlinks gehen zu <a href="http://mojoreads.de/">Mojoreads</a>, <a href="https://git.okoyono.de/mezzo/buch_des_monats">der
Code</a> von <a href="https://aaron-fischer.net/">Aaron Fischer</a>. Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
*Eine Seite mit Buchempfehlungen findet Ihr [hier](https://buchdesmonats.okoyono.de)*
<div id="covers">
<div class="cover-item">
<a href="#"><img src="" alt="Mojoreads cover" title="zu Mojoreads" /></a>
</div>
</div>
</body>
</html>

View file

@ -1,14 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>okoyono.de -- Buch des Monats</title>
<link rel="stylesheet" href="book.css" />
</head>
<body>
<h1>Buch des Monats</h1>
<a href="comic.html">Comic des Monats</a>
<a href="book.html">Buch des Monats</a>
</body>
</html>

View file

@ -3,11 +3,11 @@
<head>
<meta charset="utf-8" />
<title>okoyono.de -- Buch des Monats</title>
<link rel="stylesheet" href="book.css" />
<script src="vendors/jquery-2.1.0.min.js"></script>
<script src="vendors/masonry-3.1.5.pkgd.min.js"></script>
<script src="vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" href="/static/book.css" />
<script src="/static/vendors/jquery-2.1.0.min.js"></script>
<script src="/static/vendors/masonry-3.1.5.pkgd.min.js"></script>
<script src="/static/vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
<script src="/static/main.js"></script>
</head>
<body>
<h1>Buch des Monats</h1>
@ -21,9 +21,11 @@
Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
<div id="covers">
{{ range . }}
<div class="cover-item">
<a href="#"><img src="" alt="Mojoreads cover" title="zu Mojoreads" /></a>
<a href="https://mojoreads.de/book/?isbn={{ .ISBN }}&ref=mezzo&aff=mr"><img src="/covers/{{ .ISBN }}.jpg" alt="Mojoreads cover" title="zu Mojoreads" /></a>
</div>
{{ end }}
</div>
</body>
</html>

30
templates/COMIC.mkd.html Normal file
View file

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>okoyono.de -- Comic des Monats</title>
<link rel="stylesheet" href="/static/comic.css" />
<script src="/static/vendors/jquery-2.1.0.min.js"></script>
<script src="/static/vendors/masonry-3.1.5.pkgd.min.js"></script>
<script src="/static/vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
<script src="/static/main.js"></script>
</head>
<body>
<h1>Comic des Monats</h1>
<p>Neues Projekt: Eine Sammlung mit Comics die <a href="https://social.okoyono.de/@mezzo" rel="author">Michael
Reutter</a> für Empfehlenswert hält. Jeden Monat ein neuer Comic aus seiner Sammlung. Die Buchlinks gehen zu <a href="http://mojoreads.de/">Mojoreads</a>, <a href="https://git.okoyono.de/mezzo/buch_des_monats">der
Code</a> von <a href="https://aaron-fischer.net/">Aaron Fischer</a>. Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
*Eine Seite mit Buchempfehlungen findet Ihr [hier](https://buchdesmonats.okoyono.de)*
<div id="covers">
<div class="cover-item">
{{ range . }}
<div class="cover-item">
<a href="https://mojoreads.de/book/?isbn={{ .ISBN }}&ref=mezzo&aff=mr"><img src="/covers/{{ .ISBN }}.jpg" alt="Mojoreads cover" title="zu Mojoreads" /></a>
</div>
{{ end }}
</div>
</div>
</body>
</html>