Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 3ffdc2b

Browse files
committed
cleanup
1 parent 5c45c80 commit 3ffdc2b

File tree

24 files changed

+105
-72
lines changed

24 files changed

+105
-72
lines changed

Dockerfile

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
# Prepare
2-
3-
FROM node:8.9.4-alpine AS base
4-
5-
RUN mkdir -p /opt/app
2+
FROM node:8.11.0-alpine AS base
63

74
WORKDIR /opt/app
85

96
# Install
10-
117
FROM base AS dependencies
128

13-
COPY package.json .
14-
COPY yarn.lock .
9+
COPY package.json yarn.lock ./
1510

1611
RUN yarn --production --ignore-scripts --prefer-offline
1712
RUN cp -R node_modules production_node_modules
13+
1814
RUN yarn
1915

2016
# Build
21-
2217
FROM base AS build
2318

24-
COPY --from=dependencies /opt/app/node_modules /opt/node_modules
25-
ENV PATH /opt/node_modules/.bin:$PATH
19+
COPY --from=dependencies /opt/app/node_modules ./node_modules
20+
21+
ENV CI true
22+
COPY tsconfig.json tslint.json ./
2623

27-
COPY . /opt/app
24+
COPY src ./src
25+
26+
COPY package.json .
2827

2928
RUN yarn build
3029

31-
# Run
30+
# Test
31+
COPY jest.config.js .
32+
COPY jest ./jest
3233

34+
RUN yarn test
35+
36+
# Run
3337
FROM base AS run
3438

3539
COPY --from=dependencies /opt/app/production_node_modules ./node_modules
36-
COPY --from=build /opt/app .
40+
COPY --from=dependencies /opt/app/package.json .
41+
COPY --from=build /opt/app/build ./build
3742

3843
ENV PORT 3001
3944
ENV NODE_ENV production
4045

41-
HEALTHCHECK CMD curl -fs http://$HOST:$PORT/healthz || exit 1
42-
4346
EXPOSE $PORT
4447

4548
USER node

Dockerfile.dev

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Prepare
22

3-
FROM node:8.9.4-alpine
4-
5-
RUN mkdir -p /opt/app
3+
FROM node:8.11.0-alpine
64

75
WORKDIR /opt/app
86

97
# Install
108

11-
COPY package.json .
12-
COPY yarn.lock .
9+
COPY package.json yarn.lock ./
1310

1411
RUN yarn
1512

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
context: .
88
dockerfile: Dockerfile.dev
99
volumes:
10-
- ./server:/opt/app/server
10+
- ./src:/opt/app/src
1111
ports:
1212
- 3001:3001
1313
environment:
14-
- SHELL=/bin/sh
14+
- SHELL=/bin/sh

