Changed file structure to align with the recommendations given by https://github.com/golang-standards/project-layout

This commit is contained in:
2025-08-27 06:52:31 +02:00
parent adbd2dec20
commit 7e0a3cbcbe
8 changed files with 15 additions and 11 deletions

1
api/Admin.go Normal file
View File

@@ -0,0 +1 @@
package api

58
api/FileHandlers.go Normal file
View File

@@ -0,0 +1,58 @@
package api
import (
"encoding/json"
"net/http"
"os"
"path/filepath"
)
type FileData struct {
ImageNames []string
SpicyImageNames []string
AsciiFiles []AsciiEntry
}
type AsciiEntry struct {
Name string
FontSize int
}
const staticDir = "static"
// move FileData and AsciiEntry here if you want, or leave in main.go
func FileHandler(w http.ResponseWriter, r *http.Request) {
data := FileData{
ImageNames: []string{},
SpicyImageNames: []string{},
AsciiFiles: []AsciiEntry{},
}
files, _ := os.ReadDir(filepath.Join(staticDir, "images"))
for _, file := range files {
fileName := file.Name()
data.ImageNames = append(data.ImageNames, filepath.Join(staticDir, "images", fileName))
}
files, _ = os.ReadDir(filepath.Join("uploads"))
for _, file := range files {
fileName := file.Name()
data.ImageNames = append(data.ImageNames, filepath.Join("uploads", fileName))
}
files, _ = os.ReadDir(filepath.Join(staticDir, "spicy"))
for _, file := range files {
fileName := file.Name()
data.SpicyImageNames = append(data.SpicyImageNames, filepath.Join(staticDir, "spicy", fileName))
}
files, _ = os.ReadDir(filepath.Join(staticDir, "ascii_art"))
for _, file := range files {
fileName := file.Name()
data.AsciiFiles = append(data.AsciiFiles,
AsciiEntry{Name: filepath.Join(staticDir, "ascii_art", fileName), FontSize: 12},
)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}

36
api/UploadHandlers.go Normal file
View File

@@ -0,0 +1,36 @@
package api
import (
"fmt"
"io"
"net/http"
"os"
)
func UploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
file, header, err := r.FormFile("image") // "image" is the name of the file input
if err != nil {
http.Error(w, "Error retrieving file", http.StatusBadRequest)
return
}
defer file.Close()
dst, err := os.Create("./uploads/" + header.Filename)
if err != nil {
http.Error(w, "Error creating file on server", http.StatusInternalServerError)
return
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, "Error saving file", http.StatusInternalServerError)
return
}
fmt.Fprint(w, "<script>location.href = '/admin/'</script>")
fmt.Fprintf(w, "Image uploaded successfully: %s", header.Filename)
}