feat: drop bunnycdn

This commit is contained in:
Pihkaal
2025-10-11 17:37:36 +02:00
parent d07eddc502
commit 3827176093
84 changed files with 31 additions and 48 deletions

76
main.go
View File

@@ -2,7 +2,6 @@ package main
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
@@ -10,66 +9,40 @@ import (
"net/http"
"os"
"strings"
"time"
)
//go:embed public/index.html
var indexSrc string
const PORT = 3000
const CACHE_TTL = 5 * time.Minute
const STORAGE_URL = "https://storage.bunnycdn.com/lilou-cat/"
const CDN_URL = "https://lilou-cat.b-cdn.net/"
const IMAGES_DIR = "images"
type Image struct {
ObjectName string
}
var cachedImages []string
var (
cachedImages []string
cacheExpiration time.Time
)
func loadImages() error {
imagesDir := fmt.Sprintf("public/%s", IMAGES_DIR)
func list_images() []string {
if time.Now().Before(cacheExpiration) {
return cachedImages
}
req, _ := http.NewRequest("GET", STORAGE_URL, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("AccessKey", os.Getenv("API_KEY"))
res, err := http.DefaultClient.Do(req)
entries, err := os.ReadDir(imagesDir)
if err != nil {
fmt.Printf("error: %s\n", err)
return []string{}
return fmt.Errorf("ERROR: Failed to read public/%s directory: %w", IMAGES_DIR, err)
}
var images = []Image{}
cachedImages = []string{}
err = json.NewDecoder(res.Body).Decode(&images)
defer res.Body.Close()
if err != nil {
fmt.Printf("error: %s\n", err)
return []string{}
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".webp") {
cachedImages = append(cachedImages, entry.Name())
}
}
var imageUrls = []string{}
for _, image := range images {
imageUrls = append(imageUrls, fmt.Sprintf("%s/%s", CDN_URL, image.ObjectName))
}
cachedImages = imageUrls
cacheExpiration = time.Now().Add(CACHE_TTL)
return imageUrls
fmt.Printf("OK: Loaded %d images\n", len(cachedImages))
return nil
}
func getPage(w http.ResponseWriter, r *http.Request) {
var images = list_images()
var image = images[rand.Intn(len(images))]
src := strings.Replace(indexSrc, "{{ IMAGE_URL }}", image, 1)
imageName := cachedImages[rand.Intn(len(cachedImages))]
imageURL := fmt.Sprintf("/%s/%s", IMAGES_DIR, imageName)
src := strings.Replace(indexSrc, "{{ IMAGE_URL }}", imageURL, 1)
io.WriteString(w, src)
}
@@ -79,15 +52,26 @@ func getFavicon(w http.ResponseWriter, r *http.Request) {
}
func main() {
if err := loadImages(); err != nil {
fmt.Printf("ERROR: Error loading images: %s\n", err)
os.Exit(1)
}
if len(cachedImages) == 0 {
fmt.Printf("ERROR: no images found in public/%s\n", IMAGES_DIR)
os.Exit(1)
}
http.HandleFunc("/", getPage)
http.HandleFunc("/favicon.ico", getFavicon)
http.Handle(fmt.Sprintf("/%s/", IMAGES_DIR), http.StripPrefix(fmt.Sprintf("/%s/", IMAGES_DIR), http.FileServer(http.Dir(fmt.Sprintf("public/%s", IMAGES_DIR)))))
fmt.Printf("OK: Listening on port %d\n", PORT)
err := http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
fmt.Printf("listening on port %d\n", PORT)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
fmt.Printf("OK: Server closed\n")
} else if err != nil {
fmt.Printf("error starting server: %s\n", err)
fmt.Printf("ERROR: error starting server: %s\n", err)
os.Exit(1)
}
}