From 9cca87caa3ad597fac4ae680793b1dc17617402e Mon Sep 17 00:00:00 2001 From: didinele Date: Sun, 4 Aug 2024 20:52:04 +0300 Subject: [PATCH] chore: update env setup --- .env.private.example | 23 +++++++++-------------- .env.public | 23 ++++++++++++++++++----- docker-compose.yml | 10 +++++----- packages/services/core/src/Env.ts | 8 +++++--- services/api/src/handlers/auth/discord.ts | 6 +++--- services/api/src/struct/Auth.ts | 2 +- 6 files changed, 41 insertions(+), 31 deletions(-) diff --git a/.env.private.example b/.env.private.example index e9c0efce..4c8f204e 100644 --- a/.env.private.example +++ b/.env.private.example @@ -1,15 +1,10 @@ -DISCORD_TOKEN= -DISCORD_CLIENT_ID= -NODE_ENV=dev -LOCAL_DATABASE_PORT=5432 +NODE_ENV=dev # obviously, set to prod in prod. + +AUTOMODERATOR_DISCORD_TOKEN=boop + +SECRET_SIGNING_KEY=boop # generate using node -e "console.log(require('crypto').randomBytes(32).toString('base64'));" +OAUTH_DISCORD_CLIENT_SECRET=boop + +# Used by Caddy directly via docker-compose for SSL certs +CF_API_TOKEN=boop LOCAL_DOZZLE_PORT=8080 -LOCAL_API_PORT=9876 -ADMINS= -DATABASE_URL=postgresql://chatsift:admin@localhost:5432/chatsift -SECRET_SIGNING_KEY=32randomBytesEncodedAsBase64 # You can generate this with node -e "console.log(require('crypto').randomBytes(32).toString('base64'));" -ALLOWED_API_ORIGINS=http://localhost:3000 -PUBLIC_API_URL=http://localhost:9876 -OAUTH_DISCORD_CLIENT_ID= -OAUTH_DISCORD_CLIENT_SECRET= -CORS="http:\/\/localhost:3000|https:\/\/automoderator\.app" -CF_API_TOKEN= diff --git a/.env.public b/.env.public index 1862ec4a..b61efda6 100644 --- a/.env.public +++ b/.env.public @@ -1,10 +1,23 @@ -REDIS_URL=redis://redis:6379 -DISCORD_PROXY_URL=http://discord-proxy:9000 -AUTOMODERATOR_GATEWAY_URL=http://gateway:9000 +LOGS_DIR=/var/chatsift-logs +ROOT_DOMAIN=automoderator.app + POSTGRES_HOST=postgres POSTGRES_PORT=5432 POSTGRES_USER=chatsift POSTGRES_PASSWORD=admin POSTGRES_DATABASE=chatsift -LOGS_DIR=/var/chatsift-logs -API_URL=http://api:9876 + +REDIS_URL=redis://redis:6379 + +ADMINS=223703707118731264 + +AUTOMODERATOR_DISCORD_CLIENT_ID=878278456629157939 +AUTOMODERATOR_GATEWAY_URL=http://automoderator-gateway:9000 +AUTOMODERATOR_PROXY_URL=http://automoderator-proxy:9000 + +API_PORT=9876 +PUBLIC_API_URL_DEV=http://localhost:9876 +PUBLIC_API_URL_PROD=https://api-canary.automoderator.app +OAUTH_DISCORD_CLIENT_ID=1005791929075769344 +CORS="http:\/\/localhost:3000|https:\/\/canary\.automoderator\.app" +ALLOWED_API_ORIGINS=http://localhost:3000,https://canary.automoderator.app diff --git a/docker-compose.yml b/docker-compose.yml index c14be084..7bb56f35 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -62,11 +62,11 @@ services: - ./.env.private command: ['node', '--enable-source-maps', './services/api/dist/index.js'] ports: - - 127.0.0.1:${LOCAL_API_PORT}:9876 + - 127.0.0.1:${API_PORT}:{API_PORT} volumes: - ./logs-archive:/var/chatsift-logs - discord-proxy: + automoderator-proxy: image: chatsift/chatsift-next:automoderator-discord-proxy build: context: ./ @@ -79,7 +79,7 @@ services: volumes: - ./logs-archive:/var/chatsift-logs - gateway: + automoderator-gateway: image: chatsift/chatsift-next:automoderator-gateway build: context: ./ @@ -92,7 +92,7 @@ services: volumes: - ./logs-archive:/var/chatsift-logs - interactions: + automoderator-interactions: image: chatsift/chatsift-next:automoderator-interactions build: context: ./ @@ -105,7 +105,7 @@ services: volumes: - ./logs-archive:/var/chatsift-logs - observer: + automoderator-observer: image: chatsift/chatsift-next:automoderator-observer build: context: ./ diff --git a/packages/services/core/src/Env.ts b/packages/services/core/src/Env.ts index 81a41b25..286c74b6 100644 --- a/packages/services/core/src/Env.ts +++ b/packages/services/core/src/Env.ts @@ -14,6 +14,7 @@ const envSchema = z.object({ // General config NODE_ENV: z.enum(['dev', 'prod']).default('prod'), LOGS_DIR: z.string(), + ROOT_DOMAIN: z.string(), // DB POSTGRES_HOST: z.string(), @@ -34,14 +35,13 @@ const envSchema = z.object({ AUTOMODERATOR_DISCORD_TOKEN: z.string(), AUTOMODERATOR_DISCORD_CLIENT_ID: z.string().regex(SnowflakeRegex), - AUTOMODERATOR_GATEWAY_URL: z.string().url(), - AUTOMODERATOR_PROXY_URL: z.string().url(), // API API_PORT: z.number(), - PUBLIC_API_URL: z.string().url(), + PUBLIC_API_URL_DEV: z.string().url(), + PUBLIC_API_URL_PROD: z.string().url(), SECRET_SIGNING_KEY: z.string().length(44), OAUTH_DISCORD_CLIENT_ID: z.string().regex(SnowflakeRegex), OAUTH_DISCORD_CLIENT_SECRET: z.string(), @@ -99,3 +99,5 @@ export function credentialsForCurrentBot(): BotCredentials { } } } + +export const API_URL = Env.NODE_ENV === 'dev' ? Env.PUBLIC_API_URL_DEV : Env.PUBLIC_API_URL_PROD; diff --git a/services/api/src/handlers/auth/discord.ts b/services/api/src/handlers/auth/discord.ts index e92a4e2d..2adb76a5 100644 --- a/services/api/src/handlers/auth/discord.ts +++ b/services/api/src/handlers/auth/discord.ts @@ -1,4 +1,4 @@ -import { Env, setEquals } from '@chatsift/service-core'; +import { Env, setEquals, API_URL } from '@chatsift/service-core'; import { API } from '@discordjs/core'; import { badRequest, forbidden } from '@hapi/boom'; import { SnowflakeRegex } from '@sapphire/discord-utilities'; @@ -55,7 +55,7 @@ export default class DiscordAuthHandler implements Registerable { const state = new StateCookie(redirectURI).toCookie(); const params = new URLSearchParams({ client_id: Env.OAUTH_DISCORD_CLIENT_ID, - redirect_uri: `${Env.PUBLIC_API_URL}/auth/discord/callback`, + redirect_uri: `${API_URL}/auth/discord/callback`, response_type: 'code', scope: SCOPES, state, @@ -96,7 +96,7 @@ export default class DiscordAuthHandler implements Registerable { client_secret: Env.OAUTH_DISCORD_CLIENT_SECRET, code, grant_type: 'authorization_code', - redirect_uri: `${Env.PUBLIC_API_URL}/auth/discord/callback`, + redirect_uri: `${API_URL}/auth/discord/callback`, }); if (!setEquals(new Set(result.scope.split(' ')), new Set(SCOPES.split(' ')))) { diff --git a/services/api/src/struct/Auth.ts b/services/api/src/struct/Auth.ts index a30b3eae..e8c77de7 100644 --- a/services/api/src/struct/Auth.ts +++ b/services/api/src/struct/Auth.ts @@ -61,7 +61,7 @@ export class Auth { path: '/', sameSite: Env.NODE_ENV === 'prod' ? 'none' : 'strict', httpOnly: true, - domain: Env.NODE_ENV === 'prod' ? '.automoderator.app' : undefined, + domain: Env.NODE_ENV === 'prod' ? `.${Env.ROOT_DOMAIN}` : undefined, secure: Env.NODE_ENV === 'prod', };