Skip to content

Commit

Permalink
Add environment variables and update code based on environment in ser…
Browse files Browse the repository at this point in the history
…ver files
  • Loading branch information
MajorTom327 committed Jan 30, 2024
1 parent 2e388fc commit f3a7663
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 17 deletions.
13 changes: 10 additions & 3 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
APP_KEY=
APP_NAME=
# Env vars added by Remix Forge
APP_URL="http://localhost:3000"
SESSION_SECRET="{{uuid}}"

SESSION_SECRET=
# auth0 Env Variables
AUTH0_CLIENT_ID=""
AUTH0_CLIENT_SECRET=""
AUTH0_DOMAIN=""

# Database Env Variables
DATABASE_URL="postgresql://{{docker:postgres.POSTGRES_USER}}:{{docker:postgres.POSTGRES_PASSWORD}}@localhost:5432/{{docker:postgres.POSTGRES_DB}}"
3 changes: 2 additions & 1 deletion app/db.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from "@prisma/client";
import Env from "./services/env.server";

let prisma = new PrismaClient();

Expand All @@ -7,7 +8,7 @@ declare global {
var __db__: PrismaClient | undefined;
}

if (process.env.NODE_ENV === "production") {
if (Env.get("NODE_ENV") === "production") {
prisma = new PrismaClient();
} else {
if (!global.__db__) {
Expand Down
9 changes: 5 additions & 4 deletions app/services/auth_strategies/auth0.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Auth0Strategy } from "remix-auth-auth0";

import type { User } from "~/services/auth.server";
import { AuthStrategies } from "~/services/auth_strategies";
import Env from "../env.server";

const clientID = process.env.AUTH0_CLIENT_ID;
const clientSecret = process.env.AUTH0_CLIENT_SECRET;
const domain = process.env.AUTH0_DOMAIN;
const clientID = Env.get("AUTH0_CLIENT_ID");
const clientSecret = Env.get("AUTH0_CLIENT_SECRET");
const domain = Env.get("AUTH0_DOMAIN");

if (!clientID || !clientSecret || !domain) {
throw new Error(
Expand All @@ -19,7 +20,7 @@ export const auth0Strategy = new Auth0Strategy<User>(
clientID,
clientSecret,
domain,
callbackURL: `${process.env.APP_URL}/auth/${AuthStrategies.AUTH0}/callback`,
callbackURL: `${Env.get("APP_URL")}/auth/${AuthStrategies.AUTH0}/callback`,
},
async ({ accessToken, refreshToken, profile, extraParams }) => {
// Do something with the tokens and profile
Expand Down
16 changes: 16 additions & 0 deletions app/services/env.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import configure from "env-refiner";
import zod from "zod";

export const Env = configure({
schema: zod.object({
APP_URL: zod.string().url(),
SESSION_SECRET: zod.string(),

AUTH0_CLIENT_ID: zod.string(),
AUTH0_CLIENT_SECRET: zod.string(),
AUTH0_DOMAIN: zod.string(),

DATABASE_URL: zod.string().url(),
}),
});
export default Env;
3 changes: 2 additions & 1 deletion app/services/logger.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pino from "pino";
import { match } from "ts-pattern";
import Env from "./env.server";

const logLevel = match(process.env.NODE_ENV)
const logLevel = match(Env.get("NODE_ENV"))
.with("development", () => "debug")
.with("test", () => "silent")
.otherwise(() => "info");
Expand Down
5 changes: 3 additions & 2 deletions app/services/session.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createCookieSessionStorage } from "@remix-run/node";
import Env from "./env.server";

// export the whole sessionStorage object
export const sessionStorage = createCookieSessionStorage({
Expand All @@ -7,8 +8,8 @@ export const sessionStorage = createCookieSessionStorage({
sameSite: "lax", // this helps with CSRF
path: "/", // remember to add this so the cookie will work in all routes
httpOnly: true, // for security reasons, make this cookie http only
secrets: [process.env.SESSION_SECRET], // replace this with an actual secret
secure: process.env.NODE_ENV === "production", // enable this in prod only
secrets: [Env.get<string>("SESSION_SECRET")], // replace this with an actual secret
secure: Env.get("NODE_ENV") === "production", // enable this in prod only
},
});

Expand Down
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: "3.7"
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=dev
ports:
- "5432:5432"
# volumes:
# # - ./scripts/pg-init:/docker-entrypoint-initdb.d
# - ./postgres-data:/var/lib/postgresql/data
adminer:
image: adminer
restart: always
ports:
- 8080:8080
environment:
- ADMINER_DEFAULT_SERVER=postgres
- ADMINER_DESIGN=dracula
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"db:gen": "prisma generate",
"db:migrate": "prisma migrate dev",
"db:seed": "prisma db seed",
"db:studio": "prisma studio"
"db:studio": "prisma studio",
"env:gen": "env-refiner -i ./.env.dist -o ./.env"
},
"prettier": {},
"eslintIgnore": [
Expand All @@ -32,13 +33,14 @@
"@architect/functions": "^7.0.0",
"@paralleldrive/cuid2": "^2.2.2",
"@prisma/client": "^5.8.1",
"@remix-run/architect": "^2.5.0",
"@remix-run/css-bundle": "^2.5.0",
"@remix-run/node": "^2.5.0",
"@remix-run/react": "^2.5.0",
"@remix-run/architect": "*",
"@remix-run/css-bundle": "*",
"@remix-run/node": "*",
"@remix-run/react": "*",
"bcryptjs": "2.4.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"env-refiner": "^0.0.9",
"isbot": "^3.7.0",
"lucide-react": "^0.311.0",
"luxon": "^3.4.4",
Expand All @@ -58,7 +60,7 @@
},
"devDependencies": {
"@faker-js/faker": "^8.2.0",
"@remix-run/dev": "^2.5.0",
"@remix-run/dev": "*",
"@testing-library/cypress": "^10.0.1",
"@testing-library/jest-dom": "^6.1.4",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
Expand Down

0 comments on commit f3a7663

Please sign in to comment.