catApi/main.go

135 lines
3.6 KiB
Go
Raw Normal View History

2023-10-26 14:13:04 +00:00
package main
import (
2023-10-27 07:01:55 +00:00
"encoding/json"
"flag"
"html/template"
"io"
2023-10-26 14:13:04 +00:00
"log"
"math/rand"
"net/http"
"os"
2023-10-27 07:01:55 +00:00
"path/filepath"
2023-10-26 14:13:04 +00:00
"strconv"
"time"
2023-10-27 07:01:55 +00:00
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
2023-10-26 14:13:04 +00:00
)
2023-10-27 07:01:55 +00:00
var images []string
var logger = logrus.New()
var port string
func init() {
// Log as JSON instead of the default ASCII formatter
logger.SetFormatter(&logrus.JSONFormatter{})
// Output to stdout (or any other output you prefer)
logger.SetOutput(os.Stdout)
// Set the log level (info, warning, error, etc.)
logger.SetLevel(logrus.InfoLevel)
}
2023-10-26 14:13:04 +00:00
func main() {
2023-10-27 07:01:55 +00:00
flag.StringVar(&port, "port", "3000", "Port to run the server on")
flag.Parse()
2023-10-26 14:13:04 +00:00
2023-10-27 07:01:55 +00:00
// Initialize Viper for configuration management
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AutomaticEnv() // Allow environment variables to override config settings
2023-10-26 14:13:04 +00:00
2023-10-27 07:01:55 +00:00
// Read the configuration file (config.yaml in this example)
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading configuration file: %v", err)
}
2023-10-26 14:13:04 +00:00
2023-10-27 07:01:55 +00:00
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
images := getImages(r) // Call getImages with the request parameter
io.WriteString(w, "<html><body><div style=\"display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); grid-gap: 10px;\">")
for i, image := range images {
io.WriteString(w, `<a href="/api/id?id=`+strconv.Itoa(i)+`">`)
io.WriteString(w, `<img src="`+filepath.Base(image)+`" style="width: 100%; height: auto;"/>`)
io.WriteString(w, `</a>`)
}
io.WriteString(w, "</div></body></html>")
2023-10-26 14:13:04 +00:00
})
2023-10-27 07:01:55 +00:00
http.HandleFunc("/api/id", func(w http.ResponseWriter, r *http.Request) {
id := sanitizeInput(r.URL.Query().Get("id"))
if id == "" {
http.Error(w, "Missing id", http.StatusBadRequest)
return
}
i, err := strconv.Atoi(id)
if err != nil || i < 0 || i >= len(images) {
http.Error(w, "Invalid id", http.StatusBadRequest)
return
2023-10-26 14:13:04 +00:00
}
2023-10-27 07:01:55 +00:00
http.ServeFile(w, r, images[i])
2023-10-26 14:13:04 +00:00
})
http.HandleFunc("/api/list", func(w http.ResponseWriter, r *http.Request) {
2023-10-27 07:01:55 +00:00
// Create a slice to store image information
imageList := []map[string]string{}
for _, image := range images {
imageInfo := map[string]string{
"image": image,
"url": "/" + filepath.Base(image),
}
imageList = append(imageList, imageInfo)
}
// Convert the slice to JSON
jsonData, err := json.Marshal(imageList)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Set the content type to JSON
2023-10-26 14:13:04 +00:00
w.Header().Set("Content-Type", "application/json")
2023-10-27 07:01:55 +00:00
// Write the JSON response
w.Write(jsonData)
2023-10-26 14:13:04 +00:00
})
2023-10-27 07:01:55 +00:00
http.HandleFunc("/api/random", func(w http.ResponseWriter, r *http.Request) {
// Reseed the random number generator to make it truly random on each request
rand.Seed(time.Now().UnixNano())
i := rand.Intn(len(images))
http.ServeFile(w, r, images[i])
})
2023-10-26 14:13:04 +00:00
2023-10-27 07:01:55 +00:00
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid API path", http.StatusNotFound)
})
log.Println("Server started at port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
2023-10-26 14:13:04 +00:00
}
2023-10-27 07:01:55 +00:00
func getImages(r *http.Request) []string {
files, err := os.ReadDir("images/")
if err != nil {
logger.WithError(err).Fatal("Error reading images directory")
}
var images []string
serverAddress := r.Host // Get the server address from the request
2023-10-26 14:13:04 +00:00
for _, file := range files {
2023-10-27 07:01:55 +00:00
imagePath := "http://" + serverAddress + "/images/" + file.Name()
images = append(images, imagePath)
logger.Info("Loaded image:", imagePath)
2023-10-26 14:13:04 +00:00
}
2023-10-27 07:01:55 +00:00
return images
2023-10-26 14:13:04 +00:00
}
2023-10-27 07:01:55 +00:00
func sanitizeInput(input string) string {
return template.HTMLEscapeString(input)
2023-10-26 14:13:04 +00:00
}