-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
94 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 |
---|---|---|
|
@@ -20,7 +20,8 @@ | |
"dist", | ||
"fc", | ||
"scf", | ||
"aws" | ||
"aws", | ||
"wrapper.ejs" | ||
], | ||
"scripts": { | ||
"build": "midway-bin build -c", | ||
|
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
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
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
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
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
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,64 @@ | ||
const { FaaSStarter } = require('<%=faasModName %>'); | ||
const { asyncWrapper, start } = require('<%=starter %>'); | ||
const picomatch = require('picomatch'); | ||
<% layerDeps.forEach(function(layer){ %>const <%=layer.name%> = require('<%=layer.path%>'); | ||
<% }); %> | ||
|
||
let starter; | ||
let runtime; | ||
let inited = false; | ||
|
||
|
||
const initializeMethod = async (initializeContext = {}) => { | ||
runtime = await start({ | ||
layers: [<%= layers.join(", ") %>], | ||
getHandler: getHandler | ||
}); | ||
starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime }); | ||
<% loadDirectory.forEach(function(dirName){ %> | ||
starter.loader.loadDirectory({ baseDir: '<%=dirName%>'});<% }) %> | ||
await starter.start(); | ||
inited = true; | ||
}; | ||
|
||
const getHandler = (hanlderName) => { | ||
<% handlers.forEach(function(handlerData){ %> | ||
if (hanlderName === '<%=handlerData.name%>') { | ||
return <% if (handlerData.handler) { | ||
%> starter.handleInvokeWrapper('<%=handlerData.handler%>'); <% } else { | ||
%> async (ctx) => { | ||
const allHandlers = <%-JSON.stringify(handlerData.handlers)%>; | ||
let handler = null; | ||
let ctxPath = ctx && ctx.path || ''; | ||
if (ctxPath) { | ||
handler = allHandlers.find(handler => { | ||
return picomatch.isMatch(ctxPath, handler.router) | ||
}); | ||
} | ||
if (handler) { | ||
return starter.handleInvokeWrapper(handler.handler)(ctx); | ||
} | ||
ctx.status = 404; | ||
ctx.set('Content-Type', 'text/html'); | ||
return '<h1>404 Page Not Found</h1><hr />Request path: ' + ctxPath + '<hr /><div style="font-size: 12px;color: #999999;">Powered by <a href="https://github.com/midwayjs/midway">Midway Serverless</a></div>'; | ||
}; <% } %> | ||
} | ||
<% }); %> | ||
} | ||
|
||
|
||
exports.<%=initializer%> = asyncWrapper(async (...args) => { | ||
await initializeMethod(...args); | ||
}); | ||
|
||
<% handlers.forEach(function(handlerData){ %> | ||
exports.<%=handlerData.name%> = asyncWrapper(async (...args) => { | ||
if (!inited) { | ||
await initializeMethod(); | ||
} | ||
const handler = getHandler('<%=handlerData.name%>'); | ||
return runtime.asyncEvent(handler)(...args); | ||
}); | ||
<% }); %> |