├── .env ├── .env.development ├── src ├── index.ts ├── config │ ├── constants.ts │ ├── paths.ts │ └── environments.ts ├── tracers │ ├── index.ts │ ├── cpp │ │ ├── CppTracer.ts │ │ └── Dockerfile │ ├── java │ │ └── JavaTracer.ts │ ├── js │ │ ├── worker.js │ │ └── JsTracer.ts │ ├── Tracer.ts │ ├── LambdaTracer.ts │ └── DockerTracer.ts ├── models │ ├── index.ts │ ├── File.ts │ ├── Category.ts │ ├── Algorithm.ts │ └── Hierarchy.ts ├── middlewares │ ├── index.ts │ ├── errorHandlerMiddleware.ts │ ├── redirectMiddleware.ts │ └── frontendMiddleware.ts ├── controllers │ ├── index.ts │ ├── Controller.ts │ ├── TracersController.ts │ ├── AuthController.ts │ ├── AlgorithmsController.ts │ └── VisualizationsController.ts ├── utils │ ├── hierarchy.ts │ ├── apis.ts │ └── misc.ts └── Server.ts ├── pm2.json ├── .gitignore ├── .env.production ├── certbot.ini ├── README.md ├── tsconfig.json ├── tslint.json ├── package.json └── CONTRIBUTING.md /.env: -------------------------------------------------------------------------------- 1 | HTTP_PORT = 8080 2 | HTTPS_PORT = 8443 3 | -------------------------------------------------------------------------------- /.env.development: -------------------------------------------------------------------------------- 1 | CREDENTIALS_ENABLED = 0 2 | WEBHOOK_ENABLED = 0 3 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import Server from 'Server'; 2 | 3 | new Server().start(); 4 | -------------------------------------------------------------------------------- /src/config/constants.ts: -------------------------------------------------------------------------------- 1 | export const memoryLimit = 256; // in megabytes 2 | export const timeLimit = 5000; // in milliseconds 3 | -------------------------------------------------------------------------------- /pm2.json: -------------------------------------------------------------------------------- 1 | { 2 | "apps": [ 3 | { 4 | "name": "algorithm-visualizer", 5 | "script": "npm start" 6 | } 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /src/tracers/index.ts: -------------------------------------------------------------------------------- 1 | export { CppTracer } from './cpp/CppTracer'; 2 | export { JavaTracer } from './java/JavaTracer'; 3 | export { JsTracer } from './js/JsTracer'; 4 | -------------------------------------------------------------------------------- /src/models/index.ts: -------------------------------------------------------------------------------- 1 | export { Algorithm } from './Algorithm'; 2 | export { Category } from './Category'; 3 | export { File } from './File'; 4 | export { Hierarchy } from './Hierarchy'; 5 | -------------------------------------------------------------------------------- /src/tracers/cpp/CppTracer.ts: -------------------------------------------------------------------------------- 1 | import { DockerTracer } from 'tracers/DockerTracer'; 2 | 3 | export class CppTracer extends DockerTracer { 4 | constructor() { 5 | super('cpp'); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/tracers/java/JavaTracer.ts: -------------------------------------------------------------------------------- 1 | import { LambdaTracer } from 'tracers/LambdaTracer'; 2 | 3 | export class JavaTracer extends LambdaTracer { 4 | constructor() { 5 | super('java'); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/middlewares/index.ts: -------------------------------------------------------------------------------- 1 | export { errorHandlerMiddleware } from './errorHandlerMiddleware'; 2 | export { frontendMiddleware } from './frontendMiddleware'; 3 | export { redirectMiddleware } from './redirectMiddleware'; 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # WebStorm settings 2 | /.idea 3 | 4 | # npm 5 | /node_modules 6 | /npm-debug.log 7 | 8 | # macOS 9 | .DS_Store 10 | 11 | # local .env* files 12 | .env.local 13 | .env.*.local 14 | 15 | # downloaded files 16 | /public 17 | -------------------------------------------------------------------------------- /.env.production: -------------------------------------------------------------------------------- 1 | CREDENTIALS_ENABLED = 1 2 | CREDENTIALS_PATH = /home/ubuntu/.certbot/config/live/algorithm-visualizer.org 3 | CREDENTIALS_CA = fullchain.pem 4 | CREDENTIALS_KEY = privkey.pem 5 | CREDENTIALS_CERT = cert.pem 6 | WEBHOOK_ENABLED = 1 7 | -------------------------------------------------------------------------------- /src/controllers/index.ts: -------------------------------------------------------------------------------- 1 | export { AlgorithmsController } from './AlgorithmsController'; 2 | export { AuthController } from './AuthController'; 3 | export { TracersController } from './TracersController'; 4 | export { VisualizationsController } from './VisualizationsController'; 5 | -------------------------------------------------------------------------------- /certbot.ini: -------------------------------------------------------------------------------- 1 | config-dir = /home/ubuntu/.certbot/config 2 | work-dir = /home/ubuntu/.certbot/work 3 | logs-dir = /home/ubuntu/.certbot/logs 4 | email = parkjs814@gmail.com 5 | authenticator = webroot 6 | webroot-path = /home/ubuntu/server/public/frontend-built 7 | domains = algorithm-visualizer.org 8 | -------------------------------------------------------------------------------- /src/controllers/Controller.ts: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import Server from 'Server'; 3 | 4 | export abstract class Controller { 5 | protected readonly router: express.Router; 6 | 7 | protected constructor(protected server: Server) { 8 | this.router = express.Router(); 9 | } 10 | 11 | abstract route(router: express.Router): void; 12 | } 13 | -------------------------------------------------------------------------------- /src/controllers/TracersController.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import { Controller } from 'controllers/Controller'; 3 | import Server from 'Server'; 4 | 5 | export class TracersController extends Controller { 6 | constructor(server: Server) { 7 | super(server); 8 | this.server.tracers.forEach(tracer => tracer.route(this.router)); 9 | } 10 | 11 | route = (router: express.Router): void => { 12 | router.use('/tracers', this.router); 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Server 2 | 3 | > This repository is part of the project [Algorithm Visualizer](https://github.com/algorithm-visualizer). 4 | 5 | `server` serves [`algorithm-visualizer`](https://github.com/algorithm-visualizer/algorithm-visualizer) and provides APIs that the web app needs on the fly. (e.g., GitHub sign in, compiling/running code, etc.) 6 | 7 | ## Contributing 8 | 9 | Check out the [contributing guidelines](https://github.com/algorithm-visualizer/server/blob/master/CONTRIBUTING.md). 10 | -------------------------------------------------------------------------------- /src/tracers/js/worker.js: -------------------------------------------------------------------------------- 1 | const process = { env: { ALGORITHM_VISUALIZER: '1' } }; 2 | importScripts('/api/tracers/js'); 3 | 4 | const sandbox = code => { 5 | const require = name => ({ 'algorithm-visualizer': AlgorithmVisualizer }[name]); // fake require 6 | eval(code); 7 | }; 8 | 9 | onmessage = e => { 10 | const lines = e.data.split('\n').map((line, i) => line.replace(/(\.\s*delay\s*)\(\s*\)/g, `$1(${i})`)); 11 | const code = lines.join('\n'); 12 | sandbox(code); 13 | postMessage(AlgorithmVisualizer.Commander.commands); 14 | }; 15 | -------------------------------------------------------------------------------- /src/middlewares/errorHandlerMiddleware.ts: -------------------------------------------------------------------------------- 1 | import { NextFunction, Request, Response } from 'express'; 2 | import { Exception, InternalServerError } from 'ts-httpexceptions'; 3 | 4 | export function errorHandlerMiddleware() { 5 | return (err: any, req: Request, res: Response, next: NextFunction) => { 6 | if (!(err instanceof Exception)) { 7 | console.error(err); 8 | err = new InternalServerError(err.message, err); 9 | } 10 | 11 | const {message, status} = err; 12 | res.status(status).send(message); 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2015", 4 | "lib": [ 5 | "es2015", 6 | "esnext" 7 | ], 8 | "module": "commonjs", 9 | "moduleResolution": "node", 10 | "esModuleInterop": true, 11 | "experimentalDecorators": true, 12 | "emitDecoratorMetadata": true, 13 | "sourceMap": true, 14 | "declaration": false, 15 | "strict": true, 16 | "baseUrl": "src", 17 | "resolveJsonModule": true 18 | }, 19 | "exclude": [ 20 | "node_modules", 21 | "public" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /src/middlewares/redirectMiddleware.ts: -------------------------------------------------------------------------------- 1 | import { NextFunction, Request, Response } from 'express'; 2 | import { credentials } from 'config/environments'; 3 | 4 | export function redirectMiddleware() { 5 | return (req: Request, res: Response, next: NextFunction) => { 6 | if (req.hostname === 'algo-visualizer.jasonpark.me') { 7 | res.redirect(301, 'https://algorithm-visualizer.org/'); 8 | } else if (credentials && !req.secure) { 9 | res.redirect(301, `https://${req.hostname}${req.url}`); 10 | } else { 11 | next(); 12 | } 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /src/models/File.ts: -------------------------------------------------------------------------------- 1 | import fs from 'fs-extra'; 2 | 3 | export type Author = { 4 | login: string, 5 | avatar_url: string, 6 | } 7 | 8 | export class File { 9 | content!: string; 10 | contributors!: Author[]; 11 | 12 | constructor(public path: string, public name: string) { 13 | this.refresh(); 14 | } 15 | 16 | refresh() { 17 | this.content = fs.readFileSync(this.path, 'utf-8'); 18 | this.contributors = []; 19 | } 20 | 21 | toJSON() { 22 | const {name, content, contributors} = this; 23 | return {name, content, contributors}; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/config/paths.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | 3 | export const rootDir = path.resolve(__dirname, '..', '..'); 4 | export const publicDir = path.resolve(rootDir, 'public'); 5 | export const algorithmsDir = path.resolve(publicDir, 'algorithms'); 6 | export const codesDir = path.resolve(publicDir, 'codes'); 7 | export const visualizationsDir = path.resolve(publicDir, 'visualizations'); 8 | export const frontendDir = path.resolve(publicDir, 'frontend'); 9 | export const frontendBuildDir = path.resolve(frontendDir, 'build'); 10 | export const frontendBuiltDir = path.resolve(publicDir, 'frontend-built'); 11 | -------------------------------------------------------------------------------- /src/utils/hierarchy.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import fs from 'fs-extra'; 3 | 4 | export function createKey(name: string) { 5 | return name.toLowerCase().trim().replace(/[^\w \-]/g, '').replace(/ /g, '-'); 6 | } 7 | 8 | export function isDirectory(dirPath: string) { 9 | return fs.lstatSync(dirPath).isDirectory(); 10 | } 11 | 12 | export function listFiles(dirPath: string) { 13 | return fs.pathExistsSync(dirPath) ? fs.readdirSync(dirPath).filter(fileName => !fileName.startsWith('.')) : []; 14 | } 15 | 16 | export function listDirectories(dirPath: string) { 17 | return listFiles(dirPath).filter(fileName => isDirectory(path.resolve(dirPath, fileName))); 18 | } 19 | -------------------------------------------------------------------------------- /src/tracers/Tracer.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import { GitHubApi } from 'utils/apis'; 3 | 4 | export type Release = { 5 | tag_name: string; 6 | } 7 | 8 | export abstract class Tracer { 9 | protected constructor(public lang: string) { 10 | this.update().catch(console.error); 11 | } 12 | 13 | abstract build(release: Release): Promise; 14 | 15 | abstract route(router: express.Router): void; 16 | 17 | async update(release?: Release) { 18 | if (release) { 19 | return this.build(release); 20 | } 21 | const {data} = await GitHubApi.getLatestRelease('algorithm-visualizer', `tracers.${this.lang}`); 22 | return this.build(data); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/tracers/cpp/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM rikorose/gcc-cmake 2 | 3 | ARG tag_name 4 | 5 | RUN curl --create-dirs -o /usr/local/include/nlohmann/json.hpp -L "https://github.com/nlohmann/json/releases/download/v3.1.2/json.hpp" \ 6 | && curl --create-dirs -o /usr/tmp/algorithm-visualizer.tar.gz -L "https://github.com/algorithm-visualizer/tracers.cpp/archive/${tag_name}.tar.gz" \ 7 | && cd /usr/tmp \ 8 | && mkdir algorithm-visualizer \ 9 | && tar xvzf algorithm-visualizer.tar.gz -C algorithm-visualizer --strip-components=1 \ 10 | && cd /usr/tmp/algorithm-visualizer \ 11 | && mkdir build \ 12 | && cd build \ 13 | && cmake .. \ 14 | && make install 15 | 16 | CMD g++ Main.cpp -o Main -O2 -std=c++11 -lcurl \ 17 | && ./Main 18 | -------------------------------------------------------------------------------- /src/models/Category.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import { createKey, listDirectories } from 'utils/hierarchy'; 3 | import { Algorithm } from 'models'; 4 | 5 | export class Category { 6 | key: string; 7 | algorithms!: Algorithm[]; 8 | 9 | constructor(private path: string, public name: string) { 10 | this.key = createKey(name); 11 | this.refresh(); 12 | } 13 | 14 | refresh() { 15 | this.algorithms = listDirectories(this.path) 16 | .map(algorithmName => new Algorithm(path.resolve(this.path, algorithmName), algorithmName)); 17 | } 18 | 19 | toJSON() { 20 | const {key, name, algorithms} = this; 21 | return {key, name, algorithms}; 22 | } 23 | } 24 | 25 | export default Category; 26 | -------------------------------------------------------------------------------- /src/models/Algorithm.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import { createKey, listFiles } from 'utils/hierarchy'; 3 | import { File } from 'models'; 4 | import { getDescription } from 'utils/misc'; 5 | 6 | export class Algorithm { 7 | key: string; 8 | files!: File[]; 9 | description!: string; 10 | 11 | constructor(private path: string, public name: string) { 12 | this.key = createKey(name); 13 | this.refresh(); 14 | } 15 | 16 | refresh() { 17 | this.files = listFiles(this.path) 18 | .map(fileName => new File(path.resolve(this.path, fileName), fileName)); 19 | this.description = getDescription(this.files); 20 | } 21 | 22 | toJSON() { 23 | const {key, name} = this; 24 | return {key, name}; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/tracers/js/JsTracer.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import { Release, Tracer } from 'tracers/Tracer'; 3 | import express from 'express'; 4 | 5 | export class JsTracer extends Tracer { 6 | readonly workerPath: string; 7 | tagName?: string; 8 | 9 | constructor() { 10 | super('js'); 11 | this.workerPath = path.resolve(__dirname, 'worker.js'); 12 | } 13 | 14 | async build(release: Release) { 15 | const {tag_name} = release; 16 | this.tagName = tag_name; 17 | } 18 | 19 | route(router: express.Router) { 20 | router.get(`/${this.lang}`, (req, res) => { 21 | if (!this.tagName) throw new Error('JsTracer has not been built yet.'); 22 | const version = this.tagName.slice(1); 23 | res.redirect(`https://unpkg.com/algorithm-visualizer@${version}/dist/algorithm-visualizer.umd.js`); 24 | }); 25 | router.get(`/${this.lang}/worker`, (req, res) => res.sendFile(this.workerPath)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/tracers/LambdaTracer.ts: -------------------------------------------------------------------------------- 1 | import AWS from 'aws-sdk'; 2 | import express from 'express'; 3 | import { Release, Tracer } from 'tracers/Tracer'; 4 | import { awsAccessKeyId, awsSecretAccessKey } from 'config/environments'; 5 | import { BadRequest } from 'ts-httpexceptions'; 6 | 7 | export class LambdaTracer extends Tracer { 8 | static lambda = new AWS.Lambda({ 9 | region: 'us-east-2', 10 | accessKeyId: awsAccessKeyId, 11 | secretAccessKey: awsSecretAccessKey, 12 | }); 13 | 14 | async build(release: Release) { 15 | } 16 | 17 | route(router: express.Router) { 18 | router.post(`/${this.lang}`, (req, res, next) => { 19 | const {code} = req.body; 20 | LambdaTracer.lambda.invoke({ 21 | FunctionName: `extractor-${this.lang}`, 22 | InvocationType: 'RequestResponse', 23 | Payload: JSON.stringify(code), 24 | }, function (err, data) { 25 | if (err) return next(err); 26 | if (typeof data.Payload !== 'string') return next(new Error('Unexpected Payload Type')); 27 | const payload = JSON.parse(data.Payload); 28 | if (!payload.success) return next(new BadRequest(payload.errorMessage)); 29 | res.send(payload.commands); 30 | }); 31 | }); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/controllers/AuthController.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import { githubClientId } from 'config/environments'; 3 | import { GitHubApi } from 'utils/apis'; 4 | import { Controller } from 'controllers/Controller'; 5 | import Server from 'Server'; 6 | 7 | export class AuthController extends Controller { 8 | constructor(server: Server) { 9 | super(server); 10 | this.router 11 | .get('/request', this.request) 12 | .get('/response', this.response) 13 | .get('/destroy', this.destroy); 14 | } 15 | 16 | route = (router: express.Router): void => { 17 | router.use('/auth', this.router); 18 | }; 19 | 20 | request = (req: express.Request, res: express.Response) => { 21 | res.redirect(`https://github.com/login/oauth/authorize?client_id=${githubClientId}&scope=user,gist`); 22 | }; 23 | 24 | response = (req: express.Request, res: express.Response, next: express.NextFunction) => { 25 | const {code} = req.query; 26 | 27 | GitHubApi.getAccessToken(code).then(({data}) => { 28 | const {access_token} = data; 29 | res.send(``); 30 | }).catch(next); 31 | }; 32 | 33 | destroy = (req: express.Request, res: express.Response) => { 34 | res.send(``); 35 | }; 36 | } 37 | -------------------------------------------------------------------------------- /src/controllers/AlgorithmsController.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import { Controller } from 'controllers/Controller'; 3 | import { NotFound } from 'ts-httpexceptions'; 4 | import Server from 'Server'; 5 | 6 | export class AlgorithmsController extends Controller { 7 | constructor(server: Server) { 8 | super(server); 9 | this.router 10 | .get('/', this.getHierarchy) 11 | .get('/:categoryKey/:algorithmKey', this.getAlgorithm) 12 | .get('/sitemap.txt', this.getSitemap); 13 | } 14 | 15 | route = (router: express.Router): void => { 16 | router.use('/algorithms', this.router); 17 | }; 18 | 19 | getHierarchy = (req: express.Request, res: express.Response) => { 20 | res.json(this.server.hierarchy); 21 | }; 22 | 23 | getAlgorithm = (req: express.Request, res: express.Response, next: express.NextFunction) => { 24 | const {categoryKey, algorithmKey} = req.params; 25 | const algorithm = this.server.hierarchy.find(categoryKey, algorithmKey); 26 | if (!algorithm) return next(new NotFound('Algorithm not found.')); 27 | res.json({algorithm}); 28 | }; 29 | 30 | getSitemap = (req: express.Request, res: express.Response) => { 31 | const urls: string[] = []; 32 | this.server.hierarchy.iterate((category, algorithm) => { 33 | urls.push(`https://algorithm-visualizer.org/${category.key}/${algorithm.key}`); 34 | }); 35 | res.set('Content-Type', 'text/plain'); 36 | res.send(urls.join('\n')); 37 | }; 38 | } 39 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "class-name": true, 4 | "comment-format": [ 5 | true, 6 | "check-space" 7 | ], 8 | "indent": [ 9 | true, 10 | "spaces", 11 | 2 12 | ], 13 | "one-line": [ 14 | true, 15 | "check-open-brace", 16 | "check-whitespace" 17 | ], 18 | "no-var-keyword": true, 19 | "quotemark": [ 20 | true, 21 | "single", 22 | "avoid-escape" 23 | ], 24 | "semicolon": [ 25 | true, 26 | "always", 27 | "ignore-bound-class-methods" 28 | ], 29 | "whitespace": [ 30 | true, 31 | "check-branch", 32 | "check-decl", 33 | "check-operator", 34 | "check-module", 35 | "check-separator", 36 | "check-type" 37 | ], 38 | "typedef-whitespace": [ 39 | true, 40 | { 41 | "call-signature": "nospace", 42 | "index-signature": "nospace", 43 | "parameter": "nospace", 44 | "property-declaration": "nospace", 45 | "variable-declaration": "nospace" 46 | }, 47 | { 48 | "call-signature": "onespace", 49 | "index-signature": "onespace", 50 | "parameter": "onespace", 51 | "property-declaration": "onespace", 52 | "variable-declaration": "onespace" 53 | } 54 | ], 55 | "no-internal-module": true, 56 | "no-trailing-whitespace": true, 57 | "no-null-keyword": true, 58 | "prefer-const": true, 59 | "jsdoc-format": true 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@algorithm-visualizer/server", 3 | "version": "2.0.0", 4 | "title": "Algorithm Visualizer", 5 | "description": "Algorithm Visualizer is an interactive online platform that visualizes algorithms from code.", 6 | "scripts": { 7 | "watch": "NODE_ENV=development NODE_PATH=src ts-node-dev --respawn --ignore-watch node_modules --no-notify src", 8 | "start": "NODE_ENV=production NODE_PATH=src ts-node --transpile-only src", 9 | "tslint": "tslint -c tslint.json -p tsconfig.json" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/algorithm-visualizer/server.git" 14 | }, 15 | "license": "MIT", 16 | "devDependencies": { 17 | "@types/compression": "0.0.36", 18 | "@types/execa": "^0.9.0", 19 | "@types/express": "^4.16.1", 20 | "@types/fs-extra": "^7.0.0", 21 | "@types/morgan": "^1.7.35", 22 | "@types/node": "^12.0.0", 23 | "@types/node-cron": "^3.0.1", 24 | "@types/remove-markdown": "^0.1.1", 25 | "@types/uuid": "^3.4.4", 26 | "ts-node-dev": "^1.0.0-pre.39", 27 | "tslint": "^5.16.0" 28 | }, 29 | "dependencies": { 30 | "aws-sdk": "^2.814.0", 31 | "axios": "^0.21.2", 32 | "body-parser": "^1.18.2", 33 | "compression": "^1.7.3", 34 | "dotenv": "^8.0.0", 35 | "dotenv-flow": "^2.0.0", 36 | "express": "^4.16.4", 37 | "express-github-webhook": "^1.0.6", 38 | "fs-extra": "^6.0.1", 39 | "morgan": "^1.9.1", 40 | "node-cron": "^3.0.0", 41 | "remove-markdown": "^0.3.0", 42 | "ts-httpexceptions": "^4.1.0", 43 | "ts-node": "^8.1.0", 44 | "typescript": "^3.4.5", 45 | "uuid": "^3.3.2" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/controllers/VisualizationsController.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import path from 'path'; 3 | import uuid from 'uuid'; 4 | import fs from 'fs-extra'; 5 | import { Controller } from 'controllers/Controller'; 6 | import Server from 'Server'; 7 | import { visualizationsDir } from 'config/paths'; 8 | 9 | export class VisualizationsController extends Controller { 10 | constructor(server: Server) { 11 | super(server); 12 | this.router 13 | .post('/', this.uploadVisualization) 14 | .get('/:visualizationId', this.getVisualization); 15 | 16 | fs.remove(visualizationsDir).catch(console.error); 17 | } 18 | 19 | route = (router: express.Router): void => { 20 | router.use('/visualizations', this.router); 21 | }; 22 | 23 | uploadVisualization = (req: express.Request, res: express.Response, next: express.NextFunction) => { 24 | const {content} = req.body; 25 | const visualizationId = uuid.v4(); 26 | const visualizationPath = path.resolve(visualizationsDir, `${visualizationId}.json`); 27 | const url = `https://algorithm-visualizer.org/scratch-paper/new?visualizationId=${visualizationId}`; 28 | fs.outputFile(visualizationPath, content) 29 | .then(() => res.send(url)) 30 | .catch(next); 31 | }; 32 | 33 | getVisualization = (req: express.Request, res: express.Response, next: express.NextFunction) => { 34 | const {visualizationId} = req.params; 35 | const visualizationPath = path.resolve(visualizationsDir, `${visualizationId}.json`); 36 | res.sendFile(visualizationPath, err => { 37 | if (err) next(new Error('Visualization Expired')); 38 | fs.remove(visualizationPath); 39 | }); 40 | }; 41 | } 42 | -------------------------------------------------------------------------------- /src/middlewares/frontendMiddleware.ts: -------------------------------------------------------------------------------- 1 | import express, { NextFunction, Request, Response } from 'express'; 2 | import path from 'path'; 3 | import fs from 'fs-extra'; 4 | import url from 'url'; 5 | import Server from 'Server'; 6 | import { frontendBuiltDir } from 'config/paths'; 7 | 8 | const packageJson = require('../../package.json'); 9 | 10 | export function frontendMiddleware(server: Server) { 11 | const staticMiddleware = express.static(frontendBuiltDir, {index: false}); 12 | 13 | if (!fs.pathExistsSync(frontendBuiltDir)) { 14 | server.updateFrontend().catch(console.error); 15 | } 16 | 17 | return (req: Request, res: Response, next: NextFunction) => { 18 | staticMiddleware(req, res, err => { 19 | if (err) return next(err); 20 | if (req.method !== 'GET') return next(); 21 | 22 | const filePath = path.resolve(frontendBuiltDir, 'index.html'); 23 | fs.readFile(filePath, 'utf8', (err, data) => { 24 | if (err) return next(err); 25 | 26 | const {pathname} = url.parse(req.originalUrl); 27 | if (!pathname) return next(new Error('Failed to get `pathname`.')); 28 | const [, categoryKey, algorithmKey] = pathname.split('/'); 29 | let {title, description} = packageJson; 30 | let algorithm = undefined; 31 | if (categoryKey && categoryKey !== 'scratch-paper') { 32 | algorithm = server.hierarchy.find(categoryKey, algorithmKey) || null; 33 | if (algorithm) { 34 | title = [algorithm.categoryName, algorithm.algorithmName].join(' - '); 35 | description = algorithm.description; 36 | } else { 37 | res.status(404); 38 | return; 39 | } 40 | } 41 | 42 | const indexFile = data 43 | .replace(/\$TITLE/g, title) 44 | .replace(/\$DESCRIPTION/g, description) 45 | .replace(/\$ALGORITHM/g, algorithm === undefined ? 'undefined' : 46 | JSON.stringify(algorithm).replace(/ #### Table of Contents 4 | > - [Running Locally](#running-locally) 5 | > - [Directory Structure](#directory-structure) 6 | 7 | Are you a first-timer in contributing to open source? [These guidelines](https://opensource.guide/how-to-contribute/#how-to-submit-a-contribution) from GitHub might help! 8 | 9 | ## Running Locally 10 | 11 | 1. Fork this repository. 12 | 13 | 2. Clone your forked repo to your machine. 14 | 15 | ```bash 16 | git clone https://github.com//server.git 17 | ``` 18 | 19 | 3. Install [Docker](https://docs.docker.com/install/), if not done already. 20 | 21 | 4. Create `.env.local` in the project root: 22 | ```bash 23 | # By putting dummy values, GitHub sign in will not work locally 24 | GITHUB_CLIENT_ID = dummy 25 | GITHUB_CLIENT_SECRET = dummy 26 | 27 | # By putting dummy values, extracting visualizing commands will not work locally (except for JavaScript). 28 | AWS_ACCESS_KEY_ID = dummy 29 | AWS_SECRET_ACCESS_KEY = dummy 30 | ``` 31 | 32 | 5. Install dependencies, and run the server. 33 | 34 | ```bash 35 | cd server 36 | 37 | npm install 38 | 39 | npm run watch 40 | ``` 41 | 42 | 6. Open [`http://localhost:8080/`](http://localhost:8080/) in a web browser. 43 | 44 | ## Directory Structure 45 | 46 | - [**src/**](src) contains source code. 47 | - [**config/**](src/config) contains configuration files. 48 | - [**controllers/**](src/controllers) routes and processes incoming requests. 49 | - [**middlewares/**](src/middlewares) contains Express middlewares. 50 | - [**models/**](src/models) manages algorithm visualizations and their hierarchy. 51 | - [**tracers/**](src/tracers) build visualization libraries and compiles/runs code. 52 | - [**utils/**](src/utils) contains utility files. 53 | 54 | **NOTE** that for JavaScript, it builds a web worker rather than a docker image. Once a browser fetches the web worker, it will submit users' code to the web worker locally, instead of submitting to the remote server, to extract visualizing commands. 55 | -------------------------------------------------------------------------------- /src/utils/apis.ts: -------------------------------------------------------------------------------- 1 | import axios, { AxiosResponse } from 'axios'; 2 | import { githubClientId, githubClientSecret } from 'config/environments'; 3 | 4 | const instance = axios.create(); 5 | 6 | instance.interceptors.request.use(request => { 7 | request.params = {client_id: githubClientId, client_secret: githubClientSecret, ...request.params}; 8 | return request; 9 | }); 10 | 11 | const request = (url: string, process: (mappedUrl: string, args: any[]) => Promise>) => { 12 | const tokens = url.split('/'); 13 | const baseURL = /^https?:\/\//i.test(url) ? '' : 'https://api.github.com'; 14 | return (...args: any[]) => { 15 | const mappedUrl = baseURL + tokens.map(token => token.startsWith(':') ? args.shift() : token).join('/'); 16 | return process(mappedUrl, args); 17 | }; 18 | }; 19 | 20 | const GET = (url: string) => { 21 | return request(url, (mappedUrl: string, args: any[]) => { 22 | const [params] = args; 23 | return instance.get(mappedUrl, {params}); 24 | }); 25 | }; 26 | 27 | const DELETE = (url: string) => { 28 | return request(url, (mappedUrl: string, args: any[]) => { 29 | const [params] = args; 30 | return instance.delete(mappedUrl, {params}); 31 | }); 32 | }; 33 | 34 | const POST = (url: string) => { 35 | return request(url, (mappedUrl: string, args: any[]) => { 36 | const [body, params] = args; 37 | return instance.post(mappedUrl, body, {params}); 38 | }); 39 | }; 40 | 41 | const PUT = (url: string) => { 42 | return request(url, (mappedUrl: string, args: any[]) => { 43 | const [body, params] = args; 44 | return instance.put(mappedUrl, body, {params}); 45 | }); 46 | }; 47 | 48 | const PATCH = (url: string) => { 49 | return request(url, (mappedUrl: string, args: any[]) => { 50 | const [body, params] = args; 51 | return instance.patch(mappedUrl, body, {params}); 52 | }); 53 | }; 54 | 55 | export const GitHubApi = { 56 | listCommits: GET('/repos/:owner/:repo/commits'), 57 | 58 | getAccessToken: (code: string) => instance.post('https://github.com/login/oauth/access_token', {code}, {headers: {Accept: 'application/json'}}), 59 | 60 | getLatestRelease: GET('/repos/:owner/:repo/releases/latest'), 61 | }; 62 | -------------------------------------------------------------------------------- /src/config/environments.ts: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import { ServerOptions } from 'https'; 3 | import path from 'path'; 4 | import { issueHttpsCertificate } from '../utils/misc'; 5 | 6 | require('dotenv-flow').config(); 7 | 8 | const { 9 | NODE_ENV, 10 | 11 | HTTP_PORT, 12 | HTTPS_PORT, 13 | 14 | CREDENTIALS_ENABLED, 15 | CREDENTIALS_PATH, 16 | CREDENTIALS_CA, 17 | CREDENTIALS_KEY, 18 | CREDENTIALS_CERT, 19 | 20 | WEBHOOK_ENABLED, 21 | WEBHOOK_SECRET, 22 | 23 | GITHUB_CLIENT_ID, 24 | GITHUB_CLIENT_SECRET, 25 | 26 | AWS_ACCESS_KEY_ID, 27 | AWS_SECRET_ACCESS_KEY, 28 | } = process.env as { 29 | [key: string]: string, 30 | }; 31 | 32 | const isEnabled = (v: string) => v === '1'; 33 | 34 | const missingVars = [ 35 | 'NODE_ENV', 36 | 'HTTP_PORT', 37 | 'HTTPS_PORT', 38 | 'CREDENTIALS_ENABLED', 39 | ...(isEnabled(CREDENTIALS_ENABLED) ? [ 40 | 'CREDENTIALS_PATH', 41 | 'CREDENTIALS_CA', 42 | 'CREDENTIALS_KEY', 43 | 'CREDENTIALS_CERT', 44 | ] : []), 45 | 'WEBHOOK_ENABLED', 46 | ...(isEnabled(WEBHOOK_ENABLED) ? [ 47 | 'WEBHOOK_SECRET', 48 | ] : []), 49 | 'GITHUB_CLIENT_ID', 50 | 'GITHUB_CLIENT_SECRET', 51 | 'AWS_ACCESS_KEY_ID', 52 | 'AWS_SECRET_ACCESS_KEY', 53 | ].filter(variable => process.env[variable] === undefined); 54 | if (missingVars.length) throw new Error(`The following environment variables are missing: ${missingVars.join(', ')}`); 55 | 56 | export const __PROD__ = NODE_ENV === 'production'; 57 | export const __DEV__ = NODE_ENV === 'development'; 58 | 59 | export const httpPort = parseInt(HTTP_PORT); 60 | export const httpsPort = parseInt(HTTPS_PORT); 61 | 62 | export const webhookOptions = isEnabled(WEBHOOK_ENABLED) ? { 63 | path: '/webhook', 64 | secret: WEBHOOK_SECRET, 65 | } : undefined; 66 | 67 | export let credentials: ServerOptions | undefined; 68 | if (isEnabled(CREDENTIALS_ENABLED)) { 69 | if (fs.existsSync(CREDENTIALS_PATH)) { 70 | const readCredentials = (file: string) => fs.readFileSync(path.resolve(CREDENTIALS_PATH, file)); 71 | credentials = { 72 | ca: readCredentials(CREDENTIALS_CA), 73 | key: readCredentials(CREDENTIALS_KEY), 74 | cert: readCredentials(CREDENTIALS_CERT), 75 | }; 76 | } else { 77 | issueHttpsCertificate(); 78 | } 79 | } 80 | 81 | export const githubClientId = GITHUB_CLIENT_ID; 82 | export const githubClientSecret = GITHUB_CLIENT_SECRET; 83 | 84 | export const awsAccessKeyId = AWS_ACCESS_KEY_ID; 85 | export const awsSecretAccessKey = AWS_SECRET_ACCESS_KEY; 86 | -------------------------------------------------------------------------------- /src/tracers/DockerTracer.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import { Release, Tracer } from 'tracers/Tracer'; 3 | import express from 'express'; 4 | import uuid from 'uuid'; 5 | import fs from 'fs-extra'; 6 | import { memoryLimit, timeLimit } from 'config/constants'; 7 | import { codesDir } from 'config/paths'; 8 | import { execute } from 'utils/misc'; 9 | 10 | export class DockerTracer extends Tracer { 11 | private readonly directory: string; 12 | private readonly imageName: string; 13 | 14 | constructor(lang: string) { 15 | super(lang); 16 | this.directory = path.resolve(__dirname, lang); 17 | this.imageName = `tracer-${this.lang}`; 18 | } 19 | 20 | build(release: Release) { 21 | const {tag_name} = release; 22 | return execute(`docker build -t ${this.imageName} . --build-arg tag_name=${tag_name}`, { 23 | cwd: this.directory, 24 | stdout: process.stdout, 25 | stderr: process.stderr, 26 | }); 27 | } 28 | 29 | route(router: express.Router) { 30 | router.post(`/${this.lang}`, (req, res, next) => { 31 | const {code} = req.body; 32 | const tempPath = path.resolve(codesDir, uuid.v4()); 33 | fs.outputFile(path.resolve(tempPath, `Main.${this.lang}`), code) 34 | .then(() => { 35 | const containerName = uuid.v4(); 36 | let killed = false; 37 | const timer = setTimeout(() => { 38 | execute(`docker kill ${containerName}`).then(() => { 39 | killed = true; 40 | }); 41 | }, timeLimit); 42 | return execute([ 43 | 'docker run --rm', 44 | `--name=${containerName}`, 45 | '-w=/usr/visualization', 46 | `-v=${tempPath}:/usr/visualization:rw`, 47 | `-m=${memoryLimit}m`, 48 | '-e ALGORITHM_VISUALIZER=1', 49 | this.imageName, 50 | ].join(' ')).catch(error => { 51 | if (killed) throw new Error('Time Limit Exceeded'); 52 | throw error; 53 | }).finally(() => clearTimeout(timer)); 54 | }) 55 | .then(() => new Promise((resolve, reject) => { 56 | const visualizationPath = path.resolve(tempPath, 'visualization.json'); 57 | res.sendFile(visualizationPath, (err: any) => { 58 | if (err) return reject(new Error('Visualization Not Found')); 59 | resolve(); 60 | }); 61 | })) 62 | .catch(next) 63 | .finally(() => fs.remove(tempPath)); 64 | }); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/utils/misc.ts: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | import fs from 'fs-extra'; 3 | import { File } from 'models'; 4 | import removeMarkdown from 'remove-markdown'; 5 | import * as child_process from 'child_process'; 6 | import { ExecOptions, spawn } from 'child_process'; 7 | import { rootDir } from '../config/paths'; 8 | import path from 'path'; 9 | 10 | export function download(url: string, localPath: string) { 11 | return axios({ url, method: 'GET', responseType: 'stream' }) 12 | .then(response => new Promise((resolve, reject) => { 13 | const writer = fs.createWriteStream(localPath); 14 | writer.on('finish', resolve); 15 | writer.on('error', reject); 16 | response.data.pipe(writer); 17 | })); 18 | } 19 | 20 | export async function pull(dir: string, repo: string, commit = 'origin/master') { 21 | if (fs.pathExistsSync(dir)) { 22 | await execute(`git fetch`, { 23 | cwd: dir, 24 | stdout: process.stdout, 25 | stderr: process.stderr, 26 | }); 27 | } else { 28 | await execute(`git clone https://github.com/algorithm-visualizer/${repo}.git ${dir}`, { 29 | stdout: process.stdout, 30 | stderr: process.stderr, 31 | }); 32 | } 33 | await execute(`git reset --hard ${commit}`, { 34 | cwd: dir, 35 | stdout: process.stdout, 36 | stderr: process.stderr, 37 | }); 38 | } 39 | 40 | export function getDescription(files: File[]) { 41 | const readmeFile = files.find(file => file.name === 'README.md'); 42 | if (!readmeFile) return ''; 43 | const lines = readmeFile.content.split('\n'); 44 | lines.shift(); 45 | while (lines.length && !lines[0].trim()) lines.shift(); 46 | const descriptionLines = []; 47 | while (lines.length && lines[0].trim()) descriptionLines.push(lines.shift()); 48 | return removeMarkdown(descriptionLines.join(' ')); 49 | } 50 | 51 | type ExecuteOptions = ExecOptions & { 52 | stdout?: NodeJS.WriteStream; 53 | stderr?: NodeJS.WriteStream; 54 | }; 55 | 56 | export function execute(command: string, { stdout, stderr, ...options }: ExecuteOptions = {}): Promise { 57 | return new Promise((resolve, reject) => { 58 | const child = child_process.exec(command, options, (error, stdout, stderr) => { 59 | if (error) return reject(error.code ? new Error(stderr) : error); 60 | resolve(stdout); 61 | }); 62 | if (child.stdout && stdout) child.stdout.pipe(stdout); 63 | if (child.stderr && stderr) child.stderr.pipe(stderr); 64 | }); 65 | } 66 | 67 | export function issueHttpsCertificate() { 68 | const certbotIniPath = path.resolve(rootDir, 'certbot.ini'); 69 | const childProcess = spawn('certbot', ['certonly', '--non-interactive', '--agree-tos', '--config', certbotIniPath]); 70 | childProcess.stdout.pipe(process.stdout); 71 | childProcess.stderr.pipe(process.stderr); 72 | childProcess.on('error', console.error); 73 | childProcess.on('exit', code => { 74 | if (code === 0) { 75 | process.exit(0); 76 | } else { 77 | console.error(new Error(`certbot failed with exit code ${code}.`)); 78 | } 79 | }); 80 | } 81 | -------------------------------------------------------------------------------- /src/models/Hierarchy.ts: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import { listDirectories } from 'utils/hierarchy'; 3 | import { GitHubApi } from 'utils/apis'; 4 | import { Algorithm, Category, File } from 'models'; 5 | import { Author } from 'models/File'; 6 | import { algorithmsDir } from 'config/paths'; 7 | import { execute, pull } from 'utils/misc'; 8 | 9 | type CommitAuthors = { 10 | [sha: string]: Author, 11 | } 12 | 13 | export class Hierarchy { 14 | private categories!: Category[]; 15 | readonly path: string = algorithmsDir; 16 | 17 | constructor() { 18 | this.refresh(); 19 | this.update().catch(console.error); 20 | } 21 | 22 | refresh() { 23 | this.categories = listDirectories(this.path) 24 | .map(categoryName => new Category(path.resolve(this.path, categoryName), categoryName)); 25 | 26 | const files: File[] = []; 27 | this.categories.forEach(category => category.algorithms.forEach(algorithm => files.push(...algorithm.files))); 28 | this.cacheCommitAuthors().then(commitAuthors => this.cacheContributors(files, commitAuthors)); 29 | } 30 | 31 | async update(commit?: string) { 32 | await pull(this.path, 'algorithms', commit); 33 | this.refresh(); 34 | }; 35 | 36 | async cacheCommitAuthors(page = 1, commitAuthors: CommitAuthors = {}): Promise { 37 | const per_page = 100; 38 | const {data} = await GitHubApi.listCommits('algorithm-visualizer', 'algorithms', {per_page, page}); 39 | const commits: any[] = data; 40 | for (const {sha, author} of commits) { 41 | if (!author) continue; 42 | const {login, avatar_url} = author; 43 | commitAuthors[sha] = {login, avatar_url}; 44 | } 45 | if (commits.length < per_page) { 46 | return commitAuthors; 47 | } else { 48 | return this.cacheCommitAuthors(page + 1, commitAuthors); 49 | } 50 | } 51 | 52 | async cacheContributors(files: File[], commitAuthors: CommitAuthors) { 53 | for (const file of files) { 54 | const stdout = await execute(`git --no-pager log --follow --no-merges --format="%H" -- "${path.relative(this.path, file.path)}"`, { 55 | cwd: this.path, 56 | }); 57 | const output = stdout.toString().replace(/\n$/, ''); 58 | const shas = output.split('\n').reverse(); 59 | const contributors: Author[] = []; 60 | for (const sha of shas) { 61 | const author = commitAuthors[sha]; 62 | if (author && !contributors.find(contributor => contributor.login === author.login)) { 63 | contributors.push(author); 64 | } 65 | } 66 | file.contributors = contributors; 67 | } 68 | } 69 | 70 | find(categoryKey: string, algorithmKey: string) { 71 | const category = this.categories.find(category => category.key === categoryKey); 72 | if (!category) return; 73 | const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey); 74 | if (!algorithm) return; 75 | 76 | const categoryName = category.name; 77 | const algorithmName = algorithm.name; 78 | const files = algorithm.files; 79 | const description = algorithm.description; 80 | 81 | return {categoryKey, categoryName, algorithmKey, algorithmName, files, description}; 82 | } 83 | 84 | iterate(callback: (category: Category, algorithm: Algorithm) => void) { 85 | this.categories.forEach(category => category.algorithms.forEach(algorithm => callback(category, algorithm))); 86 | } 87 | 88 | toJSON() { 89 | const {categories} = this; 90 | return {categories}; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/Server.ts: -------------------------------------------------------------------------------- 1 | import express, { NextFunction, Request, Response } from 'express'; 2 | import morgan from 'morgan'; 3 | import bodyParser from 'body-parser'; 4 | import * as Controllers from 'controllers'; 5 | import { NotFound } from 'ts-httpexceptions'; 6 | import compression from 'compression'; 7 | import { __PROD__, credentials, httpPort, httpsPort, webhookOptions } from 'config/environments'; 8 | import http from 'http'; 9 | import https from 'https'; 10 | import cron from 'node-cron'; 11 | import { Hierarchy } from 'models'; 12 | import * as Tracers from 'tracers'; 13 | import { errorHandlerMiddleware, frontendMiddleware, redirectMiddleware } from 'middlewares'; 14 | import { execute, issueHttpsCertificate, pull } from 'utils/misc'; 15 | import { frontendBuildDir, frontendBuiltDir, frontendDir, rootDir } from 'config/paths'; 16 | 17 | const Webhook = require('express-github-webhook'); 18 | 19 | export default class Server { 20 | readonly hierarchy = new Hierarchy(); 21 | readonly tracers = Object.values(Tracers).map(Tracer => new Tracer()); 22 | private readonly app = express(); 23 | private readonly webhook = webhookOptions && Webhook(webhookOptions); 24 | 25 | constructor() { 26 | this.app 27 | .use(compression()) 28 | .use(morgan(__PROD__ ? 'tiny' : 'dev')) 29 | .use(redirectMiddleware()) 30 | .use(bodyParser.json()) 31 | .use(bodyParser.urlencoded({ extended: true })) 32 | .use('/api', this.getApiRouter()) 33 | .use(frontendMiddleware(this)); 34 | if (this.webhook) { 35 | this.app.use(this.webhook); 36 | } 37 | this.app.use(errorHandlerMiddleware()); 38 | 39 | if (this.webhook) { 40 | this.webhook.on('push', async (repo: string, data: any) => { 41 | const { ref, head_commit } = data; 42 | if (ref !== 'refs/heads/master') return; 43 | if (!head_commit) throw new Error('The `head_commit` is empty.'); 44 | 45 | switch (repo) { 46 | case 'server': 47 | await this.update(head_commit.id); 48 | break; 49 | case 'algorithm-visualizer': 50 | await this.updateFrontend(head_commit.id); 51 | break; 52 | case 'algorithms': 53 | await this.hierarchy.update(head_commit.id); 54 | break; 55 | default: 56 | throw new Error(`Webhook from unknown repository '${repo}'.`); 57 | } 58 | }); 59 | 60 | this.webhook.on('release', async (repo: string, data: any) => { 61 | const tracer = this.tracers.find(tracer => repo === `tracers.${tracer.lang}`); 62 | if (!tracer) throw new Error(`Tracer not found for repository '${repo}'.`); 63 | await tracer.update(data.release); 64 | }); 65 | } 66 | 67 | if (credentials) { 68 | cron.schedule('0 0 1 * *', () => { 69 | issueHttpsCertificate(); 70 | }); 71 | } 72 | } 73 | 74 | getApiRouter() { 75 | const router = express.Router(); 76 | Object.values(Controllers).forEach(Controller => new Controller(this).route(router)); 77 | router.use((req: Request, res: Response, next: NextFunction) => { 78 | next(new NotFound('API not found.')); 79 | }); 80 | return router; 81 | }; 82 | 83 | async update(commit?: string) { 84 | await pull(rootDir, 'server', commit); 85 | await execute('npm install', { 86 | cwd: rootDir, 87 | stdout: process.stdout, 88 | stderr: process.stderr, 89 | }); 90 | process.exit(0); 91 | }; 92 | 93 | async updateFrontend(commit?: string) { 94 | await pull(frontendDir, 'algorithm-visualizer', commit); 95 | await execute([ 96 | 'npm install', 97 | 'npm run build', 98 | `rm -rf ${frontendBuiltDir}`, 99 | `mv ${frontendBuildDir} ${frontendBuiltDir}`, 100 | ].join(' && '), { 101 | cwd: frontendDir, 102 | stdout: process.stdout, 103 | stderr: process.stderr, 104 | }); 105 | } 106 | 107 | start() { 108 | const httpServer = http.createServer(this.app); 109 | httpServer.listen(httpPort); 110 | console.info(`http: listening on port ${httpPort}`); 111 | 112 | if (credentials) { 113 | const httpsServer = https.createServer(credentials, this.app); 114 | httpsServer.listen(httpsPort); 115 | console.info(`https: listening on port ${httpsPort}`); 116 | } 117 | } 118 | } 119 | --------------------------------------------------------------------------------