Skip to content

Commit 1508b6a

Browse files
committed
fix(api-core): implemented
1 parent 3715ff1 commit 1508b6a

File tree

13 files changed

+202
-37
lines changed

13 files changed

+202
-37
lines changed

.env.example

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
# Since .env is gitignored, you can use .env.example to build a new `.env` file when you clone the repo.
2-
# Keep this file up-to-date when you add new variables to \`.env\`.
1+
# ENV
2+
NODE_ENV=development
33

4-
# This file will be committed to version control, so make sure not to have any secrets in it.
5-
# If you are cloning this repo, create a copy of this file named `.env` and populate it with your secrets.
4+
# API
5+
API_PROTOCOL=http
6+
API_HOST=0.0.0.0
7+
API_PORT=8080
8+
API_PREFIX=api
9+
JWT_SECRET_KEY=my-secret
10+
API_SECRET_KEY=x-api-key
611

7-
# The database URL is used to connect to your Supabase database.
8-
POSTGRES_URL="postgres://postgres.[USERNAME]:[PASSWORD]@aws-0-eu-central-1.pooler.supabase.com:6543/postgres?workaround=supabase-pooler.vercel"
12+
# WEBAPP
13+
AUTH_SECRET=auth-secret-key
14+
WEBAPP_PROTOCOL=http
15+
WEBAPP_HOST=localhost
16+
WEBAPP_PORT=3000
917

18+
#REDIS
19+
REDIS_HOST=localhost
20+
REDIS_PORT=6379
1021

11-
# You can generate the secret via 'openssl rand -base64 32' on Unix
12-
# @see https://next-auth.js.org/configuration/options#secret
13-
AUTH_SECRET='supersecret'
14-
15-
# Preconfigured Discord OAuth provider, works out-of-the-box
16-
# @see https://next-auth.js.org/providers/discord
17-
AUTH_DISCORD_ID=''
18-
AUTH_DISCORD_SECRET=''
19-
20-
# In case you're using the Auth Proxy (apps/auth-proxy)
21-
# AUTH_REDIRECT_PROXY_URL='https://auth.your-server.com/r'
22+
#NATS
23+
NATS_HOST=localhost
24+
NATS_PORT=4222

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ yarn-error.log*
4040
# local env files
4141
.env
4242
.env*.local
43+
.env.development
4344

4445
# vercel
4546
.vercel

apps/api-gateway/package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
"license": "UNLICENSED",
77
"author": "",
88
"scripts": {
9-
"build": "nest build",
10-
"dev": "nest start --watch",
9+
"build": "pnpm with-env nest build",
10+
"dev": "pnpm with-env-dev nest start --watch",
1111
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
1212
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
13-
"start": "nest start",
13+
"start": "pnpm with-env-dev nest start",
1414
"start:debug": "nest start --debug --watch",
15-
"start:prod": "node dist/main",
15+
"start:prod": "pnpm with-env node dist/main",
1616
"test": "jest",
1717
"test:cov": "jest --coverage",
1818
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
1919
"test:e2e": "jest --config ./test/jest-e2e.json",
20-
"test:watch": "jest --watch"
20+
"test:watch": "jest --watch",
21+
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
22+
"with-env": "dotenv -e ../../.env --",
23+
"with-env-dev": "dotenv -e ../../.env.development --"
2124
},
25+
"prettier": "@app/prettier-config",
2226
"jest": {
2327
"collectCoverageFrom": [
2428
"**/*.(t|j)s"
@@ -36,7 +40,6 @@
3640
"^.+\\.(t|j)s$": "ts-jest"
3741
}
3842
},
39-
"prettier": "@app/prettier-config",
4043
"dependencies": {
4144
"@fastify/multipart": "catalog:nestjs",
4245
"@fastify/static": "catalog:nestjs",
@@ -53,6 +56,7 @@
5356
"zod": "catalog:"
5457
},
5558
"devDependencies": {
59+
"@app/api-core": "workspace:*",
5660
"@app/constants": "workspace:*",
5761
"@app/eslint-config-nest": "workspace:*",
5862
"@app/tsconfig": "workspace:*",

apps/api-gateway/src/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Module } from "@nestjs/common";
22

3+
import { APICoreModule } from "@app/api-core";
4+
35
import { AppController } from "./app.controller";
46
import { AppService } from "./app.service";
57

68
@Module({
7-
imports: [],
9+
imports: [APICoreModule],
810
controllers: [AppController],
911
providers: [AppService],
1012
})

