Skip to content

Commit

Permalink
feat: prepend ZION to all env variables
Browse files Browse the repository at this point in the history
As discussed in [github comment](#25 (comment)),
common practice is to prepend to all env variables with the name of the
project/application/library. This commit does that.
  • Loading branch information
relu91 committed Mar 27, 2023
1 parent 0e9ba71 commit 34ce729
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 83 deletions.
18 changes: 9 additions & 9 deletions .env
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
NODE_ENV=development
SERVER_PORT=3000
DB_HOST=localhost
DB_PORT=54310
DB_USER=postgres
DB_PASSWORD=postgres
DB_TYPE=postgres
JWT_SECRET=abc123
JWT_EXPIRES_IN=15m
ZION_NODE_ENV=development
ZION_SERVER_PORT=3000
ZION_DB_HOST=localhost
ZION_DB_PORT=54310
ZION_DB_USER=postgres
ZION_DB_PASSWORD=postgres
ZION_DB_TYPE=postgres
ZION_JWT_SECRET=abc123
ZION_JWT_EXPIRES_IN=15m
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ docker compose up
```
If you want to manually set up your database, you can edit the example [.env](.env) file:
```bash
NODE_ENV=development
SERVER_PORT=3000
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_DATABASE=postgres
JWT_SECRET=abc123
JWT_EXPIRES_IN=15m
ZION_NODE_ENV=development
ZION_SERVER_PORT=3000
ZION_DB_HOST=localhost
ZION_DB_PORT=5432
ZION_DB_USER=postgres
ZION_DB_PASSWORD=postgres
ZION_DB_DATABASE=postgres
ZION_JWT_SECRET=abc123
ZION_JWT_EXPIRES_IN=15m
```
### Clone and npm
If you want to start Zion in a development mode, you can clone the repository and run the following command:
Expand All @@ -86,9 +86,9 @@ services:
database:
image: postgres:14.3-alpine
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_DATABASE}
- POSTGRES_USER=${ZION_DB_USER}
- POSTGRES_PASSWORD=${ZION_DB_PASSWORD}
- POSTGRES_DB=${ZION_DB_DATABASE}
container_name: 'zion-postgres-testing'
ports:
- '54310:5432'
Expand All @@ -98,19 +98,19 @@ services:
- database
entrypoint: ['sh', '-c','npm run db:migrate:latest && node dist/src/main.js']
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- ${ZION_SERVER_PORT}:${ZION_SERVER_PORT}
container_name: zion
environment:
- NODE_ENV
- SERVER_PORT
- ZION_NODE_ENV
- ZION_SERVER_PORT
# Using task name as explained in https://github.com/vaimee/zion/issues/11#issuecomment-1434457337
- DB_HOST=tasks.database
- DB_PORT
- DB_USER
- DB_PASSWORD
- DB_DATABASE
- JWT_SECRET
- JWT_EXPIRES_IN
- ZION_DB_HOST=tasks.database
- ZION_DB_PORT
- ZION_DB_USER
- ZION_DB_PASSWORD
- ZION_DB_DATABASE
- ZION_JWT_SECRET
- ZION_JWT_EXPIRES_IN
```


Expand Down
26 changes: 13 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ services:
database:
image: postgres:14.3-alpine
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_TYPE}
- POSTGRES_USER=${ZION_DB_USER}
- POSTGRES_PASSWORD=${ZION_DB_PASSWORD}
- POSTGRES_DB=${ZION_DB_TYPE}
container_name: 'zion-postgres-dev'
ports:
- '${DB_PORT}:5432'
Expand All @@ -15,15 +15,15 @@ services:
- database
entrypoint: ['sh', '-c','npm run db:migrate:latest && node dist/src/main.js']
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- ${ZION_SERVER_PORT}:${ZION_SERVER_PORT}
container_name: zion
environment:
- NODE_ENV
- SERVER_PORT
- DB_HOST=database
- DB_PORT
- DB_USER
- DB_PASSWORD
- DB_TYPE
- JWT_SECRET
- JWT_EXPIRES_IN
- ZION_NODE_ENV
- ZION_SERVER_PORT
- ZION_DB_HOST=database
- ZION_DB_PORT
- ZION_DB_USER
- ZION_DB_PASSWORD
- ZION_DB_TYPE
- ZION_JWT_SECRET
- ZION_JWT_EXPIRES_IN
10 changes: 5 additions & 5 deletions knexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ dotenv.config({
const config: Knex.Config = {
client: 'pg',
connection: {
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_TYPE,
host: process.env.ZION_DB_HOST,
port: Number(process.env.ZION_DB_PORT),
user: process.env.ZION_DB_USER,
password: process.env.ZION_DB_PASSWORD,
database: process.env.ZION_DB_TYPE,
},
migrations: {
directory: `${rootPath}/migrations`,
Expand Down
26 changes: 13 additions & 13 deletions src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ export class ConfigService {

public get database(): DatabaseConfig {
const databaseConfig: DatabaseConfig = {
host: this.nestConfigService.get('DB_HOST', Default.db.host),
port: this.nestConfigService.get('DB_PORT', Default.db.port),
type: this.nestConfigService.get('DB_TYPE', Default.db.type),
user: this.nestConfigService.get('DB_USER', Default.db.user),
password: this.nestConfigService.get('DB_PASSWORD', Default.db.password),
host: this.nestConfigService.get('ZION_DB_HOST', Default.db.host),
port: this.nestConfigService.get('ZION_DB_PORT', Default.db.port),
type: this.nestConfigService.get('ZION_DB_TYPE', Default.db.type),
user: this.nestConfigService.get('ZION_DB_USER', Default.db.user),
password: this.nestConfigService.get('ZION_DB_PASSWORD', Default.db.password),
};
return databaseConfig;
}

public get app(): AppConfig {
const appConfig: AppConfig = {
host: this.nestConfigService.get('APP_HOST', Default.app.host),
port: this.nestConfigService.get('APP_PORT', Default.app.port),
apiBase: this.nestConfigService.get('API_BASE', Default.app.apiBase),
host: this.nestConfigService.get('ZION_APP_HOST', Default.app.host),
port: this.nestConfigService.get('ZION_APP_PORT', Default.app.port),
apiBase: this.nestConfigService.get('ZION_API_BASE', Default.app.apiBase),
version: process.env.npm_package_version || '',
};
return appConfig;
Expand All @@ -32,8 +32,8 @@ export class ConfigService {
public get auth(): AuthConfig {
const authConfig: AuthConfig = {
jwt: {
secret: this.nestConfigService.get('JWT_SECRET', Default.auth.jwt.secret),
expiresIn: this.nestConfigService.get('JWT_EXPIRES_IN', Default.auth.jwt.expiresIn),
secret: this.nestConfigService.get('ZION_JWT_SECRET', Default.auth.jwt.secret),
expiresIn: this.nestConfigService.get('ZION_JWT_EXPIRES_IN', Default.auth.jwt.expiresIn),
},
};
return authConfig;
Expand All @@ -42,16 +42,16 @@ export class ConfigService {
public get introduction(): IntroductionConfig {
const introductionConfig: IntroductionConfig = {
mdns: {
name: this.nestConfigService.get('MDNS_TO_PATH', Default.introduction.mdns.toPath),
toPath: this.nestConfigService.get('MDNS_NAME', Default.introduction.mdns.name),
name: this.nestConfigService.get('ZION_MDNS_TO_PATH', Default.introduction.mdns.toPath),
toPath: this.nestConfigService.get('ZION_MDNS_NAME', Default.introduction.mdns.name),
},
};
return introductionConfig;
}

public get thingDescriptionEvents(): ThingDescriptionEventsConfig {
const thingDescriptionEventsConfig: ThingDescriptionEventsConfig = {
maxEvents: this.nestConfigService.get('MAX_EVENTS', Default.thingDescriptionEvents.maxEvents),
maxEvents: this.nestConfigService.get('ZION_MAX_EVENTS', Default.thingDescriptionEvents.maxEvents),
};
return thingDescriptionEventsConfig;
}
Expand Down
18 changes: 9 additions & 9 deletions test.env
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
NODE_ENV=test
SERVER_PORT=3000
DB_HOST=localhost
DB_PORT=54310
DB_USER=postgres
DB_PASSWORD=postgres
DB_TYPE=postgres
JWT_SECRET=test123
JWT_EXPIRES_IN=15m
ZION_NODE_ENV=test
ZION_SERVER_PORT=3000
ZION_DB_HOST=localhost
ZION_DB_PORT=54310
ZION_DB_USER=postgres
ZION_DB_PASSWORD=postgres
ZION_DB_TYPE=postgres
ZION_JWT_SECRET=test123
ZION_JWT_EXPIRES_IN=15m
8 changes: 4 additions & 4 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ services:
image: postgres:14.3-alpine
command: postgres -c fsync=off -c synchronous_commit=off -c full_page_writes=off -c random_page_cost=1.0
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_TYPE}
- POSTGRES_USER=${ZION_DB_USER}
- POSTGRES_PASSWORD=${ZION_DB_PASSWORD}
- POSTGRES_DB=${ZION_DB_TYPE}
container_name: 'zion-postgres-testing'
ports:
- '${DB_PORT}:5432'
- '${ZION_DB_PORT}:5432'
tmpfs: /var/lib/postgresql/data
2 changes: 1 addition & 1 deletion test/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function () {
debug: true,
});

const dbPort = Number(process.env.DB_PORT);
const dbPort = Number(process.env.ZION_DB_PORT);

// Speed up during development, if already live then do nothing
const reachablePort = await detectPort(dbPort);
Expand Down
3 changes: 2 additions & 1 deletion test/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function getExpiredAccessToken(user: User): string {
}

function signAccessToken(user: User, expirationInUnixTime: number): string {
const secret = process.env.JWT_SECRET || '';
// TODO: there should not be direct access to process.env (use settings service instead)
const secret = process.env.ZION_JWT_SECRET || '';
const token = sign(
{
sub: user.id,
Expand Down
10 changes: 5 additions & 5 deletions test/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { getUniqueEmail } from './data';
const knex = Knex({
client: 'pg',
connection: {
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.ZION_DB_HOST,
port: Number(process.env.ZION_DB_PORT),
user: process.env.ZION_DB_USER,
password: process.env.ZION_DB_PASSWORD,
database: process.env.ZION_DB_DATABASE,
},
});

Expand Down
2 changes: 1 addition & 1 deletion test/utils/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function getE2ETestResources() {
await app.listen(0);
const url = await app.getUrl();

process.env.API_BASE = url;
process.env.ZION_API_BASE = url;

const axios = ax.create({
baseURL: url,
Expand Down

0 comments on commit 34ce729

Please sign in to comment.