-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: add environment variable validation and update documentation #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,15 @@ | ||
| # Backend Environment Variables | ||
| # Copy this to .env and fill in your actual values | ||
|
|
||
| # Supabase Configuration (get from https://app.supabase.com → Project Settings → API) | ||
| # Supabase Configuration | ||
| SUPABASE_URL=https://your-project-id.supabase.co | ||
| SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... | ||
| SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... | ||
| SUPABASE_ANON_KEY=your-anon-key | ||
| SUPABASE_SERVICE_ROLE_KEY=your-service-role-key | ||
|
|
||
| # HuggingFace API Token (optional - get from https://huggingface.co/settings/tokens) | ||
| # If not provided, crisis detection will use keyword-based fallback | ||
| # HuggingFace API Token (Optional) | ||
| HUGGINGFACE_API_TOKEN=hf_YourTokenHere | ||
|
|
||
| # Frontend URL (for CORS whitelist) | ||
| FRONTEND_URL=http://localhost:3000 | ||
|
|
||
| # Server Configuration | ||
| PORT=3001 | ||
| FRONTEND_URL=http://localhost:3000 | ||
|
|
||
| # Rate Limiting (optional - defaults shown) | ||
| # Rate Limiting | ||
| RATE_LIMIT_WINDOW_MS=900000 | ||
| RATE_LIMIT_MAX_REQUESTS=100 | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { z } from 'zod'; | ||
| import dotenv from 'dotenv'; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const envSchema = z.object({ | ||
| // Supabase Configuration | ||
| SUPABASE_URL: z.string().url(), | ||
| SUPABASE_ANON_KEY: z.string().min(1), | ||
| SUPABASE_SERVICE_ROLE_KEY: z.string().min(1), | ||
|
|
||
| // HuggingFace API Token (Optional) | ||
| HUGGINGFACE_API_TOKEN: z.string().optional(), | ||
|
|
||
| // Server Configuration | ||
| FRONTEND_URL: z.string().url().default('http://localhost:3000'), | ||
| PORT: z.coerce.number().default(3001), | ||
|
|
||
| // Rate Limiting | ||
| RATE_LIMIT_WINDOW_MS: z.coerce.number().default(900000), | ||
| RATE_LIMIT_MAX_REQUESTS: z.coerce.number().default(100), | ||
| }); | ||
|
|
||
| const _env = envSchema.safeParse(process.env); | ||
|
|
||
| if (!_env.success) { | ||
| console.error('Invalid environment variables:'); | ||
| console.error(JSON.stringify(_env.error.format(), null, 4)); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| export const env = _env.data; |
|
ayushHardeniya marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,22 @@ | ||
| import dotenv from 'dotenv'; | ||
| import { env } from './env'; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| interface Config { | ||
| const config = { | ||
| supabase: { | ||
| url: string; | ||
| anonKey: string; | ||
| serviceRoleKey: string; | ||
| }; | ||
| huggingface: { | ||
| apiToken?: string; | ||
| }; | ||
| server: { | ||
| port: number; | ||
| frontendUrl: string; | ||
| }; | ||
| rateLimit: { | ||
| windowMs: number; | ||
| maxRequests: number; | ||
| }; | ||
| } | ||
|
|
||
| const config: Config = { | ||
| supabase: { | ||
| url: process.env.SUPABASE_URL || '', | ||
| anonKey: process.env.SUPABASE_ANON_KEY || '', | ||
| serviceRoleKey: process.env.SUPABASE_SERVICE_ROLE_KEY || '', | ||
| url: env.SUPABASE_URL, | ||
| anonKey: env.SUPABASE_ANON_KEY, | ||
| serviceRoleKey: env.SUPABASE_SERVICE_ROLE_KEY, | ||
| }, | ||
| huggingface: { | ||
| apiToken: process.env.HUGGINGFACE_API_TOKEN, | ||
| apiToken: env.HUGGINGFACE_API_TOKEN, | ||
| }, | ||
| server: { | ||
| port: parseInt(process.env.PORT || '3001', 10), | ||
| frontendUrl: process.env.FRONTEND_URL || 'http://localhost:3000', | ||
| port: env.PORT, | ||
| frontendUrl: env.FRONTEND_URL, | ||
| }, | ||
| rateLimit: { | ||
| windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS || '900000', 10), | ||
| maxRequests: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS || '100', 10), | ||
| windowMs: env.RATE_LIMIT_WINDOW_MS, | ||
| maxRequests: env.RATE_LIMIT_MAX_REQUESTS, | ||
| }, | ||
| }; | ||
|
|
||
| // Validation | ||
| const requiredEnvVars = [ | ||
| 'SUPABASE_URL', | ||
| 'SUPABASE_ANON_KEY', | ||
| 'SUPABASE_SERVICE_ROLE_KEY', | ||
| 'FRONTEND_URL', | ||
| ]; | ||
|
|
||
| const missingVars = requiredEnvVars.filter((varName) => !process.env[varName]); | ||
|
|
||
| if (missingVars.length > 0) { | ||
| throw new Error( | ||
| `Missing required environment variables: ${missingVars.join(', ')}\n` + | ||
| 'Please check your .env file and ensure all required variables are set.' | ||
| ); | ||
| } | ||
|
|
||
| export default config; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated .env.example file removes helpful documentation that was present in the original version, such as URLs where developers can obtain the required keys (e.g., "get from https://app.supabase.com → Project Settings → API" for Supabase configuration, "get from https://huggingface.co/settings/tokens" for HuggingFace). Consider restoring these comments to improve the developer experience, especially for new contributors who may not know where to find these credentials.