packages/api-core/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"license": "MIT",
66
"main": "dist/index.js",
77
"scripts": {
8-
"build": "tsc",
8+
"build": "pnpm with-env tsc",
99
"clean": "git clean -xdf .cache .turbo dist node_modules",
10-
"dev": "tsc",
10+
"dev": "pnpm with-end tsc",
1111
"format": "prettier --check . --ignore-path ../../.gitignore",
1212
"lint": "eslint ./src",
13-
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
13+
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
14+
"with-env": "dotenv -e ../../.env --"
1415
},
1516
"prettier": "@app/prettier-config",
1617
"dependencies": {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface AppConfig {
2+
// #ENV
3+
NODE_ENV: string;
4+
// #API
5+
API_PROTOCOL: string;
6+
API_HOST: string;
7+
API_PORT: number;
8+
API_PREFIX: string;
9+
JWT_SECRET_KEY: string;
10+
API_SECRET_KEY: string;
11+
// #REDIS
12+
REDIS_HOST: string;
13+
REDIS_PORT: number;
14+
// NATS
15+
NATS_HOST: string;
16+
NATS_PORT: number;
17+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import * as process from "node:process";
2+
import { registerAs } from "@nestjs/config";
3+
import { IsEnum, IsInt, IsOptional, IsString, Max, Min } from "class-validator";
4+
5+
import validateConfig from ".././utils/validate-config";
6+
import { AppConfig } from "./app-config.type";
7+
8+
enum Environment {
9+
Development = "development",
10+
Production = "production",
11+
Test = "test",
12+
}
13+
14+
class EnvironmentVariablesValidator {
15+
// #ENV
16+
@IsEnum(Environment)
17+
@IsOptional()
18+
NODE_ENV!: Environment;
19+
20+
// #API
21+
@IsString()
22+
API_PROTOCOL!: string;
23+
24+
@IsString()
25+
API_HOST!: string;
26+
27+
@IsInt()
28+
@Min(0)
29+
@Max(65535)
30+
API_PORT!: number;
31+
32+
@IsString()
33+
API_PREFIX!: string;
34+
35+
@IsString()
36+
JWT_SECRET_KEY!: string;
37+
38+
@IsString()
39+
API_SECRET_KEY!: string;
40+
41+
// #REDIS
42+
@IsInt()
43+
@Min(0)
44+
@Max(65535)
45+
REDIS_PORT!: number;
46+
47+
@IsString()
48+
REDIS_HOST!: string;
49+
50+
// #NATS
51+
@IsInt()
52+
@Min(0)
53+
@Max(65535)
54+
NATS_PORT!: number;
55+
56+
@IsString()
57+
NATS_HOST!: string;
58+
}
59+
60+
export default registerAs<AppConfig>("app", () => {
61+
validateConfig(process.env, EnvironmentVariablesValidator);
62+
63+
return {
64+
// #ENV
65+
NODE_ENV: process.env.NODE_ENV!,
66+
// #API
67+
API_PROTOCOL: process.env.API_PROTOCOL!,
68+
API_HOST: process.env.API_HOST!,
69+
API_PORT: parseInt(process.env.API_PORT as string)!,
70+
API_PREFIX: process.env.API_PREFIX!,
71+
JWT_SECRET_KEY: process.env.JWT_SECRET_KEY!,
72+
API_SECRET_KEY: process.env.API_SECRET_KEY!,
73+
// #REDIS
74+
REDIS_HOST: process.env.REDIS_HOST!,
75+
REDIS_PORT: parseInt(process.env.REDIS_PORT as string)!,
76+
// NATS
77+
NATS_PORT: parseInt(process.env.NATS_PORT as string)!,
78+
NATS_HOST: process.env.NATS_HOST!,
79+
};
80+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { AppConfig } from "./app-config.type";
2+
3+
export interface AllConfigType {
4+
app: AppConfig;
5+
}

packages/api-core/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
export const name = "api-core";
1+
import { Module } from "@nestjs/common";
2+
3+
import { APIConfigModule } from "./modules/api-config/api-config.module";
4+
5+
@Module({
6+
imports: [APIConfigModule],
7+
controllers: [],
8+
})
9+
export class APICoreModule {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Module } from "@nestjs/common";
2+
import { ConfigModule } from "@nestjs/config";
3+
4+
import appConfig from "../../config/app.config";
5+
6+
@Module({
7+
imports: [
8+
ConfigModule.forRoot({
9+
isGlobal: true,
10+
load: [appConfig],
11+
}),
12+
],
13+
controllers: [],
14+
})
15+
export class APIConfigModule {}
16+
17+
export * from "../../config/config.type";

0 commit comments

Comments
 (0)