diff --git a/cmd/websrv/main.go b/cmd/websrv/main.go index 594d479..91eff57 100644 --- a/cmd/websrv/main.go +++ b/cmd/websrv/main.go @@ -1,43 +1,15 @@ package main import ( - "encoding/json" "fmt" - "io" "log" "net/http" "os" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/api" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" ) -type Container struct { - Products map[string]int `json:"products"` - Context Context `json:"context"` -} - -type Product struct { - Warehouse string `json:"warehouse"` - Quantity int `json:"quantity"` - Delivery int `json:"delivery_time"` -} - -type OutgoingProducts struct { - Products map[string][]Product `json:"products"` -} - -func NewOutgoingProducts() *OutgoingProducts { - op := OutgoingProducts{ - Products: make(map[string][]Product), - } - return &op -} - -type Context struct { - Country string `json:"country"` - State string `json:"state"` -} - var ( connectionString = "" port = ":8080" @@ -54,36 +26,8 @@ func main() { fmt.Println("Connectionstring fehlt!. Bsp.: :@tcp(127.0.0.1:3306)/elio?parseTime=true") return } - //Env Variablen nutzen - http.HandleFunc("/api/products", func(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - return - } - defer r.Body.Close() - data, err := io.ReadAll(r.Body) - if err != nil { - log.Println(err) - return - } - var payload database.Container - err = json.Unmarshal(data, &payload) - if err != nil { - log.Printf("Could not parse Json: %s", err) - return - } - result, err := nps.FetchData(&payload) - if err != nil { - //Todo Fehlerhandling - log.Println(err) - } - jsonResult, err := json.Marshal(result) - if err != nil { - log.Println(err) - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintln(w, string(jsonResult)) - }) + //Dependency Injection + http.HandleFunc("/api/products", api.GetProductApiHandleFunc(nps)) 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)) diff --git a/internal/api/products.go b/internal/api/products.go new file mode 100644 index 0000000..06865ea --- /dev/null +++ b/internal/api/products.go @@ -0,0 +1,70 @@ +package api + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" +) + +type Container struct { + Products map[string]int `json:"products"` + Context Context `json:"context"` +} + +type Product struct { + Warehouse string `json:"warehouse"` + Quantity int `json:"quantity"` + Delivery int `json:"delivery_time"` +} + +type OutgoingProducts struct { + Products map[string][]Product `json:"products"` +} + +func NewOutgoingProducts() *OutgoingProducts { + op := OutgoingProducts{ + Products: make(map[string][]Product), + } + return &op +} + +type Context struct { + Country string `json:"country"` + State string `json:"state"` +} + +func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + return + } + defer r.Body.Close() + data, err := io.ReadAll(r.Body) + if err != nil { + log.Println(err) + return + } + var payload database.Container + err = json.Unmarshal(data, &payload) + if err != nil { + log.Printf("Could not parse Json: %s", err) + return + } + result, err := nps.FetchData(&payload) + if err != nil { + //Todo Fehlerhandling + log.Println(err) + } + jsonResult, err := json.Marshal(result) + if err != nil { + log.Println(err) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, string(jsonResult)) + } +}