jest/__setup__/env-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// read env to be accessible on every test (used in integration tests)
2-
import env from '../../server/env';
2+
import env from '../../src/env';
33
process.env = env;

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"test:integration": "npm run validate:env && jest --config jest/integration.config.js",
1111
"test:unit": "npm run validate:env && jest --config jest/unit.config.js",
1212
"test:all": "npm run validate:env && jest",
13-
"lint": "tslint -t stylish 'server/**/*.ts'",
14-
"lint:watch": "chokidar \"server/**/*.ts\" -c \"tslint -t stylish --force '{path}'\"",
15-
"validate:env": "tsc server/env.ts --lib esnext --esModuleInterop --outDir ./build/ && node ./build/env.js",
13+
"lint": "tslint -t stylish 'src/**/*.ts'",
14+
"lint:watch": "chokidar \"src/**/*.ts\" -c \"tslint -t stylish --force '{path}'\"",
15+
"validate:env": "tsc src/env.ts --lib esnext --esModuleInterop --outDir ./build/ && node ./build/env.js",
1616
"clean": "rimraf build",
1717
"clean:graphql": "rimraf build/**/*.{gql,graphql,graphqls}",
18-
"copy:graphql": "cpx \"server/**/*.{gql,graphql,graphqls}\" build",
19-
"copy:graphql:watch": "chokidar \"./server/**/*.{gql,graphql,graphqls}\" -c \"npm run clean:graphql && npm run copy:graphql && touch server/server.ts\"",
18+
"copy:graphql": "cpx \"src/**/*.{gql,graphql,graphqls}\" build",
19+
"copy:graphql:watch": "chokidar \"./src/**/*.{gql,graphql,graphqls}\" -c \"npm run clean:graphql && npm run copy:graphql && touch src/server.ts\"",
2020
"docker-mount": "docker-share mount -t"
2121
},
2222
"engines": {

server/env.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

server/paths.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

server/app.ts renamed to src/app.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ import koaMiddleware from 'graphql-voyager/middleware/koa';
99
const koaPlayground = require('graphql-playground-middleware-koa').default;
1010
import { promisifyAll } from 'bluebird';
1111
import { Client } from 'flashheart';
12-
import { graphqlPath, graphiqlPath, voyagerPath, playgroundPath } from './paths';
1312
import schema from './graphql/schema';
1413
import logger from './logger';
14+
import { entryPoint } from './entrypoint';
1515

1616
promisifyAll(Client.prototype);
1717

1818
const app = new koa();
1919

2020
const router = new koaRouter();
2121

22+
// Entry Point
23+
router.get('/', entryPoint);
24+
2225
// CORS?
2326
if (process.env.CORS) {
2427
app.use(koaConvert(koaCors()));
@@ -35,13 +38,13 @@ const graphqlMiddleware = graphqlKoa({
3538
},
3639
});
3740

38-
router.post(`/${graphqlPath}*`, koaBodyparser(), graphqlMiddleware);
39-
router.get(`/${graphqlPath}*`, graphqlMiddleware);
41+
router.post(`/${process.env.GRAPHQL_ENDPOINT}*`, koaBodyparser(), graphqlMiddleware);
42+
router.get(`/${process.env.GRAPHQL_ENDPOINT}*`, graphqlMiddleware);
4043

4144
// GraphQL Voyager?
4245
if (process.env.VOYAGER) {
43-
router.all(`/${voyagerPath}`, koaMiddleware({
44-
endpointUrl: `/${graphqlPath}`,
46+
router.all(`/${process.env.VOYAGER_ENDPOINT}`, koaMiddleware({
47+
endpointUrl: `/${process.env.GRAPHQL_ENDPOINT}`,
4548
displayOptions: {
4649
sortByAlphabet: true,
4750
},
@@ -50,21 +53,21 @@ if (process.env.VOYAGER) {
5053

5154
// GraphiQL?
5255
if (process.env.GRAPHIQL) {
53-
router.get(`/${graphiqlPath}`, graphiqlKoa({ endpointURL: `/${graphqlPath}` }));
56+
router.get(`/${process.env.GRAPHIQL_ENDPOINT}`, graphiqlKoa({ endpointURL: `/${process.env.GRAPHQL_ENDPOINT}` }));
5457
}
5558

5659
// GraphQL Playground?
5760
if (process.env.PLAYGROUND) {
5861
router.all(
59-
`/${playgroundPath}`,
62+
`/${process.env.PLAYGROUND_ENDPOINT}`,
6063
koaPlayground({
61-
endpoint: `/${graphqlPath}`,
64+
endpoint: `/${process.env.GRAPHQL_ENDPOINT}`,
6265
}),
6366
);
6467
}
6568

6669
// Koa Heartbeat
67-
app.use(koaHeartbeat({ path: '/healthz', body: 'up!' }));
70+
app.use(koaHeartbeat({ path: `/${process.env.LIVENESS_ENDPOINT}`, body: 'ok' }));
6871

6972
app.use(router.routes());
7073
app.use(router.allowedMethods());

server/connectors/joke.ts renamed to src/connectors/joke.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Joke, JokeCategoryValues } from '../graphql/types/jokes';
55
const http = createClient({ logger });
66

77
const getRandomJokeByCategory = async (category: JokeCategoryValues): Promise<any> => {
8-
const uri = `${process.env.JOKE_SERVICE_URI}/jokes/random${category ? `?limitTo=[${category.toLowerCase()}]` : ''}`;
8+
const uri = `${process.env.JOKE_SERVICE_URL}/jokes/random${category ? `?limitTo=[${category.toLowerCase()}]` : ''}`;
99

1010
const { value } = await http.getAsync(uri);
1111

src/entrypoint.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import { pickBy, identity, isEmpty } from 'lodash';
4+
5+
const endpointsConfig = {
6+
graphql: `${process.env.SELF_URL}/${process.env.GRAPHQL_ENDPOINT}`,
7+
liveness: `${process.env.SELF_URL}/${process.env.LIVENESS_ENDPOINT}`,
8+
graphiql: process.env.GRAPHIQL && `${process.env.SELF_URL}/${process.env.GRAPHIQL_ENDPOINT}`,
9+
playground: process.env.PLAYGROUND && `${process.env.SELF_URL}/${process.env.PLAYGROUND_ENDPOINT}`,
10+
voyager: process.env.VOYAGER && `${process.env.SELF_URL}/${process.env.VOYAGER_ENDPOINT}`,
11+
};
12+
13+
const packageJsonPath = path.resolve('package.json');
14+
let packageJson: any = {};
15+
16+
if (fs.existsSync(packageJsonPath)) {
17+
packageJson = require(packageJsonPath);
18+
}
19+
20+
const entryPointConfig: any = {
21+
name: packageJson.name,
22+
description: packageJson.description,
23+
repository: packageJson.repository,
24+
endpoints: pickBy(endpointsConfig, identity),
25+
};
26+
27+
28+
const entryPoint = async (ctx) => {
29+
ctx.body = entryPointConfig;
30+
};
31+
32+
export {
33+
entryPoint,
34+
};

0 commit comments

Comments
 (0)