diff --git a/backend/api-gateway/Dockerfile b/backend/api-gateway/Dockerfile index 67b6d2a..96ff061 100644 --- a/backend/api-gateway/Dockerfile +++ b/backend/api-gateway/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine +FROM golang:1.23-alpine WORKDIR /app COPY . . diff --git a/backend/api-gateway/go.mod b/backend/api-gateway/go.mod index 6ddbfc8..ee1a3cd 100644 --- a/backend/api-gateway/go.mod +++ b/backend/api-gateway/go.mod @@ -1,3 +1,3 @@ module codebrewery/backend/apigateway -go 1.23.2 +go 1.23 diff --git a/backend/api-gateway/main.go b/backend/api-gateway/main.go index 4e51b7d..fdfe73f 100644 --- a/backend/api-gateway/main.go +++ b/backend/api-gateway/main.go @@ -2,40 +2,50 @@ package main import ( - "bytes" - "encoding/json" - "log" - "net/http" + "bytes" + "encoding/json" + "log" + "net/http" ) type CodeRequest struct { - Language string `json:"language"` - Code string `json:"code"` + Language string `json:"language"` + Code string `json:"code"` } func executeCode(w http.ResponseWriter, r *http.Request) { - var req CodeRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, "Invalid request", http.StatusBadRequest) - return - } + var req CodeRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "Invalid request", http.StatusBadRequest) + return + } - // Forward the request to the Code Execution Service - resp, err := http.Post("http://code-execution-service:8080/execute", "application/json", bytes.NewBuffer(req)) - if err != nil { - http.Error(w, "Failed to execute code", http.StatusInternalServerError) - return - } - defer resp.Body.Close() + // Marshal the request to JSON + reqBody, err := json.Marshal(req) + if err != nil { + http.Error(w, "Failed to marshal request", http.StatusInternalServerError) + return + } - // Return the response from the Code Execution Service - w.WriteHeader(resp.StatusCode) - var result map[string]interface{} - json.NewDecoder(resp.Body).Decode(&result) - json.NewEncoder(w).Encode(result) + // Forward the request to the Code Execution Service + resp, err := http.Post("http://code-execution-service:8080/execute", "application/json", bytes.NewBuffer(reqBody)) + if err != nil { + http.Error(w, "Failed to execute code", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + // Return the response from the Code Execution Service + w.WriteHeader(resp.StatusCode) + var result map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + http.Error(w, "Failed to decode response", http.StatusInternalServerError) + return + } + json.NewEncoder(w).Encode(result) } func main() { - http.HandleFunc("/execute", executeCode) - log.Fatal(http.ListenAndServe(":8081", nil)) + http.HandleFunc("/execute", executeCode) + log.Fatal(http.ListenAndServe(":8081", nil)) } diff --git a/backend/code-execution-service/Dockerfile b/backend/code-execution-service/Dockerfile index bf682a7..ab293b5 100644 --- a/backend/code-execution-service/Dockerfile +++ b/backend/code-execution-service/Dockerfile @@ -1,8 +1,21 @@ -FROM golang:1.20-alpine +# Stage 1: Build +FROM golang:1.18-alpine AS build WORKDIR /app -COPY . . -RUN go build -o code-execution-service . -CMD ["./code-execution-service"] +COPY go.mod main.go ./ +RUN go mod download + + +RUN go build -o codeexec ./main.go + + +FROM alpine:latest + +WORKDIR /app + + +COPY --from=build /app/codeexec . + +CMD ["./codeexec"] diff --git a/backend/code-execution-service/go.mod b/backend/code-execution-service/go.mod index fe6535a..7bee8ef 100644 --- a/backend/code-execution-service/go.mod +++ b/backend/code-execution-service/go.mod @@ -1,3 +1,3 @@ module codebrewery/codeexecutionservice -go 1.23.2 +go 1.23 diff --git a/backend/code-execution-service/main.go b/backend/code-execution-service/main.go index 08ab999..e1bfef5 100644 --- a/backend/code-execution-service/main.go +++ b/backend/code-execution-service/main.go @@ -1,50 +1,52 @@ -package codeexecutionservice +package main import ( - "encoding/json" - "log" - "net/http" - "os/exec" - "sync" + "encoding/json" + "fmt" + "log" + "net/http" + "os/exec" + "sync" ) type CodeRequest struct { - Language string `json:"language"` - Code string `json:"code"` + Language string `json:"language"` + Code string `json:"code"` } type CodeResponse struct { - Output string `json:"output"` - Error string `json:"error"` + Output string `json:"output"` + Error string `json:"error"` } var mu sync.Mutex func executeCode(language, code string) (string, string) { - mu.Lock() - defer mu.Unlock() + mu.Lock() + defer mu.Unlock() - cmd := exec.Command("docker", "run", "--rm", "-i", language, "sh", "-c", code) - output, err2 := cmd.CombinedOutput() - return string(output), string(err2.Error()) + cmd := exec.Command("docker", "run", "--rm", "-i", language, "sh", "-c", code) + output, err2 := cmd.CombinedOutput() + return string(output), string(err2.Error()) } func runHandler(w http.ResponseWriter, r *http.Request) { - var req CodeRequest - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, "Invalid request", http.StatusBadRequest) - return - } - - output, err := executeCode(req.Language, req.Code) - response := CodeResponse{ - Output: output, - Error: err, - } - json.NewEncoder(w).Encode(response) + var req CodeRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "Invalid request", http.StatusBadRequest) + return + } + + output, err := executeCode(req.Language, req.Code) + response := CodeResponse{ + Output: output, + Error: err, + } + json.NewEncoder(w).Encode(response) } func main() { - http.HandleFunc("/execute", runHandler) - log.Fatal(http.ListenAndServe(":8080", nil)) + http.HandleFunc("/execute", runHandler) + fmt.Print("code-execution-service started at port 8080\n") + log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index 38af64f..fd1bb5c 100644 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -1,15 +1,12 @@ -version: '3.8' services: - code-execution-service: + api-gateway: build: - context: ./code-execution-service + context: ./api-gateway ports: - "8080:8080" - api-gateway: + code-execution-service: build: - context: ./api-gateway + context: ./code-execution-service ports: - "8081:8081" - depends_on: - - code-execution-service diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..7f78d77 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,11 @@ +FROM node:20-alpine + +WORKDIR /app + +COPY package.json ./ +COPY package-lock.json ./ +RUN npm install + +COPY . . + +CMD ["npm", "run", "dev"]