Files
next-book/main.go
2025-05-02 14:36:51 +01:00

53 lines
946 B
Go

package main
import (
"fmt"
"html/template"
"net/http"
"os"
"time"
"nextbook/pkg/storygraph"
"github.com/joho/godotenv"
)
var (
port string
)
func init() {
godotenv.Load(os.Getenv("DOTENV_DIR") + ".env")
var ok bool
port, ok = os.LookupEnv("PORT")
if !ok {
port = "8000"
}
}
func main() {
latestBooks := map[string]*storygraph.Book{}
go func() {
var err error
for {
latestBooks, err = storygraph.GetLatestBooks()
if err != nil {
fmt.Println("Error fetching latest books:", err)
return
}
fmt.Println("Updated latest book recommendations")
time.Sleep(1 * time.Hour)
}
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/index.html"))
tmpl.Execute(w, latestBooks)
})
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
}