UNPKG

741 BPlain TextView Raw
1import { Router, Request, Response, json } from 'express'
2
3const router = Router()
4
5import DockerCompiler from './DockerCompiler'
6import CachingUrlFetcher from './CachingUrlFetcher'
7
8const compiler = new DockerCompiler(new CachingUrlFetcher())
9
10router.use(json())
11
12/**
13 * Run a method of `DockerCompiler`
14 * @param method The method to run e.g `compile`, `build`
15 */
16function run (method: string) {
17 return async (req: Request, res: Response) => {
18 try {
19 // @ts-ignore
20 const node = await compiler[method](req.body)
21 res.status(200).json(node)
22 } catch (error) {
23 res.status(500).write(error.stack)
24 }
25 }
26}
27
28router.put('/compile', run('compile'))
29router.put('/compile', run('execute'))
30
31export default router