2025-07-19 19:27:47 +02:00

81 lines
2.4 KiB
Go

package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/api"
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
)
var (
connectionString = ""
port = ":8080"
)
type (
//RegistrierungsID -> ProductID -> Product
ProductStore map[string]map[string]database.Product
)
const (
curlhelp = `
Bewerbungsaufgabe von Marco Kittel 2025
Produkte abrufen
curl -X POST localhost:8080/api/products -d '{ "products": { "A6053": 2, "B3009": 1200 }, "context": { "country": "EU", "state": "" } }'
Reservierung reservieren
curl -X POST localhost:8080/api/products/reserve -d '{ "products": { "A6053": 2, "B3009": 1200 }, "context": { "country": "EU", "state": "" } }'
Reservierung bestätigen
curl -X POST localhost:8080/api/products/confirm -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
Reservierung abbrechen
curl -X POST localhost:8080/api/products/abort -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
Reservierung freigeben
curl -X POST localhost:8080/api/products/release -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
`
)
func main() {
connectionString := os.Getenv("CONNECTIONSTRING")
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
// Brauche Context für Asyncrone Calls. Darum nutze ich DI. Ist ein Golang Antipattern :-(
// Muss noch mehr Lernen um idiomatisch zu werden
nps := database.NewProductService(ctx, connectionString)
//Hier prüfe ich nach alten Registrierungen und gebe Sie frei.
nps.Autorelease()
if len(connectionString) == 0 {
fmt.Println("Connectionstring fehlt!. Bsp.: <user>:<passwort>@tcp(127.0.0.1:3306)/elio?parseTime=true")
return
}
http.HandleFunc("/api/products", api.GetProductApiHandleFunc(nps))
http.HandleFunc("/api/products/reserve", api.GetProductReservationApiHandleFunc(nps))
http.HandleFunc("/api/products/confirm", api.GetConfirmReservationApiHandleFunc(nps))
http.HandleFunc("/api/products/release", api.GetReleaseReservationApiHandleFunc(nps))
http.HandleFunc("/api/products/abort", api.GetAbortApiHandleFunc(nps))
go func() {
log.Printf("Easy Peasy: Die Party startet auf Port %s\n", port)
log.Printf("Probiers mal damit: %s\n", curlhelp)
log.Fatal(http.ListenAndServe(port, nil))
}()
<-ctx.Done()
log.Println("Beende Server in drei Sekunden...")
time.Sleep(time.Second * 3)
}