( buildArtifacts: ArtifactBuilder = defaultArtifactBuilder, )
| 23 | : 500; |
| 24 | |
| 25 | export const createContainerApp = ( |
| 26 | buildArtifacts: ArtifactBuilder = defaultArtifactBuilder, |
| 27 | ) => { |
| 28 | const app = new Hono(); |
| 29 | let buildStarted = false; |
| 30 | |
| 31 | app.get('/health', (c) => c.json({ status: 200 })); |
| 32 | |
| 33 | app.post('/build-version', async (c) => { |
| 34 | let payload: BuildVersionRequest | undefined; |
| 35 | |
| 36 | try { |
| 37 | if (buildStarted) { |
| 38 | throw new HTTPException(409, { |
| 39 | message: 'This container instance already accepted a build.', |
| 40 | }); |
| 41 | } |
| 42 | buildStarted = true; |
| 43 | |
| 44 | payload = await c.req.json<BuildVersionRequest>(); |
| 45 | if (!payload) { |
| 46 | throw new Error('Invalid request payload. Expected JSON body.'); |
| 47 | } |
| 48 | |
| 49 | const buildKey = getBuildKey(payload); |
| 50 | const startedAt = Date.now(); |
| 51 | console.log(`[container] starting ${payload.mode} build ${buildKey}`); |
| 52 | const artifactCount = await buildArtifacts(payload); |
| 53 | const durationMs = Date.now() - startedAt; |
| 54 | |
| 55 | console.log( |
| 56 | `[container] finished ${payload.mode} build ${buildKey} - ${artifactCount} artifacts in ${durationMs}ms`, |
| 57 | ); |
| 58 | |
| 59 | return c.json({ state: 'ready', buildKey }); |
| 60 | } catch (error) { |
| 61 | logger.error( |
| 62 | { err: error, buildKey: payload ? getBuildKey(payload) : undefined }, |
| 63 | '[container] build failed', |
| 64 | ); |
| 65 | |
| 66 | return c.json( |
| 67 | { |
| 68 | state: 'failed', |
| 69 | buildKey: payload ? getBuildKey(payload) : 'unknown', |
| 70 | error: error instanceof Error ? error.message : String(error), |
| 71 | }, |
| 72 | errorStatus(error), |
| 73 | ); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | app.notFound((c) => { |
| 78 | const url = new URL(c.req.url); |
| 79 | console.warn( |
| 80 | `[container] unmatched request ${c.req.method} ${url.pathname}${url.search}`, |
| 81 | ); |
| 82 | return c.json({ status: 404, error: 'Not Found.' }, 404); |
no test coverage detected