mirror of
https://github.com/NotAShelf/catApi.git
synced 2025-02-22 03:18:07 +00:00
This commit is contained in:
parent
4456e0d0a3
commit
2a73463318
1 changed files with 58 additions and 22 deletions
80
main.go
80
main.go
|
@ -4,13 +4,12 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -21,6 +20,49 @@ var images []string
|
||||||
var logger = logrus.New()
|
var logger = logrus.New()
|
||||||
var port string
|
var port string
|
||||||
|
|
||||||
|
// Okay I admit, this is bad. Just a workaround for now, until I figure out a clean
|
||||||
|
// way of displaying all images in a grid.
|
||||||
|
var tmpl = template.Must(template.New("index").Parse(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Image Gallery</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; text-align: center; }
|
||||||
|
.gallery {
|
||||||
|
column-count: 5;
|
||||||
|
column-gap: 8px;
|
||||||
|
}
|
||||||
|
.gallery a {
|
||||||
|
display: inline-block;
|
||||||
|
width: 100%;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
.gallery img {
|
||||||
|
width: 100%;
|
||||||
|
height: auto;
|
||||||
|
border-radius: 8px;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
@media (max-width: 1024px) { .gallery { column-count: 4; } }
|
||||||
|
@media (max-width: 768px) { .gallery { column-count: 3; } }
|
||||||
|
@media (max-width: 480px) { .gallery { column-count: 2; } }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Image Gallery</h1>
|
||||||
|
<div class="gallery">
|
||||||
|
{{range $index, $img := .Images}}
|
||||||
|
<a href="/api/id?id={{$index}}">
|
||||||
|
<img src="/api/id?id={{$index}}" alt="Image {{$index}}">
|
||||||
|
</a>
|
||||||
|
{{end}}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>`))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Log as JSON instead of the default ASCII formatter
|
// Log as JSON instead of the default ASCII formatter
|
||||||
logger.SetFormatter(&logrus.JSONFormatter{})
|
logger.SetFormatter(&logrus.JSONFormatter{})
|
||||||
|
@ -40,13 +82,9 @@ func main() {
|
||||||
log.Fatalf("Error reading configuration file: %v", err)
|
log.Fatalf("Error reading configuration file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
port := viper.GetString("server.port")
|
port = viper.GetString("server.port")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
|
||||||
log.Fatalf("Error reading configuration file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
images = getImages()
|
images = getImages()
|
||||||
|
|
||||||
// Add request logging middleware
|
// Add request logging middleware
|
||||||
|
@ -84,26 +122,20 @@ func getImages() []string {
|
||||||
return images
|
return images
|
||||||
}
|
}
|
||||||
|
|
||||||
func sanitizeInput(input string) string {
|
|
||||||
return template.HTMLEscapeString(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
io.WriteString(w, "<html><body><div style=\"display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); grid-gap: 10px;\">")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
for i := range images {
|
tmpl.Execute(w, struct {
|
||||||
io.WriteString(w, `<a href="/api/id?id=`+strconv.Itoa(i)+`">`)
|
Images []string
|
||||||
io.WriteString(w, `<img src="/api/id?id=`+strconv.Itoa(i)+`" style="width: 100%; height: auto;"/>`)
|
}{Images: images})
|
||||||
io.WriteString(w, `</a>`)
|
|
||||||
}
|
|
||||||
io.WriteString(w, "</div></body></html>")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func idHandler(w http.ResponseWriter, r *http.Request) {
|
func idHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
id := sanitizeInput(r.URL.Query().Get("id"))
|
id := r.URL.Query().Get("id")
|
||||||
if id == "" {
|
if id == "" {
|
||||||
http.Error(w, "Missing id", http.StatusBadRequest)
|
http.Error(w, "Missing id", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
i, err := strconv.Atoi(id)
|
i, err := strconv.Atoi(id)
|
||||||
if err != nil || i < 0 || i >= len(images) {
|
if err != nil || i < 0 || i >= len(images) {
|
||||||
http.Error(w, "Invalid id", http.StatusBadRequest)
|
http.Error(w, "Invalid id", http.StatusBadRequest)
|
||||||
|
@ -120,7 +152,7 @@ func idHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isValidImagePath(path string) bool {
|
func isValidImagePath(path string) bool {
|
||||||
if !filepath.HasPrefix(path, "images/") {
|
if !strings.HasPrefix(path, "images/") {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -135,11 +167,13 @@ func listHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
imageList = append(imageList, imageInfo)
|
imageList = append(imageList, imageInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, err := json.Marshal(imageList)
|
jsonData, err := json.Marshal(imageList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(jsonData)
|
w.Write(jsonData)
|
||||||
}
|
}
|
||||||
|
@ -155,7 +189,9 @@ func logRequest(next http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomHandler(w http.ResponseWriter, r *http.Request) {
|
func randomHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
rand.Seed(time.Now().UnixNano())
|
source := rand.NewSource(time.Now().UnixNano())
|
||||||
|
rand.New(source)
|
||||||
|
|
||||||
i := rand.Intn(len(images))
|
i := rand.Intn(len(images))
|
||||||
http.Redirect(w, r, "/api/id?id="+strconv.Itoa(i), http.StatusSeeOther)
|
http.Redirect(w, r, "/api/id?id="+strconv.Itoa(i), http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue