Files
Add-API/main.go
2025-10-04 15:59:21 +02:00

55 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
)
type HealthResponse struct {
Status string `json:"status"`
}
type AddRequest struct {
PinCode int `json:"pinCode"`
N1 int `json:"n1"`
N2 int `json:"n2"`
}
type AddResponse struct {
Output int `json:"output"`
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(HealthResponse{Status: "ok"})
}
func addHandler(w http.ResponseWriter, r *http.Request) {
var req AddRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, "invalid json", http.StatusBadRequest)
return
}
if req.PinCode != 1234 {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
result := req.N1 + req.N2
resp := AddResponse{Output: result}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}
func main() {
http.HandleFunc("/health", healthHandler)
http.HandleFunc("/add", addHandler)
fmt.Println("Server running on port 8080...")
http.ListenAndServe(":8080", nil)
}