Skip to content

Commit

Permalink
feat: add koa server for gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 28, 2019
1 parent 4375669 commit bc4e9fc
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 17 deletions.
30 changes: 30 additions & 0 deletions lib/gateway/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// tslint:disable-next-line:no-submodule-imports
import { NowRequest, NowResponse } from '@now/node/dist';
import path from 'path';
import Koa from 'koa';
import Router from 'koa-router';

import { loadConfig } from '../utils';
import { Server } from './server';
import { FcRequest, FcResponse } from './types';
Expand Down Expand Up @@ -80,3 +83,30 @@ export async function nowHandler(req: NowRequest, res: NowResponse): Promise<voi
break;
}
}

export const createHttpServer = () => {
process.env.KOA_SERVER = 'true';

const app = new Koa();
const router = new Router();
const cwd = process.cwd();
const configFile = path.join(cwd, '/surgio.conf.js');
const config = loadConfig(cwd, configFile);
const surgioServer = new Server(config);

router.use((() => {
return async (_, next) => {
await surgioServer.init();
await next();
};
})());

router.get('/get-artifact/:name', surgioServer.koaGetArtifact.bind(surgioServer));
router.get('/list-artifact', surgioServer.koaListArtifact.bind(surgioServer));

app
.use(router.routes())
.use(router.allowedMethods());

return app.callback();
};
48 changes: 48 additions & 0 deletions lib/gateway/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// tslint:disable-next-line:no-submodule-imports
import { NowRequest, NowResponse } from '@now/node/dist';
import { Context } from 'koa';
import nunjucks from 'nunjucks';
import url from 'url';

Expand Down Expand Up @@ -128,4 +129,51 @@ export class Server {
});
res.send(result);
}

public async koaGetArtifact(ctx: Context): Promise<void> {
const dl = ctx.query.dl;
const artifactName = ctx.params.name;

if (!artifactName) {
ctx.status = 400;
return;
}

const result = await this.getArtifact(artifactName as string);

if (result) {
ctx.set('content-type', 'text/plain; charset=utf-8');
ctx.set('cache-control', 'private, no-cache, no-store');

if (dl === '1'){
ctx.set('content-disposition', `attachment; filename="${artifactName}"`);
}

ctx.body = result;
} else {
ctx.status = 404;
}
}

public async koaListArtifact(ctx: Context): Promise<void> {
const engine = nunjucks.configure({
autoescape: false,
});
const artifactListTpl = require('./template/artifact-list').default;

ctx.body = engine.renderString(artifactListTpl, {
artifactList: this.artifactList,
getPreviewUrl: (name: string) => getDownloadUrl(this.config.urlBase, name),
getDownloadUrl: (name: string) => (
url.format({
pathname: `/get-artifact/${name}`,
query: {
dl: '1',
},
})
),
encodeURIComponent,
surgioVersion: require('../../package.json').version,
});
}
}
4 changes: 2 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,9 @@ export const addProxyToSurgeRuleSet = (str: string, rule: string): string => {
};

export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray<RemoteSnippetConfig>): Promise<ReadonlyArray<RemoteSnippet>> => {
console.log('正在下载远程片段...');

function load(url: string): Promise<string> {
console.log(`正在下载远程片段: ${url}`);

return axios.get<string>(url, {
timeout: NETWORK_TIMEOUT,
responseType: 'text',
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/fs-extra": "^8.0.0",
"@types/hapi__joi": "^16.0.2",
"@types/inquirer": "^6.0.3",
"@types/koa": "^2.0.51",
"@types/lodash": "^4.14.144",
"@types/lru-cache": "^5.1.0",
"@types/mz": "^0.0.32",
Expand Down Expand Up @@ -70,6 +71,7 @@
},
"dependencies": {
"@hapi/joi": "^16.1.7",
"@types/koa-router": "^7.0.42",
"ali-oss": "^6.1.0",
"axios": "^0.19.0",
"chalk": "^2.4.2",
Expand All @@ -81,6 +83,8 @@
"fs-extra": "^8.1.0",
"get-port": "^5.0.0",
"inquirer": "^6.5.0",
"koa": "^2.11.0",
"koa-router": "^7.4.0",
"lodash": "^4.17.12",
"lru-cache": "^5.1.1",
"merge-stream": "^2.0.0",
Expand Down
Loading

0 comments on commit bc4e9fc

Please sign in to comment.