-
Notifications
You must be signed in to change notification settings - Fork 2
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
31 changed files
with
1,495 additions
and
122 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
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
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 @@ | ||
# Use a slim Node.js image | ||
FROM node:18-slim | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
|
||
# Install any dependencies if necessary (you can skip if no dependencies) | ||
# COPY package*.json ./ | ||
# RUN npm install | ||
|
||
# No entry point or CMD needed as you'll provide the command at runtime |
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,33 @@ | ||
package node | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func RunJavaScriptCode(code string, input string) (string, string, error) { | ||
cmd := exec.Command( | ||
"docker", "run", "--rm", | ||
"-i", // allows standard input to be passed in | ||
"apps-node-sandbox", // Docker image with Node.js environment | ||
"node", "-e", code, // Runs JavaScript code with Node.js | ||
) | ||
|
||
// Pass input to the JavaScript script | ||
cmd.Stdin = bytes.NewBufferString(input) | ||
|
||
// Capture standard output and error output | ||
var output bytes.Buffer | ||
var errorOutput bytes.Buffer | ||
cmd.Stdout = &output | ||
cmd.Stderr = &errorOutput | ||
|
||
// Run the command | ||
if err := cmd.Run(); err != nil { | ||
return "", fmt.Sprintf("Command execution failed: %s: %v", errorOutput.String(), err), nil | ||
} | ||
|
||
return strings.TrimSuffix(output.String(), "\n"), strings.TrimSuffix(errorOutput.String(), "\n"), 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
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package handlers | ||
|
||
import ( | ||
"cloud.google.com/go/firestore" | ||
"encoding/json" | ||
"execution-service/models" | ||
"execution-service/utils" | ||
"google.golang.org/api/iterator" | ||
"net/http" | ||
) | ||
|
||
func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
var test models.Test | ||
if err := utils.DecodeJSONBody(w, r, &test); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Basic validation for question title and question ID | ||
if test.QuestionDocRefId == "" || test.QuestionTitle == "" { | ||
http.Error(w, "QuestionDocRefId and QuestionTitle are required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Normalise test cases | ||
test.VisibleTestCases = utils.NormaliseTestCaseFormat(test.VisibleTestCases) | ||
test.HiddenTestCases = utils.NormaliseTestCaseFormat(test.HiddenTestCases) | ||
|
||
// Automatically populate validation for input and output in test case | ||
test.InputValidation = utils.GetDefaultValidation() | ||
test.OutputValidation = utils.GetDefaultValidation() | ||
|
||
// Validate test case format | ||
if _, err := utils.ValidateTestCaseFormat(test.VisibleTestCases, test.InputValidation, | ||
test.OutputValidation); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
if _, err := utils.ValidateTestCaseFormat(test.HiddenTestCases, test.InputValidation, | ||
test.OutputValidation); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Check if a test already exists for the question | ||
iter := s.Client.Collection("tests").Where("questionDocRefId", "==", test.QuestionDocRefId).Documents(ctx) | ||
for { | ||
_, err := iter.Next() | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
http.Error(w, "Error fetching test", http.StatusInternalServerError) | ||
return | ||
} | ||
http.Error(w, "Test already exists for the question", http.StatusConflict) | ||
return | ||
} | ||
defer iter.Stop() | ||
|
||
// Save test to Firestore | ||
docRef, _, err := s.Client.Collection("tests").Add(ctx, map[string]interface{}{ | ||
"questionDocRefId": test.QuestionDocRefId, | ||
"questionTitle": test.QuestionTitle, | ||
"visibleTestCases": test.VisibleTestCases, | ||
"hiddenTestCases": test.HiddenTestCases, | ||
"inputValidation": test.InputValidation, | ||
"outputValidation": test.OutputValidation, | ||
"createdAt": firestore.ServerTimestamp, | ||
"updatedAt": firestore.ServerTimestamp, | ||
}) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Get data | ||
doc, err := docRef.Get(ctx) | ||
if err != nil { | ||
if err != iterator.Done { | ||
http.Error(w, "Test not found", http.StatusInternalServerError) | ||
return | ||
} | ||
http.Error(w, "Failed to get test", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Map data | ||
if err := doc.DataTo(&test); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusCreated) | ||
json.NewEncoder(w).Encode(test) | ||
} | ||
|
||
// Manual test cases | ||
|
||
//curl -X POST http://localhost:8083/tests \ | ||
//-H "Content-Type: application/json" \ | ||
//-d '{ | ||
//"questionDocRefId": "sampleDocRefId123", | ||
//"questionTitle": "Sample Question Title", | ||
//"visibleTestCases": "2\nhello\nolleh\nHannah\nhannaH", | ||
//"hiddenTestCases": "2\nHannah\nhannaH\nabcdefg\ngfedcba" | ||
//}' | ||
|
||
//curl -X POST http://localhost:8083/tests \ | ||
//-H "Content-Type: application/json" \ | ||
//-d "{ | ||
//\"questionDocRefId\": \"sampleDocRefId12345\", | ||
//\"questionTitle\": \"Sample Question Title\", | ||
//\"visibleTestCases\": \"2\\nhello\\nolleh\\nHannah\\nhannaH\", | ||
//\"hiddenTestCases\": \"2\\nHannah\\nhannaH\\nabcdefg\\ngfedcba\" | ||
//}" |
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,40 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/go-chi/chi/v5" | ||
"google.golang.org/api/iterator" | ||
"net/http" | ||
) | ||
|
||
func (s *Service) DeleteTest(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
// Parse request | ||
docRefID := chi.URLParam(r, "questionDocRefId") | ||
|
||
docRef := s.Client.Collection("tests").Where("questionDocRefId", "==", docRefID).Limit(1).Documents(ctx) | ||
doc, err := docRef.Next() | ||
if err != nil { | ||
if err == iterator.Done { | ||
http.Error(w, "Test not found", http.StatusNotFound) | ||
return | ||
} | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
defer docRef.Stop() | ||
|
||
_, err = doc.Ref.Delete(ctx) | ||
if err != nil { | ||
http.Error(w, "Error deleting test", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
// Manual test cases | ||
|
||
//curl -X DELETE http://localhost:8083/tests/sampleDocRefId123 \ | ||
//-H "Content-Type: application/json" |
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,71 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"execution-service/models" | ||
"execution-service/utils" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
func (s *Service) ReadAllTests(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
questionDocRefId := chi.URLParam(r, "questionDocRefId") | ||
if questionDocRefId == "" { | ||
http.Error(w, "questionDocRefId is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
iter := s.Client.Collection("tests").Where("questionDocRefId", "==", questionDocRefId).Limit(1).Documents(ctx) | ||
doc, err := iter.Next() | ||
if err != nil { | ||
if err == iterator.Done { | ||
http.Error(w, "Test not found", http.StatusNotFound) | ||
return | ||
} | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
defer iter.Stop() | ||
|
||
var test models.Test | ||
if err := doc.DataTo(&test); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_, hiddenTestCases, err := utils.GetTestLengthAndUnexecutedCases(test.HiddenTestCases) | ||
|
||
var hiddenTests []models.HiddenTest | ||
for _, hiddenTestCase := range hiddenTestCases { | ||
hiddenTests = append(hiddenTests, models.HiddenTest{ | ||
Input: hiddenTestCase.Input, | ||
Expected: hiddenTestCase.Expected, | ||
}) | ||
} | ||
|
||
_, visibleTestCases, err := utils.GetTestLengthAndUnexecutedCases(test.VisibleTestCases) | ||
|
||
var visibleTests []models.VisibleTest | ||
for _, visibleTestCase := range visibleTestCases { | ||
visibleTests = append(visibleTests, models.VisibleTest{ | ||
Input: visibleTestCase.Input, | ||
Expected: visibleTestCase.Expected, | ||
}) | ||
} | ||
|
||
allTests := models.AllTests{ | ||
VisibleTests: visibleTests, | ||
HiddenTests: hiddenTests, | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(allTests) | ||
} | ||
|
||
//curl -X GET http://localhost:8083/tests/bmzFyLMeSOoYU99pi4yZ/ \ | ||
//-H "Content-Type: application/json" |
File renamed without changes.
Oops, something went wrong.