Skip to content

Commit

Permalink
Add some env validations
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaUrsa committed Nov 16, 2023
1 parent 8f7cf23 commit b71795e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
12 changes: 4 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@ PRISMA_DB_URL = postgres://tripsit:SuperSecure123@tripbot_database:5432/tripsit
POSTGRES_DB_URL = postgres://tripsit:SuperSecure123@tripbot_database:5432/tripsit

# REQUIRED Database password
# This shold be the same as the PRISMA_DB_URL and POSTGRS_DB_URL passwords
# This should be the same as the PRISMA_DB_URL and POSTGRS_DB_URL passwords
POSTGRESQL_PASSWORD = SuperSecure123

## Everything after this is OPTIONAL

# Keep as development
NODE_ENV = development

# Feel free to change
TIME_ZONE = "America/Chicago"
TZ = "America/Chicago"

# Used for docker
COMPOSE_PROJECT_NAME = 'tripbot'
XID = 1000
PUID = 1000
PGID = 1000

#### Everything after this is OPTIONAL ####

# Domains
DNS_DOMAIN = localhost
DNS_DOMAIN = tripsit.io

# PG Admin
PGADMIN_PASSWORD = SuperSecure123
Expand Down
10 changes: 2 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ name: tripsit-dev-apps
# Common environment values
x-environment: &default-tz-puid-pgid
TZ: $TZ
PUID: $PUID
PGID: $PGID
PUID: 1000
PGID: 1000

# Keys common to some of the core services that we always to automatically restart on failure
x-common-keys-core: &common-keys-core
Expand Down Expand Up @@ -46,8 +46,6 @@ services:
volumes:
- ./src:/usr/src/app/src # Needed for hot-reloading of source code
- ./.env:/usr/src/app/.env # Needed for hot-reloading the .env
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
labels:
- com.centurylinklabs.watchtower.enable=false
- traefik.enable=true
Expand Down Expand Up @@ -91,7 +89,6 @@ services:
- tripbot_database-net
environment:
<<: *default-tz-puid-pgid
TZ: America/Chicago
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
labels:
Expand Down Expand Up @@ -144,12 +141,9 @@ services:
- tripbot_redis-net
volumes:
- ./src:/usr/src/app/src # Needed for hot-reloading of source code
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
environment:
<<: *default-tz-puid-pgid
PORT: 5000
TZ: America/Chicago
DATABASE_URL: postgres://tripsit:${POSTGRESQL_PASSWORD}@tripbot_database:5432/tripsit
labels:
- com.centurylinklabs.watchtower.enable=false
Expand Down
3 changes: 0 additions & 3 deletions src/docker/Dockerfile.tripbot
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

FROM node:21.1.0-alpine AS development

ENV TZ="America/Chicago"
ENV NODE_ENV=development
RUN date

Expand Down Expand Up @@ -62,7 +61,6 @@ CMD npx prisma migrate deploy && npx tsc-watch --onSuccess "npx nodemon --config

FROM node:21.1.0-alpine AS build

ENV TZ="America/Chicago"
ENV NODE_ENV=production
RUN date

Expand Down Expand Up @@ -102,7 +100,6 @@ RUN npm ci --omit:dev && npm cache clean --force

FROM node:21.1.0-alpine AS production

ENV TZ="America/Chicago"
ENV NODE_ENV=production
RUN date

