-
Notifications
You must be signed in to change notification settings - Fork 23
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
17 changed files
with
306 additions
and
304 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
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 |
---|---|---|
@@ -1,48 +1,23 @@ | ||
import { hostname } from 'os'; | ||
|
||
import express, { NextFunction, Request, Response } from 'express'; | ||
|
||
import api from './api'; | ||
import { allApplications } from './constants'; | ||
import AppError from './utils/appError'; | ||
|
||
const app = express(); | ||
const host = hostname(); | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
app.get('/readiness', (_req: Request, res: Response) => res.sendStatus(200)); | ||
app.get('/validate', api.validate); | ||
app.get('/api/account/deploy-enabled', api.validate); | ||
|
||
app.get(`/${host}/:appName/:version/call/:name`, api.callFunction); | ||
app.post(`/${host}/:appName/:version/call/:name`, api.callFunction); | ||
app.get( | ||
`/${host}/:appName/:version/static/.metacall/faas/apps/:app/:file`, | ||
api.serveStatic | ||
); | ||
|
||
app.post('/api/package/create', api.uploadPackage); | ||
app.post('/api/repository/add', api.fetchFilesFromRepo); | ||
|
||
app.post('/api/repository/branchlist', api.fetchBranchList); | ||
app.post('/api/repository/filelist', api.fetchFileList); | ||
app.post('/api/deploy/logs', api.logs); | ||
|
||
app.post('/api/deploy/create', api.deploy); | ||
|
||
app.get('/api/inspect', (_req, res) => { | ||
res.send(Object.values(allApplications)); | ||
}); | ||
|
||
app.post('/api/deploy/delete', api.deployDelete); | ||
|
||
// For all the additional unimplemented routes | ||
app.all('*', (req: Request, res: Response, next: NextFunction) => { | ||
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404)); | ||
}); | ||
|
||
app.use(api.globalError); | ||
|
||
export default app; | ||
import { Deployment, MetaCallJSON } from '@metacall/protocol/deployment'; | ||
import { ChildProcess } from 'child_process'; | ||
|
||
export interface Resource { | ||
id: string; | ||
path: string; | ||
jsons: MetaCallJSON[]; | ||
runners?: string[]; | ||
type?: string; | ||
blob?: string; | ||
} | ||
|
||
export class Application { | ||
public resource?: Promise<Resource>; | ||
public proc?: ChildProcess; | ||
public deployment?: Deployment; | ||
|
||
public kill(): void { | ||
this.proc?.kill(); | ||
} | ||
} | ||
|
||
export const Applications: Record<string, Application> = {}; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,62 +1,72 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { Applications } from '../app'; | ||
import AppError from '../utils/appError'; | ||
import { | ||
Processes, | ||
WorkerMessageType, | ||
WorkerMessageUnknown | ||
} from '../worker/master'; | ||
import { WorkerMessageType, WorkerMessageUnknown } from '../worker/protocol'; | ||
|
||
export const callFunction = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Response | void => { | ||
if (!(req.params && req.params.name)) | ||
next( | ||
if (!req.params?.suffix) { | ||
return next( | ||
new AppError('A the deployment name (suffix) is required.', 404) | ||
); | ||
} | ||
|
||
if (!req.params?.func) { | ||
return next( | ||
new AppError( | ||
'A function name is required in the path; i.e: /call/sum.', | ||
404 | ||
) | ||
); | ||
} | ||
|
||
const { appName: app, name } = req.params; | ||
const { suffix, func } = req.params; | ||
const args = Object.values(req.body); | ||
const application = Applications[suffix]; | ||
|
||
if (!(app in Processes)) { | ||
// Check if the application exists and it is running | ||
if (!application?.proc) { | ||
return res | ||
.status(404) | ||
.send( | ||
`Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you can call its functions.` | ||
`Oops! It looks like the application '${suffix}' has not been deployed yet. Please deploy it before you can call its functions.` | ||
); | ||
} | ||
|
||
let responseSent = false; // Flag to track if response has been sent | ||
let errorCame = false; | ||
|
||
Processes[app].send({ | ||
type: WorkerMessageType.Invoke, | ||
data: { | ||
name, | ||
args | ||
} | ||
}); | ||
new Promise((resolve, reject) => { | ||
application.proc?.send({ | ||
type: WorkerMessageType.Invoke, | ||
data: { | ||
name: func, | ||
args | ||
} | ||
}); | ||
|
||
Processes[app].on('message', (message: WorkerMessageUnknown) => { | ||
if (!responseSent) { | ||
// Check if response has already been sent | ||
application.proc?.on('message', (message: WorkerMessageUnknown) => { | ||
if (message.type === WorkerMessageType.InvokeResult) { | ||
responseSent = true; // Set flag to true to indicate response has been sent | ||
return res.send(JSON.stringify(message.data)); | ||
} else { | ||
errorCame = true; | ||
resolve(JSON.stringify(message.data)); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// Default response in case the 'message' event is not triggered | ||
if (!responseSent && errorCame) { | ||
responseSent = true; // Set flag to true to indicate response has been sent | ||
errorCame = false; | ||
return res.send('Function calling error'); | ||
} | ||
application.proc?.on('exit', code => { | ||
// The application may have been ended unexpectedly, | ||
// probably segmentation fault (exit code 139 in Linux) | ||
reject( | ||
JSON.stringify({ | ||
error: `Deployment '${suffix}' process exited with code: ${ | ||
code || 'unknown' | ||
}` | ||
}) | ||
); | ||
}); | ||
}) | ||
.then(data => { | ||
res.send(data); | ||
}) | ||
.catch(error => { | ||
res.status(500).send(error); | ||
}); | ||
}; |
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
Oops, something went wrong.