Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
tianyazhichiC
algorithm-visualizer
提交
5d40c97d
A
algorithm-visualizer
项目概览
tianyazhichiC
/
algorithm-visualizer
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm-visualizer
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5d40c97d
编写于
7月 16, 2018
作者:
J
Jason Park
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Compile and run java code in docker
上级
396ef5f3
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
96 addition
and
21 deletion
+96
-21
docker/images/java/Dockerfile
docker/images/java/Dockerfile
+0
-1
package.json
package.json
+1
-0
src/backend/apis/index.js
src/backend/apis/index.js
+12
-10
src/backend/common/error.js
src/backend/common/error.js
+11
-0
src/backend/controllers/categories.js
src/backend/controllers/categories.js
+1
-1
src/backend/controllers/index.js
src/backend/controllers/index.js
+1
-1
src/backend/controllers/tracers.js
src/backend/controllers/tracers.js
+56
-0
src/frontend/apis/index.js
src/frontend/apis/index.js
+5
-4
src/frontend/components/ToastContainer/stylesheet.scss
src/frontend/components/ToastContainer/stylesheet.scss
+1
-0
src/frontend/core/tracerManager.jsx
src/frontend/core/tracerManager.jsx
+8
-4
未找到文件。
docker/images/java/Dockerfile
已删除
100644 → 0
浏览文件 @
396ef5f3
FROM
baekjoon/onlinejudge-java:1.8
\ No newline at end of file
package.json
浏览文件 @
5d40c97d
...
...
@@ -3,6 +3,7 @@
"version"
:
"2.0.0"
,
"description"
:
"Algorithm Visualizer"
,
"scripts"
:
{
"postinstall"
:
"docker pull baekjoon/onlinejudge-java:1.8"
,
"dev"
:
"NODE_ENV=development node bin/www"
,
"start"
:
"NODE_ENV=production node bin/www"
,
"build"
:
"npm run build:frontend && npm run build:backend"
,
...
...
src/backend/apis/index.js
浏览文件 @
5d40c97d
import
Promise
from
'
bluebird
'
;
import
axios
from
'
axios
'
;
import
fs
from
'
fs
-extra
'
;
import
fs
from
'
fs
'
;
import
{
githubClientId
,
githubClientSecret
}
from
'
/environment
'
;
axios
.
interceptors
.
request
.
use
(
request
=>
{
const
instance
=
axios
.
create
();
instance
.
interceptors
.
request
.
use
(
request
=>
{
request
.
params
=
{
client_id
:
githubClientId
,
client_secret
:
githubClientSecret
,
...
request
.
params
};
return
request
;
});
axios
.
interceptors
.
response
.
use
(
response
=>
{
instance
.
interceptors
.
response
.
use
(
response
=>
{
return
response
.
data
;
},
error
=>
{
return
Promise
.
reject
(
error
.
response
.
data
);
...
...
@@ -28,43 +30,43 @@ const request = (url, process) => {
const
GET
=
URL
=>
{
return
request
(
URL
,
(
mappedURL
,
args
)
=>
{
const
[
params
]
=
args
;
return
axios
.
get
(
mappedURL
,
{
params
});
return
instance
.
get
(
mappedURL
,
{
params
});
});
};
const
DELETE
=
URL
=>
{
return
request
(
URL
,
(
mappedURL
,
args
)
=>
{
const
[
params
]
=
args
;
return
axios
.
delete
(
mappedURL
,
{
params
});
return
instance
.
delete
(
mappedURL
,
{
params
});
});
};
const
POST
=
URL
=>
{
return
request
(
URL
,
(
mappedURL
,
args
)
=>
{
const
[
body
,
params
]
=
args
;
return
axios
.
post
(
mappedURL
,
body
,
{
params
});
return
instance
.
post
(
mappedURL
,
body
,
{
params
});
});
};
const
PUT
=
URL
=>
{
return
request
(
URL
,
(
mappedURL
,
args
)
=>
{
const
[
body
,
params
]
=
args
;
return
axios
.
put
(
mappedURL
,
body
,
{
params
});
return
instance
.
put
(
mappedURL
,
body
,
{
params
});
});
};
const
PATCH
=
URL
=>
{
return
request
(
URL
,
(
mappedURL
,
args
)
=>
{
const
[
body
,
params
]
=
args
;
return
axios
.
patch
(
mappedURL
,
body
,
{
params
});
return
instance
.
patch
(
mappedURL
,
body
,
{
params
});
});
};
const
GitHubApi
=
{
listCommits
:
GET
(
'
/repos/:owner/:repo/commits
'
),
getAccessToken
:
code
=>
axios
.
post
(
'
https://github.com/login/oauth/access_token
'
,
{
code
},
{
headers
:
{
Accept
:
'
application/json
'
}
}),
getAccessToken
:
code
=>
instance
.
post
(
'
https://github.com/login/oauth/access_token
'
,
{
code
},
{
headers
:
{
Accept
:
'
application/json
'
}
}),
getLatestRelease
:
GET
(
'
/repos/:owner/:repo/releases/latest
'
),
download
:
(
url
,
path
)
=>
axios
({
download
:
(
url
,
path
)
=>
instance
({
method
:
'
get
'
,
url
,
responseType
:
'
stream
'
,
...
...
src/backend/common/error.js
浏览文件 @
5d40c97d
class
ApplicationError
extends
Error
{
toJSON
()
{
return
{
message
:
this
.
message
};
}
}
class
NotFoundError
extends
ApplicationError
{
...
...
@@ -10,9 +13,17 @@ class PermissionError extends ApplicationError {
class
AuthorizationError
extends
ApplicationError
{
}
class
CompileError
extends
ApplicationError
{
}
class
RuntimeError
extends
ApplicationError
{
}
export
{
ApplicationError
,
NotFoundError
,
PermissionError
,
AuthorizationError
,
CompileError
,
RuntimeError
,
};
\ No newline at end of file
src/backend/controllers/categories.js
浏览文件 @
5d40c97d
import
express
from
'
express
'
;
import
fs
from
'
fs
-extra
'
;
import
fs
from
'
fs
'
;
import
path
from
'
path
'
;
import
{
NotFoundError
}
from
'
/common/error
'
;
import
{
exec
}
from
'
child_process
'
;
...
...
src/backend/controllers/index.js
浏览文件 @
5d40c97d
export
{
default
as
auth
}
from
'
./auth
'
;
export
{
default
as
categories
}
from
'
./categories
'
;
export
{
default
as
compilers
}
from
'
./compil
ers
'
;
export
{
default
as
tracers
}
from
'
./trac
ers
'
;
src/backend/controllers/
compil
ers.js
→
src/backend/controllers/
trac
ers.js
浏览文件 @
5d40c97d
...
...
@@ -3,42 +3,54 @@ import express from 'express';
import
fs
from
'
fs-extra
'
;
import
uuid
from
'
uuid
'
;
import
path
from
'
path
'
;
import
child_process
from
'
child_process
'
;
import
{
GitHubApi
}
from
'
/apis
'
;
import
{
__PROD__
}
from
'
/environment
'
;
import
{
CompileError
,
RuntimeError
}
from
'
/common/error
'
;
const
router
=
express
.
Router
();
const
getLibPath
=
(...
args
)
=>
path
.
resolve
(
__dirname
,
'
..
'
,
'
public
'
,
'
libs
'
,
...
args
);
const
createTempDir
=
()
=>
{
const
dirPath
=
path
.
resolve
(
__dirname
,
'
..
'
,
'
public
'
,
'
codes
'
,
uuid
.
v4
());
fs
.
mkdirSync
(
dirPath
);
return
dirPath
;
};
const
downloadLibs
=
()
=>
{
GitHubApi
.
getLatestRelease
(
'
algorithm-visualizer
'
,
'
tracers
'
).
then
(
release
=>
{
return
Promise
.
each
(
release
.
assets
,
asset
=>
GitHubApi
.
download
(
asset
.
browser_download_url
,
getLibPath
(
asset
.
name
)));
});
};
downloadLibs
();
// TODO: download again when webhooked
if
(
__PROD__
)
downloadLibs
();
// TODO: download again when webhooked
const
getJsWorker
=
(
req
,
res
,
next
)
=>
{
res
.
sendFile
(
getLibPath
(
'
js.js
'
));
};
const
compileJava
=
(
req
,
res
,
next
)
=>
{
const
dirPath
=
createTempDir
();
fs
.
writeFileSync
(
dirPath
,
req
.
body
);
/* TODO:
1. Write into a source file
2. Execute in Docker
3. Read the output file
*/
const
execute
=
(
imageId
,
srcPath
,
command
,
ErrorClass
)
=>
new
Promise
((
resolve
,
reject
)
=>
{
const
libPath
=
getLibPath
();
const
dockerCommand
=
`docker run --rm -w=/usr/judge -t -v=
${
libPath
}
:/usr/bin/tracers:ro -v=
${
srcPath
}
:/usr/judge:rw -e MAX_TRACES=1000000 -e MAX_TRACERS=100
${
imageId
}
`
;
child_process
.
exec
(
`
${
dockerCommand
}
${
command
}
`
,
(
error
,
stdout
,
stderr
)
=>
{
if
(
error
)
return
reject
(
new
ErrorClass
(
stdout
));
resolve
();
});
});
const
trace
=
({
imageId
,
compileCommand
,
runCommand
})
=>
(
req
,
res
,
next
)
=>
{
const
{
code
}
=
req
.
body
;
const
srcPath
=
path
.
resolve
(
__dirname
,
'
..
'
,
'
public
'
,
'
codes
'
,
uuid
.
v4
());
fs
.
outputFile
(
path
.
resolve
(
srcPath
,
'
code.java
'
),
code
)
.
then
(()
=>
execute
(
imageId
,
srcPath
,
compileCommand
,
CompileError
))
.
then
(()
=>
execute
(
imageId
,
srcPath
,
runCommand
,
RuntimeError
))
.
then
(()
=>
res
.
sendFile
(
path
.
resolve
(
srcPath
,
'
traces.json
'
)))
.
catch
(
next
)
.
finally
(()
=>
fs
.
remove
(
srcPath
));
};
router
.
route
(
'
/js
'
)
.
get
(
getJsWorker
);
router
.
route
(
'
/java
'
)
.
post
(
compileJava
);
.
post
(
trace
({
imageId
:
'
baekjoon/onlinejudge-java:1.8
'
,
compileCommand
:
'
javac -cp /usr/bin/tracers/java.jar code.java
'
,
runCommand
:
'
java -cp /usr/bin/tracers/java.jar:. Main
'
,
}));
export
default
router
;
\ No newline at end of file
src/frontend/apis/index.js
浏览文件 @
5d40c97d
...
...
@@ -69,18 +69,19 @@ const GitHubApi = {
};
let
jsWorker
=
null
;
const
Compil
erApi
=
{
js
:
code
=>
new
Promise
((
resolve
,
reject
)
=>
{
const
Trac
erApi
=
{
js
:
({
code
})
=>
new
Promise
((
resolve
,
reject
)
=>
{
if
(
jsWorker
)
jsWorker
.
terminate
();
jsWorker
=
new
Worker
(
'
/api/
compil
ers/js
'
);
jsWorker
=
new
Worker
(
'
/api/
trac
ers/js
'
);
jsWorker
.
onmessage
=
e
=>
resolve
(
e
.
data
);
jsWorker
.
onerror
=
reject
;
jsWorker
.
postMessage
(
code
);
}),
java
:
POST
(
'
/tracers/java
'
),
};
export
{
CategoryApi
,
GitHubApi
,
Compil
erApi
,
Trac
erApi
,
};
\ No newline at end of file
src/frontend/components/ToastContainer/stylesheet.scss
浏览文件 @
5d40c97d
...
...
@@ -13,6 +13,7 @@
padding
:
16px
;
margin
:
8px
;
font-size
:
$font-size-large
;
white-space
:
pre-line
;
&
.success
{
border-color
:
rgb
(
0
,
150
,
0
);
...
...
src/frontend/core/tracerManager.jsx
浏览文件 @
5d40c97d
...
...
@@ -3,7 +3,7 @@ import Promise from 'bluebird';
import
{
extension
}
from
'
/common/util
'
;
import
{
Array1DData
,
Array2DData
,
ChartData
,
Data
,
GraphData
,
LogData
}
from
'
/core/datas
'
;
import
{
Array1DRenderer
,
Array2DRenderer
,
ChartRenderer
,
GraphRenderer
,
LogRenderer
,
Renderer
}
from
'
/core/renderers
'
;
import
{
Compil
erApi
}
from
'
/apis
'
;
import
{
Trac
erApi
}
from
'
/apis
'
;
class
TracerManager
{
constructor
()
{
...
...
@@ -63,7 +63,7 @@ class TracerManager {
setFile
(
file
)
{
this
.
file
=
file
;
this
.
runInitial
();
if
(
extension
(
file
.
name
)
===
'
js
'
)
this
.
runInitial
();
}
reset
(
traces
=
[])
{
...
...
@@ -155,8 +155,12 @@ class TracerManager {
execute
()
{
const
{
name
,
content
}
=
this
.
file
;
const
ext
=
extension
(
name
);
if
(
ext
in
CompilerApi
)
{
return
CompilerApi
[
ext
](
content
).
then
(
traces
=>
this
.
reset
(
traces
));
if
(
ext
in
TracerApi
)
{
return
TracerApi
[
ext
]({
code
:
content
})
.
then
(
traces
=>
this
.
reset
(
traces
))
.
catch
(
e
=>
{
throw
e
.
err
;
});
}
else
{
return
Promise
.
reject
(
new
Error
(
'
Language Not Supported
'
));
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录