Skip to content

Commit

Permalink
Reduce app boilerplate in API templates (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Jul 13, 2021
1 parent 5b09594 commit 3e1005e
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-needles-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**template/\*-rest-api:** Reduce app boilerplate
2 changes: 1 addition & 1 deletion template/express-rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build": "skuba build",
"format": "skuba format",
"lint": "skuba lint",
"start": "ENVIRONMENT=local skuba start",
"start": "ENVIRONMENT=local skuba start --port <%- port %>",
"start:debug": "yarn start --inspect-brk",
"test": "skuba test",
"test:ci": "skuba test --coverage",
Expand Down
5 changes: 1 addition & 4 deletions template/express-rest-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import express from 'express';

import { healthCheckHandler } from './api/healthCheck';
import { smokeTestHandler } from './api/smokeTest';
import { config } from './config';

const app = express()
.get('/health', healthCheckHandler)
.get('/smoke', smokeTestHandler);

export default Object.assign(app, {
port: config.port,
});
export default app;
2 changes: 0 additions & 2 deletions template/express-rest-api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
logLevel: 'debug',
name: '<%- serviceName %>',
version: 'local',

port: Env.nonNegativeInteger('PORT', { default: Number('<%- port %>') }),
}),

test: () => ({
Expand Down
3 changes: 2 additions & 1 deletion template/express-rest-api/src/listen.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import './register';

import app from './app';
import { config } from './config';
import { rootLogger } from './framework/logging';

// If your application is deployed with more than 1 vCPU you can delete this
// file and use a clustering utility to run `lib/app`.

const listener = app.listen(app.port, () => {
const listener = app.listen(config.port, () => {
const address = listener.address();

if (typeof address === 'object' && address) {
Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"build": "skuba build",
"format": "skuba format",
"lint": "skuba lint",
"start": "ENVIRONMENT=local skuba start",
"start": "ENVIRONMENT=local skuba start --port <%- port %>",
"start:debug": "yarn start --inspect-brk",
"test": "skuba test",
"test:ci": "skuba test --coverage",
Expand Down
6 changes: 2 additions & 4 deletions template/koa-rest-api/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import app from './app';
const agent = agentFromApp(app);

describe('app', () => {
it('exports props for skuba start', () => {
expect(app).toHaveProperty('callback');
expect(app).toHaveProperty('port');
});
it('exports callback for skuba start', () =>
expect(app).toHaveProperty('callback'));

it('has a happy health check', () => agent.get('/health').expect(200, ''));

Expand Down
5 changes: 1 addition & 4 deletions template/koa-rest-api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import './register';

import { router } from './api';
import { config } from './config';
import { createApp } from './framework/server';

const app = createApp(router.allowedMethods(), router.routes());

export default Object.assign(app, {
port: config.port,
});
export default app;
2 changes: 0 additions & 2 deletions template/koa-rest-api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
logLevel: 'debug',
name: '<%- serviceName %>',
version: 'local',

port: Env.nonNegativeInteger('PORT', { default: Number('<%- port %>') }),
}),

test: () => ({
Expand Down
3 changes: 2 additions & 1 deletion template/koa-rest-api/src/listen.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import './register';

import app from './app';
import { config } from './config';
import { rootLogger } from './framework/logging';

// This implements a minimal version of `koa-cluster`'s interface
// If your application is deployed with more than 1 vCPU you can delete this
// file and use `koa-cluster` to run `lib/app`.

const listener = app.listen(app.port, () => {
const listener = app.listen(config.port, () => {
const address = listener.address();

if (typeof address === 'object' && address) {
Expand Down

0 comments on commit 3e1005e

Please sign in to comment.