Skip to content

Commit aeaf47c

Browse files
add docs
1 parent 8f32d45 commit aeaf47c

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@ This is a backend for grading C++ code in a secure and controlled manner.
55
## Usage
66

77
```shell
8-
> docker build . -t code-grader
9-
> docker run -p 8080 --privileged code-grader
8+
> docker-compose build
9+
> docker-compose up
1010
```
1111

1212
## API
1313

1414
### Grade Code Request
1515

16+
```bash
17+
curl --location --request POST 'http://localhost:8080/api/v1/grade' \
18+
--header 'Content-Type: application/json' \
19+
--data-raw '{
20+
"TestCaseName": "example-1",
21+
"UserCode": "int main() { cout << \"Hello\"; }"
22+
}'
23+
```
24+
1625
### Retrieve Grade Result
1726

27+
```bash
28+
curl --location \
29+
--request GET 'http://localhost:8080/api/v1/result/cbaf98c458f3878df6f5'
30+
```
31+
1832
## Author
1933

2034
Huang Meng \

backend/internal/api/setup_router.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func SetupRouter(cc *core.CoreController) *gin.Engine {
2222
c.String(http.StatusOK, "pong")
2323
})
2424

25+
r.GET("/api", func(c *gin.Context) {
26+
c.Redirect(http.StatusTemporaryRedirect, "https://app.swaggerhub.com/apis-docs/markhuang1212/CodeGraderCore")
27+
})
28+
2529
authorized := r.Group("/api/v1")
2630

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

backend/internal/core/cache.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ package core
22

33
import (
44
"sync"
5+
"time"
56

67
"github.com/markhuang1212/code-grader/backend/internal/types"
78
)
89

910
type GradeResultCache struct {
10-
Data map[string]types.GradeResult
11-
Lock sync.RWMutex
11+
Data map[string]types.GradeResult
12+
Lock sync.RWMutex
13+
Timeout time.Duration
1214
}
1315

1416
func NewGradeResultCache() *GradeResultCache {
1517
ret := GradeResultCache{
16-
Data: make(map[string]types.GradeResult),
18+
Data: make(map[string]types.GradeResult),
19+
Timeout: 12 * time.Minute,
1720
}
1821

1922
return &ret
@@ -23,6 +26,10 @@ func (c *GradeResultCache) Add(key string, val types.GradeResult) {
2326
c.Lock.Lock()
2427
defer c.Lock.Unlock()
2528
c.Data[key] = val
29+
go func() {
30+
time.Sleep(c.Timeout)
31+
c.Del(key)
32+
}()
2633
}
2734

2835
func (c *GradeResultCache) Del(key string) {

backend/internal/core/cache_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core_test
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/markhuang1212/code-grader/backend/internal/core"
78
"github.com/markhuang1212/code-grader/backend/internal/types"
@@ -10,11 +11,15 @@ import (
1011

1112
func TestCache(t *testing.T) {
1213
c := core.NewGradeResultCache()
14+
c.Timeout = 2 * time.Second
1315
c.Add("id1", types.GradeResult{
1416
Status: types.GradeResultCompilationError,
1517
Msg: "Hello",
1618
})
1719
ret, ok := c.Get("id1")
1820
assert.True(t, ok)
1921
assert.Equal(t, "Hello", ret.Msg)
22+
time.Sleep(3 * time.Second)
23+
ret, ok = c.Get("id1")
24+
assert.False(t, ok)
2025
}

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ services:
1010
- "8443:8443"
1111
volumes:
1212
- /var/run/docker.sock:/var/run/docker.sock
13+
- /tmp:/tmp
1314
deploy:
1415
resources:
1516
limits:
1617
memory: 4G
17-
cpus: '1.0'
18+
cpus: '2.0'
1819
restart_policy:
1920
condition: on-failure
20-
delay: 5s
2121
max_attempts: 3
22-
window: 120s
2322
runtime-compile:
2423
build:
2524
context: .

0 commit comments

Comments
 (0)