这是OJ的后端,主要就是负责操作数据库,然后组织数据给前端。后端API说明
后端框架: Express
数据库: MongoDB
ODM: Mongoose
Socket:Socket.IO
当收到题目提交的时候会发送消息给JudgeEmitter程序,同时会先给前端返回提交成功的信息,JudgeEmitter程序会启动docker运行我的oj-judge镜像去执行用户的提交,当收到结果后会发送消息给后端,这时后端会通过Socket给前端发送提交的结果信息。
整个项目只能在MacOS或Linux平台上部署,因为要实现在Docker容器内部创建兄弟容器,得把本地的/var/run/docker.sock
挂载到容器中,详情请看这篇文章,Windows中不支持。
-
在Docker中运行(推荐)
由于依赖Docker以及MongoDB,所以推荐直接使用Docker镜像来部署
项目中包含Dockerfile,可以通过Docker build 来构建本地镜像,不过更推荐你直接通过
Docker pull 313183373/oj-backend
直接从Docker Hub中拉取已经建立好的镜像,当然还需要一些别的Image来配合使用,可以都先pull下来,分别是docker pull 313183373/oj-db
数据库docker pull 313183373/oj-judge
判题程序docker pull 313183373/oj-frontend
前端然后在本目录下(存在docker-compose.yml文件)使用
docker-compose up
来启动整个项目,然后在浏览器中访问localhost:3030即可 -
本地运行
本地运行需要自行配置好环境,这是我本地测试过的版本,只要版本差别不大应该也不会出现什么问题
nodejs v11.10.0
mongodb v3.6.2
Docker 18.09.2
直接使用
node app.js
启动后端程序,当然先需要启动mongo数据库,然后提前拉取313183373/oj-judge的镜像(docker pull 313183373/oj-judge
)以便后端随时启用它启动数据库以及后端之后,你就可以对后端的API做一些测试了。
然后克隆oj-frontend仓库
git clone https://github.com/313183373/oj-frontend.git
在oj-frontend仓库目录下,运行
npm install
安装依赖。待依赖安装完成后,可以通过
npm start
来启动React项目,当然这是测试环境生产环境需要运行
npm run build
生成build文件夹,然后通过nginx
服务器部署静态文件夹,同时需要配置反向代理,这里有一份示例配置## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # http://wiki.nginx.org/Pitfalls # http://wiki.nginx.org/QuickStart # http://wiki.nginx.org/Configuration # # Generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. Or just disable in sites-enabled. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; gzip on; gzip_disable "msie6"; gzip_comp_level 6; gzip_min_length 1100; gzip_buffers 16 8k; gzip_proxied any; gzip_types text/plain text/css text/js text/xml text/javascript application/javascript application/x-javascript application/json application/xml application/rss+xml image/svg+xml; root /var/lib/jenkins/build; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; try_files $uri /index.html; } location /api/ { proxy_pass http://localhost:5000/; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
上面的root指的是build文件夹的位置,你可以自己指定,location /api/那一部分是反向代理的配置,后端在5000端口上。
这些都配置好了之后,就可以在浏览器中打开localhost看到页面。