Skip to content

Commit

Permalink
feat(config): switch from individual app env vars
Browse files Browse the repository at this point in the history
Switch from using individual `WARTHOG_APP_*` variables in favor of
a single `WARTHOG_API_BASE_URL`.

BREAKING CHANGE
  • Loading branch information
goldcaddy77 committed Sep 15, 2021
1 parent 6a275ab commit 3dbbfe7
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 83 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Warthog is now on version 3.0! There were a few breaking changes that you should
<summary>Expand for Breaking change details</summary>
<p>

### Deprecated individual `WARTHOG_APP_*` variables in favor of `WARTHOG_API_BASE_URL`

It's a pain to have to manage 3 separate environment variables for setting the base API URL. We still support this, but prefer the new `WARTHOG_API_BASE_URL` and will use it first if it's present when spinning up a server.

### BaseModel has been overhauled

- SchemaGenerator no longer generates a Binding by default, must now pass a param in and pass `--binding` in the CLI
Expand Down Expand Up @@ -250,9 +254,7 @@ Almost all config in Warthog is driven by environment variables. The following i

| variable | value | default |
| ----------------------------- | -------------------------------------------------------- | ------------------------- |
| WARTHOG_APP_HOST | App server host | _none_ |
| WARTHOG_APP_PORT | App server port | 4000 |
| WARTHOG_APP_PROTOCOL | App server protocol | DEV: http, PROD: https |
| WARTHOG_API_BASE_URL | Api base url | _none_ |
| WARTHOG_AUTO_GENERATE_FILES | Auto-generate files | DEV: true, PROD: false |
| WARTHOG_AUTO_OPEN_PLAYGROUND | Open playground on server start | DEV: true, PROD: false |
| WARTHOG_CLI_GENERATE_PATH | Where should CLI generate files | ./src |
Expand All @@ -274,6 +276,15 @@ Almost all config in Warthog is driven by environment variables. The following i
| WARTHOG_SUBSCRIPTIONS | Should we enable subscriptions and open a websocket port | false |
| WARTHOG_VALIDATE_RESOLVERS | TypeGraphQL validation enabled? | false |

### Removed Environment Variables

All of the `WARTHOG_APP_*` environment variables will still work, but it is strongly recommended you use the single variable `WARTHOG_API_BASE_URL`

| variable | value |
| WARTHOG_APP_HOST | App server host |
| WARTHOG_APP_PORT | App server port |
| WARTHOG_APP_PROTOCOL | App server protocol |

## Field/Column Decorators

All of the auto-generation magic comes from the decorators added to the attributes on your models. Warthog decorators are convenient wrappers around TypeORM decorators (to create DB schema) and TypeGraphQL (to create GraphQL schema). You can find a list of decorators available in the [src/decorators](./src/decorators) folder. Most of these are also used in the [examples](./examples) folder in this project.
Expand Down
3 changes: 1 addition & 2 deletions examples/01-simple-model/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-1
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/02-complex-example/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_URL=postgres://postgres:@localhost:5432/warthog-example-2
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
Expand Down
3 changes: 1 addition & 2 deletions examples/03-one-to-many-relationship/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-3
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/04-many-to-many-relationship/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-4
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/05-migrations/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-5
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/06-base-service/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-6
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/07-feature-flags/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-feature-flag
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/08-performance/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-8
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
4 changes: 1 addition & 3 deletions examples/09-production/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ WARTHOG_DB_SUBSCRIBERS=dist/examples/09-production/src/**/*.model.js
WARTHOG_RESOLVERS_PATH=dist/examples/09-production/src/**/*.resolver.js
WARTHOG_DB_MIGRATIONS=dist/examples/09-production/db/migrations/**/*.js
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_APP_PROTOCOL=http
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-9
WARTHOG_DB_HOST=localhost
WARTHOG_DB_PASSWORD=
Expand Down
4 changes: 1 addition & 3 deletions examples/09-production/env.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
shared: &shared
PGUSER: postgres
WARTHOG_APP_HOST: localhost
WARTHOG_APP_PORT: 4100
WARTHOG_APP_PROTOCOL: http
WARTHOG_API_BASE_URL: http://localhost:4100
WARTHOG_DB_DATABASE: warthog-example-9
WARTHOG_DB_HOST: localhost
WARTHOG_DB_PASSWORD:
Expand Down
3 changes: 1 addition & 2 deletions examples/10-subscriptions/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-10-subscriptions
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/11-transactions/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-11-transactions
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions examples/14-base-service-v2/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=development
PGUSER=postgres
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-14-base-service-v2
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
6 changes: 1 addition & 5 deletions src/cli/commands/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ export default {
run: async (toolbox: WarthogGluegunToolbox) => {
const config: any = toolbox.config.load();

const host = config.get('APP_HOST');
const port = config.get('APP_PORT');
const url = `http://${host}:${port}/playground`;

return open(url, { wait: false });
return open(config.getApiUrl('/playground'), { wait: false });
}
};
3 changes: 1 addition & 2 deletions src/cli/templates/new/_env.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ PGUSER=postgres
NODE_ENV=development
WARTHOG_AUTO_OPEN_PLAYGROUND=false
WARTHOG_AUTO_GENERATE_FILES=false
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4000
WARTHOG_DB_DATABASE=<%= props.kebabName %>
WARTHOG_DB_HOST=localhost
WARTHOG_DB_LOGGING=all
Expand Down
14 changes: 6 additions & 8 deletions src/cli/templates/new/env.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ test:
PGUSER: postgres # Otherwise tries to use system user
WARTHOG_DB_LOGGING: none

