Skip to content

Commit

Permalink
Microservice running separately
Browse files Browse the repository at this point in the history
  • Loading branch information
slashexx committed Nov 2, 2024
1 parent baf4060 commit 4d1bcf4
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 68 deletions.
2 changes: 1 addition & 1 deletion backend/api-gateway/Dockerfile
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 . .
Expand Down
2 changes: 1 addition & 1 deletion backend/api-gateway/go.mod
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
60 changes: 35 additions & 25 deletions backend/api-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
21 changes: 17 additions & 4 deletions backend/code-execution-service/Dockerfile
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"]
2 changes: 1 addition & 1 deletion backend/code-execution-service/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module codebrewery/codeexecutionservice

go 1.23.2
go 1.23
60 changes: 31 additions & 29 deletions backend/code-execution-service/main.go
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))
}
11 changes: 4 additions & 7 deletions backend/docker-compose.yml
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
11 changes: 11 additions & 0 deletions frontend/Dockerfile
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"]

0 comments on commit 4d1bcf4

Please sign in to comment.