Upgrade codebase to go 1.20 fix some outdated stuff

* New go version 1.20
* Reformat code
* Clean up regex
* Remove deprecated function calls (ioutils)
* Handle all errors
* Fix typos

Signed-off-by: Aaron Fischer <mail@aaron-fischer.net>
This commit is contained in:
Aaron Fischer 2023-07-18 21:21:16 +02:00
parent 6110eddf68
commit 7db4b81ce3
4 changed files with 30 additions and 21 deletions

4
go.mod
View file

@ -1,5 +1,3 @@
module okoyono.de/buchdesmonats
go 1.16
require github.com/a-h/gemini v0.0.61
go 1.20

2
go.sum
View file

@ -1,2 +0,0 @@
github.com/a-h/gemini v0.0.61 h1:WSV9f5T6Vut9gIATUhqAh9KfsA4/kqcUIbTiDbsn1JY=
github.com/a-h/gemini v0.0.61/go.mod h1:p9wvIRDc2s3Lnbkw7CgNzDNgJHPuXDwh3dOF7w0NT8s=

View file

@ -2,15 +2,13 @@ package main
import (
"fmt"
//"html/template"
xmltpl "text/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
xmltpl "text/template"
)
type Item struct {
@ -18,7 +16,7 @@ type Item struct {
Author string
Title string
Filename string
Date string
Date string
}
func (i Item) ImageURL() string {
@ -41,14 +39,24 @@ func (i Item) downloadCover() error {
return err
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal("Can not close the response body")
}
}(resp.Body)
out, err := os.Create(i.targetFilename())
if err != nil {
return err
}
defer out.Close()
defer func(out *os.File) {
err := out.Close()
if err != nil {
log.Fatal("Can not close the file")
}
}(out)
_, err = io.Copy(out, resp.Body)
@ -65,16 +73,21 @@ func getItems(filename string) []Item {
log.Fatal(filename + " is missing")
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal("Can not close the response body")
}
}(resp.Body)
content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("Can not download the file. Network problem?")
}
currentYear := ""
currentMonth := 0
re := regexp.MustCompile(`^[^[]+ \[(?P<author>[^"]+)"(?P<title>[^"]+)"\]\([^=]+=(?P<isbn>[0-9]+).*$`)
re := regexp.MustCompile(`^[^[]+ \[(?P<author>[^"]+)"(?P<title>[^"]+)"]\([^=]+=(?P<isbn>[0-9]+).*$`)
yearRe := regexp.MustCompile(`^## (?P<year>20[0-9]{2})$`)
var yearBucket []Item
@ -86,7 +99,7 @@ func getItems(filename string) []Item {
currentMonth = 0
// Add the bucket in reverse order
for i := len(yearBucket)-1; i >= 0; i-- {
for i := len(yearBucket) - 1; i >= 0; i-- {
items = append(items, yearBucket[i])
}
yearBucket = nil
@ -97,10 +110,10 @@ func getItems(filename string) []Item {
currentMonth++
yearBucket = append(yearBucket, Item{
Author: strings.Trim(matches[1], " "),
Title: strings.Trim(matches[2], " "),
ISBN: matches[3],
Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
Author: strings.Trim(matches[1], " "),
Title: strings.Trim(matches[2], " "),
ISBN: matches[3],
Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
Filename: filename,
})
}
@ -118,7 +131,7 @@ func getTemplate(sourceFile string, templateFilename string, w http.ResponseWrit
// Get all items from the git repo
items := getItems(sourceFile)
// Generate the restulting HTML
// Generate the resulting HTML
t, err := xmltpl.ParseFiles("templates/" + templateFilename)
if err != nil {
panic(err)

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>okoyono.de -- Buch des Monats</title>