Skip to content

Commit

Permalink
feat: support aliyun serverless service
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 14, 2019
1 parent ff691fa commit 0108097
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
45 changes: 45 additions & 0 deletions lib/gateway/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'path';
import { loadConfig } from '../utils';
import { Server } from './server';
let server;

export function initializer(context, callback): void {
const cwd = process.cwd();
const configFile = path.join(cwd, '/surgio.conf.js');
const config = loadConfig(cwd, configFile);

server = new Server(config, context);

server.init()
.then(() => {
callback(null, '');
})
.catch(err => {
callback(err, '');
});
}

export function handler(request, response): void {
const { url } = request;
const artifactName = path.basename(url);

if (!artifactName) {
server.notFound(response);
return;
}

server.getArtifact(artifactName)
.then(result => {
if (result) {
response.setStatusCode(200);
response.setHeader('content-type', 'text/plain; charset=utf-8');
response.setHeader('cache-control', 'private, no-cache, no-store');
response.send(result);
} else {
server.notFound(response);
}
})
.catch(err => {
server.errorHandler(err, response);
});
}
51 changes: 51 additions & 0 deletions lib/gateway/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ArtifactConfig, CommandConfig, RemoteSnippet } from '../types';
import { loadRemoteSnippetList } from '../utils';
import { generate } from '../generate';

export class Server {
public remoteSnippetList: ReadonlyArray<RemoteSnippet>;
public artifactList: ReadonlyArray<ArtifactConfig>;
private readonly config: CommandConfig;
private readonly context: Record<any, any>;

constructor(config: CommandConfig, context: Record<any, any>) {
this.config = config;
this.context = context;
}

public async init(): Promise<void> {
const config = this.config;
const remoteSnippetsConfig = config.remoteSnippets || [];

this.artifactList = config.artifacts;
this.remoteSnippetList = await loadRemoteSnippetList(remoteSnippetsConfig);
}

public async getArtifact(artifactName: string): Promise<string> {
const target = this.artifactList.filter(item => item.name === artifactName);

if (!target.length) {
return undefined;
}

return await generate(this.config, target[0], this.remoteSnippetList);
}

public errorHandler(response: any, err: Error): void {
response.setStatusCode(500);
response.setHeader('content-type', 'text/html; charset=UTF-8');
response.send(
'<h1>Server Error</h1>' +
`<h2>${err.name}: ${err.message}</h2>` +
`<pre>${err.stack}</pre>`
);
}

public notFound(response: any): void {
response.setStatusCode(404);
response.setHeader('content-type', 'text/html; charset=UTF-8');
response.send(
'<h1>Not Found</h1>'
);
}
}
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3649,11 +3649,6 @@ cosmiconfig@^5.0.0, cosmiconfig@^5.2.1:
js-yaml "^3.13.1"
parse-json "^4.0.0"

country-code-emoji@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/country-code-emoji/-/country-code-emoji-1.0.0.tgz#7c77791839c9e9921beec08ef080caa7cfb8b40c"
integrity sha512-fBM5A49oZkOxOVb0bx7q7Hanlfh8e3z/r6/ZnFhbL57JXGIgWPC2HYrjXEyiGML7OFftDV/WfAlJdDkoAbj1Rg==

cp-file@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/cp-file/-/cp-file-6.2.0.tgz#40d5ea4a1def2a9acdd07ba5c0b0246ef73dc10d"
Expand Down Expand Up @@ -9877,7 +9872,7 @@ proxy-addr@~2.0.5:
forwarded "~0.1.2"
ipaddr.js "1.9.0"

proxy-agent@*, proxy-agent@^3.1.0:
proxy-agent@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/proxy-agent/-/proxy-agent-3.1.0.tgz#3cf86ee911c94874de4359f37efd9de25157c113"
integrity sha512-IkbZL4ClW3wwBL/ABFD2zJ8iP84CY0uKMvBPk/OceQe/cEjrxzN1pMHsLwhbzUoRhG9QbSxYC+Z7LBkTiBNvrA==
Expand Down

0 comments on commit 0108097

Please sign in to comment.