Expand Down
7 changes: 6 additions & 1 deletion src/global/commands/g.ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export async function aiModerateReport(
// log.debug(F, `message: ${message}`);

// log.debug(F, `results: ${JSON.stringify(results, null, 2)}`);

if (!env.OPENAI_API_ORG || !env.OPENAI_API_KEY) return undefined;

return openai.moderations
.create({
input: message,
Expand All @@ -60,7 +63,6 @@ export async function aiModerateReport(
if (err instanceof OpenAI.APIError) {
log.error(F, `${err.status}`); // 400
log.error(F, `${err.name}`); // BadRequestError

log.error(F, `${err.headers}`); // {server: 'nginx', ...}
} else {
throw err;
Expand Down Expand Up @@ -160,6 +162,9 @@ export default async function aiChat(

// log.debug(F, `payload: ${JSON.stringify(payload, null, 2)}`);
let responseMessage = {} as OpenAI.Chat.ChatCompletionMessageParam;

if (!env.OPENAI_API_ORG || !env.OPENAI_API_KEY) return { response, promptTokens, completionTokens };

const chatCompletion = await openai.chat.completions
.create(payload)
.catch(err => {
Expand Down
4 changes: 2 additions & 2 deletions src/global/utils/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const env = {
API_USERNAME: process.env.API_USERNAME,
API_PASSWORD: process.env.API_PASSWORD,

OPENAI_API_ORG: process.env.OPENAI_API_ORG,
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
OPENAI_API_ORG: process.env.OPENAI_API_ORG ?? '',
OPENAI_API_KEY: process.env.OPENAI_API_KEY ?? '',

IRC_USERNAME: 'TripBot',
IRC_PASSWORD: process.env.IRC_PASSWORD,
Expand Down
46 changes: 35 additions & 11 deletions src/global/utils/env.validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ export default function validateEnv(
}

if (service === 'DISCORD') {
if (!process.env.DISCORD_CLIENT_ID) {
log.error(F, stripIndents`Missing DISCORD_CLIENT_ID: You wont be able to login to discord. \
You get this from the discord developer portal.`);
if (!process.env.DISCORD_GUILD_ID) {
log.error(F, stripIndents`Missing DISCORD_GUILD_ID: You get this from your discord guild. \
It should be set by default to "960606557622657026"`);
return false;
}

if (!process.env.DISCORD_GUILD_ID) {
log.error(F, 'Missing DISCORD_GUILD_ID: You get this from your discord guild.');
if (!process.env.DISCORD_OWNER_ID) {
log.warn(F, stripIndents`Missing DISCORD_OWNER_ID: Certain commands wont respond right, but nbd. \
You get this from your own discord user.`);
}

if (!process.env.DISCORD_CLIENT_ID) {
log.error(F, stripIndents`Missing DISCORD_CLIENT_ID: You wont be able to login to discord. \
You get this from the discord developer portal.`);
return false;
}

Expand All @@ -30,6 +36,24 @@ export default function validateEnv(
return false;
}

if (!process.env.PRISMA_DB_URL) {
log.error(F, stripIndents`Missing PRISMA_DB_URL: You wont be able to use the database via Prisma.\
By default this should be set to 'postgres://tripsit:SuperSecure123@tripbot_database:5432/tripsit'.`);
return false;
}

if (!process.env.POSTGRES_DB_URL) {
log.error(F, stripIndents`Missing POSTGRES_DB_URL: You wont be able to use the database via Knex.\
By default this should be set to 'postgres://tripsit:SuperSecure123@tripbot_database:5432/tripsit'.`);
return false;
}

if (!process.env.POSTGRESQL_PASSWORD) {
log.error(F, stripIndents`Missing POSTGRESQL_PASSWORD: You wont be able to use the database.\
By default this should be set to 'SuperSecure123'.`);
return false;
}

// Check that the discord token is a valid token
if (process.env.DISCORD_CLIENT_TOKEN === 'In your Discord Developer Portal') {
log.error(F, `Welcome to TripBot. This is likely your first run, congrats on making it this far!
Expand All @@ -50,12 +74,12 @@ export default function validateEnv(
log.warn(F, 'Missing POSTGRES_DB_URL: You wont be able to use the database!');
}

if (!process.env.KEYCLOAK_BASE_URL
|| !process.env.KEYCLOAK_REALM_NAME
|| !process.env.KEYCLOAK_CLIENT_ID
|| !process.env.KEYCLOAK_CLIENT_SECRET) {
log.warn(F, 'Missing keycloak credentials: You won\'t be able to interact with KeyCloak.');
}
// if (!process.env.KEYCLOAK_BASE_URL
// || !process.env.KEYCLOAK_REALM_NAME
// || !process.env.KEYCLOAK_CLIENT_ID
// || !process.env.KEYCLOAK_CLIENT_SECRET) {
// log.warn(F, 'Missing keycloak credentials: You won\'t be able to interact with KeyCloak.');
// }

if (!process.env.GITHUB_TOKEN) {
log.warn(F, 'Missing GITHUB_TOKEN: You wont be able to use /issue');
Expand Down

0 comments on commit b71795e

Please sign in to comment.