-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
101 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.20-alpine | ||
FROM golang:1.23-alpine | ||
|
||
WORKDIR /app | ||
COPY . . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module codebrewery/backend/apigateway | ||
|
||
go 1.23.2 | ||
go 1.23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module codebrewery/codeexecutionservice | ||
|
||
go 1.23.2 | ||
go 1.23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |