-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support aliyun serverless service
- Loading branch information
Showing
3 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters