2023-06-04 22:54:29 -06:00

100 lines
2.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
// Define a function to handle incoming requests
func requestHandler(w http.ResponseWriter, r *http.Request) {
// Get the 'name' variable from the request
name := chi.URLParam(r, "name")
// Use a default value if 'name' is not present
if name == "" {
name = "World"
}
// Respond with a greeting message
fmt.Fprintf(w, "Hello, %s!\n", name)
}
type ImageProperties struct {
Width int `json:"width"`
Height int `json:"height"`
ColorSpace ColorSpace `json:"color_space"`
}
func fetchImage(w http.ResponseWriter, r *http.Request) {
var imageProps ImageProperties
err := json.NewDecoder(r.Body).Decode(&imageProps)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Now you can access the fields of imageProps, e.g.:
fmt.Printf("Received request for an image with width: %d, height: %d\n", imageProps.Width, imageProps.Height)
for _, color := range imageProps.ColorSpace {
fmt.Printf("Color code: %d, RGB: %v\n", color.Code, color.Color)
}
// img := ReadImage("image.jpg")
// img := ReadImage("image-out.jpg")
// img := ReadImage("dahlia-out.jpg")
img := ReadImage("dahlia.jpg")
bestCrop := GetBestPieceOfImage(imageProps.Width, imageProps.Height, img)
data := ConvertToEInkImage(bestCrop, imageProps.ColorSpace)
fmt.Printf("Bytes to send: %+v\n", len(data))
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
}
func basicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != "username" || password != "password" {
http.Error(w, "Unauthorized.", 401)
return
}
next.ServeHTTP(w, r)
})
}
func main() {
fmt.Println("Starting server")
// Create a new Chi router
router := chi.NewRouter()
router.Use(middleware.RequestID)
router.Use(middleware.RealIP)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
// Register the requestHandler function to handle requests at the root path
// and a path with the 'name' parameter
router.Get("/", requestHandler)
router.Get("/{name}", requestHandler)
router.Group(func(r chi.Router) {
r.Use(basicAuth)
r.Post("/fetchImage", fetchImage)
})
// Start the HTTP server on port 8080 and log any errors
log.Fatal(http.ListenAndServe("0.0.0.0:8080", router))
}