All checks were successful
CI Pipeline for the Go API / Kubernetes Smoke Test (push) Successful in 14s
CI Pipeline for the Go API / Go Vet (basic static check) (push) Successful in 17s
CI Pipeline for the Go API / Docker Image Build Test (push) Successful in 26s
CI Pipeline for the Go API / 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) Successful in 5s
55 lines
1.1 KiB
Go
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: "healthy"})
|
|
}
|
|
|
|
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)
|
|
}
|