提交 31ee7ecf 编写于 作者: J Jason Park

Download traces lib

上级 dfec3da6
import Promise from 'bluebird';
import axios from 'axios';
import fs from 'fs';
import { githubClientId, githubClientSecret } from '/environment';
axios.interceptors.request.use(request => {
request.params = { client_id: githubClientId, client_secret: githubClientSecret, ...request.params };
return request;
});
axios.interceptors.response.use(response => {
return response.data;
......@@ -54,9 +61,18 @@ const PATCH = URL => {
};
const GitHubApi = {
auth: (client_id, client_secret) => axios.defaults.params = { client_id, client_secret },
listCommits: GET('/repos/:owner/:repo/commits'),
getAccessToken: code => axios.post('https://github.com/login/oauth/access_token', { code }, { headers: { Accept: 'application/json' } }),
getLatestRelease: GET('/repos/:owner/:repo/releases/latest'),
download: (url, path) => axios({
method: 'get',
url,
responseType: 'stream',
}).then(data => new Promise((resolve, reject) => {
data.pipe(fs.createWriteStream(path));
data.on('end', resolve);
data.on('error', reject);
})),
};
export {
......
import Promise from 'bluebird';
import express from 'express';
import path from 'path';
import { GitHubApi } from '/apis';
const router = express.Router();
const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'libs', ...args);
const downloadLibs = () => {
GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(release => {
return Promise.each(release.assets, asset => GitHubApi.download(asset.browser_download_url, getPath(asset.name)));
});
};
downloadLibs(); // TODO: download again when webhooked
const getJsWorker = (req, res, next) => {
res.sendFile(path.resolve(__dirname, '..', 'tracers', 'languages', 'js', 'build', 'index.js'));
};
......
......@@ -4,10 +4,7 @@ import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import * as controllers from '/controllers';
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
import { GitHubApi } from '/apis';
import { githubClientId, githubClientSecret } from '/environment';
GitHubApi.auth(githubClientId, githubClientSecret);
const app = express();
app.use(morgan('tiny'));
app.use(cookieParser());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册