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

View File

@@ -1,8 +1,6 @@
services: services:
lilou-cat: lilou-cat:
image: pihkaal/lilou-cat:latest image: pihkaal/lilou-cat:latest
environment:
- API_KEY
networks: networks:
- web - web
labels: labels:

76
main.go
View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@@ -87,6 +87,7 @@
img { img {
width: 100%; width: 100%;
max-width: 50vh; max-width: 50vh;
rotate: 90deg;
} }
@media (min-width: 768px) { @media (min-width: 768px) {