2023-11-28 15:52:53 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-11-29 16:00:35 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-11-28 15:52:53 +01:00
|
|
|
|
|
|
|
"git.velvettear.de/velvettear/loggo"
|
2023-11-28 16:34:10 +01:00
|
|
|
"git.velvettear.de/velvettear/slideshow-api/internal"
|
|
|
|
"git.velvettear.de/velvettear/slideshow-api/internal/config"
|
2023-11-28 15:52:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// start the web server
|
|
|
|
func Run() {
|
|
|
|
address := config.ServerAddress + ":" + strconv.Itoa(config.ServerPort)
|
|
|
|
loggo.Info("starting web server...", "address: "+address)
|
|
|
|
registerHandlers()
|
|
|
|
error := http.ListenAndServe(address, nil)
|
|
|
|
if error != nil {
|
|
|
|
loggo.Fatal("encountered an error starting the web server", "address: "+address, error.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// register the handlers
|
|
|
|
func registerHandlers() {
|
2023-11-29 16:00:35 +01:00
|
|
|
http.HandleFunc("/", serveRandomImageName)
|
|
|
|
http.HandleFunc("/image/", serveImage)
|
|
|
|
http.HandleFunc("/palette/", servePalette)
|
2023-11-28 15:52:53 +01:00
|
|
|
}
|
|
|
|
|
2023-11-29 16:00:35 +01:00
|
|
|
// request url: '/' - serve the name of a random image
|
|
|
|
func serveRandomImageName(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
timestamp := time.Now().UnixMilli()
|
|
|
|
var response response
|
2023-11-30 14:36:49 +01:00
|
|
|
response.Content, response.error = internal.GetRandomImage()
|
2023-11-29 16:00:35 +01:00
|
|
|
if response.error != nil {
|
|
|
|
response.StatusCode = 404
|
2023-11-28 15:52:53 +01:00
|
|
|
}
|
2023-11-29 16:00:35 +01:00
|
|
|
response.send(writer)
|
2023-11-30 14:16:39 +01:00
|
|
|
loggo.DebugTimed("served random image '"+response.Content+"'", timestamp)
|
2023-11-29 16:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// request url: '/image' - serve an image
|
|
|
|
func serveImage(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
var response response
|
|
|
|
image, _ := strings.CutPrefix(request.URL.Path, "/image/")
|
|
|
|
image, error := internal.GetImagePath(image)
|
2023-11-28 15:52:53 +01:00
|
|
|
if error != nil {
|
2023-11-29 16:00:35 +01:00
|
|
|
response.error = error
|
|
|
|
response.StatusCode = 404
|
|
|
|
response.send(writer)
|
2023-11-28 15:52:53 +01:00
|
|
|
return
|
|
|
|
}
|
2023-11-29 16:27:23 +01:00
|
|
|
streamImage(writer, image, request.URL.Query().Get("resolution"))
|
2023-11-29 16:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// request url: '/palette' - serve the color palette of an image
|
|
|
|
func servePalette(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
var response response
|
|
|
|
image, _ := strings.CutPrefix(request.URL.Path, "/palette/")
|
|
|
|
image, error := internal.GetImagePath(image)
|
2023-11-28 15:52:53 +01:00
|
|
|
if error != nil {
|
2023-11-29 16:00:35 +01:00
|
|
|
response.error = error
|
|
|
|
response.StatusCode = 404
|
|
|
|
response.send(writer)
|
2023-11-28 15:52:53 +01:00
|
|
|
return
|
|
|
|
}
|
2023-11-29 16:43:30 +01:00
|
|
|
amount, _ := strconv.Atoi(request.URL.Query().Get("colors"))
|
|
|
|
if amount <= 0 {
|
|
|
|
amount = 16
|
|
|
|
}
|
|
|
|
streamColorPalette(writer, image, amount, request.URL.Query().Get("algorithm"))
|
2023-11-28 15:52:53 +01:00
|
|
|
}
|