local_app: &local_app
WARTHOG_APP_HOST: localhost
WARTHOG_APP_PORT: 4100
local_api: &local_api
WARTHOG_API_BASE_URL: http://localhost:4000

build_env: &build_env
WARTHOG_DB_ENTITIES: dist/src/**/*.model.js
Expand Down Expand Up @@ -38,26 +37,25 @@ prod_db: &prod_db

development:
<<: *local
<<: *local_app
<<: *local_api
<<: *local_db

development:build:
<<: *local
<<: *local_app
<<: *local_api
<<: *build_env
<<: *local_db

development:prod-like:
WARTHOG_APP_PROTOCOL: http
<<: *local
<<: *local_app
<<: *local_api
<<: *build_env
<<: *prod_db

production:
NODE_ENV: production
WARTHOG_APP_HOST: localhost
WARTHOG_APP_PORT: ${env:PORT}
WARTHOG_API_BASE_URL: ${env:WARTHOG_API_BASE_URL}
WARTHOG_INTROSPECTION: true
WARTHOG_PLAYGROUND: true
<<: *prod_db
Expand Down
3 changes: 1 addition & 2 deletions src/core/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { clearConfig } from '../test/server-vars';

import { Config } from './config';

describe('Config', () => {
Expand All @@ -14,7 +13,7 @@ describe('Config', () => {
try {
new Config({ configSearchPath: __dirname });
} catch (error) {
expect(error.message).toContain('WARTHOG_APP_HOST is required');
expect(error.message).toContain('WARTHOG_DB_HOST is required');
}
});
});
Expand Down
36 changes: 27 additions & 9 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Config {
WARTHOG_DB_CONNECTION: 'postgres',
WARTHOG_ROOT_FOLDER: this.PROJECT_ROOT,
WARTHOG_ALLOW_OPTIONAL_ID_ON_CREATE: 'false',
WARTHOG_APP_PROTOCOL: 'https',
WARTHOG_APP_PROTOCOL: 'https', // DEPRECATED
WARTHOG_AUTO_GENERATE_FILES: 'false',
WARTHOG_AUTO_OPEN_PLAYGROUND: 'false',
WARTHOG_INTROSPECTION: 'true',
Expand Down Expand Up @@ -98,9 +98,10 @@ export class Config {
};

this.devDefaults = {
WARTHOG_APP_HOST: 'localhost',
WARTHOG_APP_PORT: '4000',
WARTHOG_APP_PROTOCOL: 'http',
WARTHOG_API_BASE_URL: 'http://localhost:4000',
WARTHOG_APP_HOST: 'localhost', // DEPRECATED
WARTHOG_APP_PORT: '4000', // DEPRECATED
WARTHOG_APP_PROTOCOL: 'http', // DEPRECATED
WARTHOG_AUTO_GENERATE_FILES: 'true',
WARTHOG_AUTO_OPEN_PLAYGROUND: 'true',
WARTHOG_DB_HOST: 'localhost',
Expand All @@ -119,9 +120,10 @@ export class Config {
};

this.testDefaults = {
WARTHOG_APP_HOST: 'localhost',
WARTHOG_APP_PORT: '4000',
WARTHOG_APP_PROTOCOL: 'http',
WARTHOG_API_BASE_URL: 'http://localhost:4000',
WARTHOG_APP_HOST: 'localhost', // DEPRECATED
WARTHOG_APP_PORT: '4000', // DEPRECATED
WARTHOG_APP_PROTOCOL: 'http', // DEPRECATED
WARTHOG_AUTO_GENERATE_FILES: 'false',
WARTHOG_AUTO_OPEN_PLAYGROUND: 'false',
WARTHOG_DB_DATABASE: 'warthog-test',
Expand Down Expand Up @@ -194,8 +196,6 @@ export class Config {
debug('Config', this.config);

// Must be after config is set above
this.validateEntryExists('WARTHOG_APP_HOST');
this.validateEntryExists('WARTHOG_APP_PORT');
this.validateEntryExists('WARTHOG_GENERATED_FOLDER');
this.validateEntryExists('WARTHOG_DB_CONNECTION');
this.validateEntryExists('WARTHOG_DB_HOST');
Expand Down Expand Up @@ -259,6 +259,24 @@ export class Config {
return config;
}

public getApiBaseUrl() {
// Prefer the new API_BASE_URL variable
if (this.get('API_BASE_URL')) {
return this.get('API_BASE_URL');
}

// Continue to support passing variables as pieces (from v1 and v2)
return `${this.get('APP_PROTOCOL')}://${this.get('APP_HOST')}:${this.get('APP_PORT')}`;
}

public getApiUrl(path?: string) {
return new URL(path ?? '', this.getApiBaseUrl()).href;
}

public getApiPort() {
return new URL('', this.getApiBaseUrl()).port;
}

public get(key?: string) {
if (typeof key === 'undefined') {
return this.config;
Expand Down
20 changes: 3 additions & 17 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ export interface ServerOptions<T> {
autoGenerateFiles?: boolean;
context?: (request: Request) => object;
expressApp?: express.Application;
host?: string;
generatedFolder?: string;
logger?: Logger;
middlewares?: any[]; // TODO: fix
pubSub?: PubSubEngine | PubSubOptions;
openPlayground?: boolean;
port?: string | number;
resolversPath?: string[];
warthogImportPath?: string;
introspection?: boolean; // DEPRECATED
Expand All @@ -65,16 +63,6 @@ export class Server<C extends BaseContext> {
private appOptions: ServerOptions<C>,
private dbOptions: Partial<ConnectionOptions> = {}
) {
if (typeof this.appOptions.host !== 'undefined') {
process.env.WARTHOG_APP_HOST = this.appOptions.host;
// When we move to v2.0 we'll officially deprecate these config values in favor of ENV vars
// throw new Error(
// '`host` option has been removed, please set `WARTHOG_APP_HOST` environment variable instead'
// );
}
if (typeof this.appOptions.port !== 'undefined') {
process.env.WARTHOG_APP_PORT = this.appOptions.port.toString();
}
if (typeof this.appOptions.generatedFolder !== 'undefined') {
process.env.WARTHOG_GENERATED_FOLDER = this.appOptions.generatedFolder;
}
Expand Down Expand Up @@ -136,13 +124,11 @@ export class Server<C extends BaseContext> {
}

getServerUrl() {
return `${this.config.get('APP_PROTOCOL')}://${this.config.get('APP_HOST')}:${this.config.get(
'APP_PORT'
)}`;
return this.config.getApiUrl();
}

getGraphQLServerUrl() {
return `${this.getServerUrl()}/graphql`;
return this.config.getApiUrl('/graphql');
}

async getBinding(options: { origin?: string; token?: string } = {}): Promise<Binding> {
Expand Down Expand Up @@ -185,7 +171,7 @@ export class Server<C extends BaseContext> {
const keepAliveTimeout = Number(this.config.get('WARTHOG_KEEP_ALIVE_TIMEOUT_MS'));
const headersTimeout = Number(this.config.get('WARTHOG_HEADERS_TIMEOUT_MS'));

this.httpServer = this.expressApp.listen({ port: this.config.get('APP_PORT') }, () =>
this.httpServer = this.expressApp.listen({ port: this.config.getApiPort() }, () =>
this.logger.info(`🚀 Server ready at ${url}`)
);

Expand Down
3 changes: 1 addition & 2 deletions src/core/tests/dotenv-files/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ WARTHOG_C=ENV
WARTHOG_D=ENV

# required variables
WARTHOG_APP_HOST=localhost
WARTHOG_APP_PORT=4100
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-1
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
Expand Down
3 changes: 1 addition & 2 deletions src/core/tests/valid-config-file/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ describe('Config (valid file)', () => {

test('loads static config', async () => {
// Set some defaults or the constructor will blow up in CI
process.env.WARTHOG_APP_HOST = 'localhost';
process.env.WARTHOG_APP_PORT = '80';
process.env.WARTHOG_API_BASE_URL = 'http://localhost:4000';
process.env.WARTHOG_DB_HOST = 'localhost';
config = new Config({ configSearchPath: __dirname });

Expand Down
Loading

0 comments on commit 3dbbfe7

Please sign in to comment.