Skip to content

Commit 8f32d45

Browse files
prototype finished
1 parent 87a1e04 commit 8f32d45

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

backend/.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"go.testTimeout": "60s",
33
"cSpell.words": [
4+
"cdgr",
45
"cout",
56
"endl"
67
]

backend/internal/api/setup_router.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"github.com/markhuang1212/code-grader/backend/internal/util"
1010
)
1111

12+
type GradeResultResponse struct {
13+
Ready bool
14+
Result types.GradeResult
15+
}
16+
1217
func SetupRouter(cc *core.CoreController) *gin.Engine {
1318

1419
r := gin.Default()
@@ -17,7 +22,7 @@ func SetupRouter(cc *core.CoreController) *gin.Engine {
1722
c.String(http.StatusOK, "pong")
1823
})
1924

20-
authorized := r.Group("/")
25+
authorized := r.Group("/api/v1")
2126

2227
authorized.POST("/grade", func(c *gin.Context) {
2328

@@ -37,5 +42,20 @@ func SetupRouter(cc *core.CoreController) *gin.Engine {
3742

3843
})
3944

45+
authorized.GET("/result/:id", func(c *gin.Context) {
46+
id := c.Params.ByName("id")
47+
result, ok := cc.Cache.Get(id)
48+
if !ok {
49+
c.JSON(200, GradeResultResponse{
50+
Ready: false,
51+
})
52+
} else {
53+
c.JSON(200, GradeResultResponse{
54+
Ready: true,
55+
Result: result,
56+
})
57+
}
58+
})
59+
4060
return r
4161
}

backend/internal/api/setup_router_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api_test
33
import (
44
"net/http"
55
"net/http/httptest"
6+
"strings"
67
"testing"
78

89
"github.com/markhuang1212/code-grader/backend/internal/api"
@@ -20,3 +21,22 @@ func TestPingRoute(t *testing.T) {
2021
assert.Equal(t, http.StatusOK, w.Code)
2122
assert.Equal(t, "pong", w.Body.String())
2223
}
24+
25+
func TestGrading(t *testing.T) {
26+
router := api.SetupRouter(core.NewCoreController(1))
27+
28+
w := httptest.NewRecorder()
29+
30+
body := `
31+
{
32+
"TestCaseName": "example-1",
33+
"UserCode": "int main() { return 0; }"
34+
}
35+
`
36+
req, _ := http.NewRequest("POST", "/api/v1/grade", strings.NewReader(body))
37+
router.ServeHTTP(w, req)
38+
39+
assert.Equal(t, http.StatusAccepted, w.Code)
40+
loc := w.Header().Get("Location")
41+
assert.NotEmpty(t, loc)
42+
}

backend/internal/grader/grade_user_code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func GradeUserCode(ctx context.Context, gr types.GradeRequest) (*types.GradeResu
1414

1515
result := types.GradeResult{}
1616

17-
tmpDir, err := ioutil.TempDir("/tmp", "cdgr_")
17+
tmpDir, err := ioutil.TempDir("/tmp", "cdgr-")
1818
if err != nil {
1919
return nil, errors.Wrap(err, "cannot create tmpDir")
2020
}

0 commit comments

Comments
 (0)