Files
Add-API/main.go
zomborip cd4864dad5
Some checks failed
CI Pipeline for the Go API / CD - Kubernetes Smoke Test (push) Successful in 7s
CI Pipeline for the Go API / CI - Go Vet (basic static check) (push) Successful in 9s
CI Pipeline for the Go API / CI - Docker Image Build Test (push) Successful in 18s
CI Pipeline for the Go API / CI - Go Build & Cross-Compile (push) Successful in 31s
CI Pipeline for the Go API / Push Image to Gitea Registry (push) Successful in 17s
CI Pipeline for the Go API / Kubernetes Deployment (push) Failing after 4m5s
Correct Tags
2025-10-05 22:39:06 +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: "oke"})
}
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)
}