From 320b61cda53c1e5814041331d7c2e844ba1420e0 Mon Sep 17 00:00:00 2001 From: zomborip Date: Sat, 4 Oct 2025 15:59:21 +0200 Subject: [PATCH] =?UTF-8?q?Kont=C3=A9neriz=C3=A1lt=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 9 ++++++++ docker-compose.yml | 8 +++++++ main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 main.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cbadea6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM golang:1.23-alpine + +WORKDIR /app +COPY . . + +RUN go build -o go-api main.go + +EXPOSE 8080 +CMD ["./go-api"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a0588f1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: '3.8' + +services: + go-api: + build: . + container_name: go-api + ports: + - "8080:8080" diff --git a/main.go b/main.go new file mode 100644 index 0000000..bdd9065 --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +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) +}