Skip to content

Commit ae3746f

Browse files
d
1 parent 24d0a0e commit ae3746f

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

backend/cmd/compile_user_code.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package cmd
33
import (
44
"context"
55
"io"
6+
"strconv"
7+
68
//"log"
79
"path/filepath"
810

911
dockertypes "github.com/docker/docker/api/types"
1012
"github.com/docker/docker/api/types/container"
1113
"github.com/docker/docker/client"
12-
"github.com/docker/docker/pkg/stdcopy"
1314
"github.com/markhuang1212/code-grader/backend/types"
1415
"github.com/pkg/errors"
1516
)
@@ -37,33 +38,24 @@ func CompileUserCode(ctx context.Context, gr types.GradeRequest) ([]byte, error)
3738
"CXXFLAGS=-std=c++11",
3839
},
3940
OpenStdin: true,
40-
StdinOnce: true,
4141
}, &container.HostConfig{
4242
NetworkMode: "none",
43-
Resources: container.Resources{
44-
Memory: CompilationMemoryLimit,
43+
Resources: container.Resources{
44+
// Memory: CompilationMemoryLimit,
4545
},
4646
}, nil, nil, "")
4747

4848
if err != nil {
4949
return nil, errors.Wrap(ErrInternalError, "cannot create container")
5050
}
5151

52-
attachInput, err := cli.ContainerAttach(ctx, resp.ID, dockertypes.ContainerAttachOptions{
53-
Stdin: true,
54-
})
55-
56-
if err != nil {
57-
return nil, errors.Wrap(ErrInternalError, "cannot attach container input")
58-
}
59-
60-
attachOutput, err := cli.ContainerAttach(ctx, resp.ID, dockertypes.ContainerAttachOptions{
61-
Stdout: true,
62-
Stderr: true,
52+
hjresp, err := cli.ContainerAttach(ctx, resp.ID, dockertypes.ContainerAttachOptions{
53+
Stdin: true,
54+
Stream: true,
6355
})
6456

6557
if err != nil {
66-
return nil, errors.Wrap(ErrInternalError, "cannot attach container output")
58+
return nil, errors.Wrap(ErrInternalError, "cannot attach container")
6759
}
6860

6961
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNextExit)
@@ -83,30 +75,37 @@ func CompileUserCode(ctx context.Context, gr types.GradeRequest) ([]byte, error)
8375
// }
8476
}()
8577

86-
attachInput.Conn.Write([]byte(gr.UserCode))
87-
attachInput.Close()
78+
userCodeLength := len(gr.UserCode)
79+
hjresp.Conn.Write([]byte(strconv.Itoa(userCodeLength) + "\n"))
80+
hjresp.Conn.Write([]byte(gr.UserCode))
81+
hjresp.Close()
8882

89-
outR, outW := io.Pipe()
90-
errR, errW := io.Pipe()
91-
stdcopy.StdCopy(outW, errW, attachOutput.Conn)
92-
outW.Close()
93-
errW.Close()
94-
attachOutput.Conn.Close()
83+
if err != nil {
84+
return nil, errors.Wrap(ErrInternalError, "cannot close attached session (output)")
85+
}
9586

9687
select {
9788
case status := <-statusCh:
9889
switch status.StatusCode {
9990
case 0:
100-
result, err := io.ReadAll(outR)
91+
stdout, err := cli.ContainerLogs(ctx, resp.ID, dockertypes.ContainerLogsOptions{
92+
ShowStdout: true,
93+
ShowStderr: false,
94+
})
10195
if err != nil {
102-
return nil, errors.Wrap(ErrInternalError, "error reading outR")
96+
return nil, errors.Wrap(ErrInternalError, "error reading stdout")
10397
}
98+
result, _ := io.ReadAll(stdout)
10499
return result, nil
105100
case 1:
106-
result, err := io.ReadAll(errR)
101+
stderr, err := cli.ContainerLogs(ctx, resp.ID, dockertypes.ContainerLogsOptions{
102+
ShowStderr: true,
103+
ShowStdout: false,
104+
})
107105
if err != nil {
108-
return nil, errors.Wrap(ErrInternalError, "error reading errR")
106+
return nil, errors.Wrap(ErrInternalError, "error reading stdout")
109107
}
108+
result, _ := io.ReadAll(stderr)
110109
return result, ErrCompilationError
111110
case 2:
112111
return nil, ErrInternalError

backend/cmd/compile_user_code_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestCompileUserCode(t *testing.T) {
1414

1515
gr1 := types.GradeRequest{
1616
TestCaseName: "example-1",
17-
UserCode: "",
17+
UserCode: " ",
1818
}
1919

2020
out, err := cmd.CompileUserCode(ctx, gr1)

backend/cmd/setup_router.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ func SetupRouter() *gin.Engine {
1010

1111
r := gin.Default()
1212

13-
r.GET("/ping", func(c *gin.Context) {
13+
authorized := r.Group("/")
14+
15+
authorized.GET("/ping", func(c *gin.Context) {
1416
c.String(http.StatusOK, "pong")
1517
})
1618

17-
r.POST("/grade", func(c *gin.Context) {
19+
authorized.POST("/grade", func(c *gin.Context) {
1820

1921
})
2022

runtime-compile/run.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
# 1: Compilation Error
66
# 2: Internal Error
77

8-
tee > code.txt
8+
read LENGTH
9+
10+
dd bs=$LENGTH count=1 of=code.txt > /dev/null
911

1012
if [ -z "${TEST_CASE_DIR}" ]
1113
then
@@ -29,11 +31,11 @@ cat ${TEST_CASE_DIR}/prepend.txt > main.cpp
2931
cat code.txt >> main.cpp
3032
cat ${TEST_CASE_DIR}/append.txt >> main.cpp
3133

32-
${CXX} ${CXXFLAGS} -o /tmp/a.out main.cpp
34+
${CXX} ${CXXFLAGS} -o a.out main.cpp
3335

3436
if [[ $? -ne 0 ]]
3537
then
3638
exit 1
3739
fi
3840

39-
cat /tmp/a.out
41+
cat a.out

0 commit comments

Comments
 (0)