-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (125 loc) · 3.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"fmt"
"io"
"os"
"bufio"
"log"
"encoding/json"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
)
type CodeData struct {
Code string `json:"Code"`
}
func main() {
address := "localhost:8003"
fs := http.FileServer(http.Dir("./"))
http.HandleFunc("/runcode", createContainer)
http.Handle("/", fs)
log.Println("listen on", address)
log.Fatal(http.ListenAndServe(address, nil))
}
func createContainer(w http.ResponseWriter, r *http.Request){
var data CodeData
// 读取请求体
requestBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
// 解析 JSON 数据到结构体中
if err := json.Unmarshal(requestBody, &data); err != nil {
log.Fatal(err)
}
ex, err := os.Executable()
if err != nil {
panic(err)
}
// Get the absolute path of the executable file
exPath := filepath.Dir(ex)
// Get the absolute path of the project directory
projectPath := filepath.Dir(exPath)
// 打印结构体中的数据
fmt.Printf("Code: %s\n", data.Code)
os.MkdirAll(projectPath+"\\usercode",0777)
userCodeFilepath := projectPath+"\\usercode\\main.go"
log.Println("usercodefilepath: ",userCodeFilepath)
f,err := os.OpenFile(userCodeFilepath,os.O_CREATE | os.O_RDWR,0644)
if err != nil {
log.Fatal(err)
}
f.WriteString(data.Code)
f.Close()
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
defer cli.Close()
reader, err := cli.ImagePull(ctx, "docker.io/library/golang", types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, reader)
mountsSource := projectPath+"\\usercode"
log.Println("mountsSource: ",mountsSource)
// Mount the code directory as a volume
mounts := []mount.Mount{
{
Type: mount.TypeBind,
Source: mountsSource,
Target: "/code",
},
}
hostConfig := &container.HostConfig{
Mounts: mounts,
// AutoRemove: true,
}
config := &container.Config{
Image: "golang:latest",
Cmd: []string{"go", "run", "/code/main.go"},
Tty: true,
AttachStdout: true,
}
resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true,ShowStderr: true,Follow: true})
if err != nil {
panic(err)
}
defer out.Close()
log.Println("获取运行结果")
// stdcopy.StdCopy(os.Stdout, os.Stderr, out)
scanner := bufio.NewScanner(out)
result :=""
for scanner.Scan() {
line := scanner.Text()
result += line +"<br/>"
}
if err := cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true, RemoveLinks: false}); err != nil {
panic(err)
}
if _, err := fmt.Fprint(w, result); err != nil {
panic(err)
}
log.Println("执行结束")
}