Skip to content

Commit

Permalink
feat: fix some env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dahal committed May 19, 2024
1 parent f2b833d commit b30ac54
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 83 deletions.
29 changes: 6 additions & 23 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@
# Next.js environment variables
NODE_ENV="development"
NEXT_PUBLIC_NODE_ENV="development"
BASE_URL="http://localhost:3000"
NEXT_PUBLIC_BASE_URL="http://localhost:3000"

# Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry
# Uncomment the following line to disable telemetry at run time
NEXT_TELEMETRY_DISABLED=1
# Disable telemetry at run time
NEXT_TELEMETRY_DISABLED=1 # Injected in Dockerfile

# Prisma
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
DATABASE_URL="postgres://captable:password@pg:5432/captable"

# Next Auth
# You can generate a new secret on the command line with:
# https://next-auth.js.org/configuration/options#secret

# openssl rand -base64 32
# Run `openssl rand -base64 32` to generate a new secret
NEXTAUTH_SECRET="xxxxxxxxxx"
NEXTAUTH_URL="http://localhost:3000"

Expand All @@ -36,24 +30,13 @@ GOOGLE_CLIENT_ID="xxxxxxxxxx"
GOOGLE_CLIENT_SECRET="xxxxxxxxxx"

# SMTP
EMAIL_SERVER_HOST="localhost"
EMAIL_SERVER_PORT=1025
EMAIL_SERVER_USERNAME="captable"
EMAIL_SERVER_PASSWORD="password"
EMAIL_SERVER_SECURE=0
# EMAIL_SERVER=smtp://captable:[email protected]:2500
EMAIL_FROM=[email protected]
EMAIL_FROM="'Captable, Inc.' <[email protected]>"
EMAIL_SERVER=smtp://captable:[email protected]:2500

# Uploads
UPLOAD_PROVIDER="s3"
UPLOAD_ENDPOINT="http://127.0.0.1:9002"
NEXT_PUBLIC_UPLOAD_DOMAIN="http://127.0.0.1:9002"

# value should be 'auto' while using r2
UPLOAD_REGION="us-east-1"
UPLOAD_REGION="us-east-1" # value should be 'auto' while using r2
UPLOAD_ACCESS_KEY_ID="captable"
UPLOAD_SECRET_ACCESS_KEY="password"

UPLOAD_BUCKET_PUBLIC="captable-public-bucket"
UPLOAD_BUCKET_PRIVATE="captable-private-bucket"

1 change: 0 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ tasks:
pnpm install &&
cp .env.example .env &&
export NEXTAUTH_SECRET="$(openssl rand -base64 32)" &&
export BASE_URL="$(gp url 3000)" &&
export NEXT_PUBLIC_BASE_URL="$(gp url 3000)" &&
export EMAIL_SERVER_PORT=2500
command: pnpm db:migrate && pnpm dx
Expand Down
50 changes: 49 additions & 1 deletion SELF-HOSTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,52 @@ If you like to self-host Captable, Inc., please schedule a call with us, and we

- **Official Docker Images**:
- <a href="https://hub.docker.com/r/captable/captable" target="_blank">Docker hub</a>
- <a href="https://github.com/captableinc/captable/pkgs/container/captable" target="_blank">Github registry</a>
<!-- - <a href="https://github.com/captableinc/captable/pkgs/container/captable" target="_blank">Github registry</a> -->

- **Environment Variables**:\
Following envrionment variables are required

```bash
NODE_ENV="production"
DATABASE_URL="postgres://user:password@host:port/dbname"
NEXTAUTH_SECRET="xxx" # Generated by `openssl rand -base64 32`
NEXTAUTH_URL="https://your-domain.com"
NEXT_PUBLIC_BASE_URL="https://your-domain.com"

# Email server environment variables
EMAIL_FROM="[email protected]"
EMAIL_SERVER="smtp://username:password@host:port"

# File uplod environment variables
UPLOAD_REGION="us-west-1" # auto when using Cloudflare R2
UPLOAD_ENDPOINT="https://xxx.r2.cloudflarestorage.com"
UPLOAD_ACCESS_KEY_ID="xxx"
UPLOAD_SECRET_ACCESS_KEY="xxx"
UPLOAD_BUCKET_PUBLIC="public-bucket-name"
UPLOAD_BUCKET_PRIVATE="private-bucket-name"
```

- **Setup CORS for file uploads**:\
Some of the services including Cloudflare R2 may require you to setup CORS for file uploads.\

> Here is an sample CORS configuration for Cloudflare R2.
```json
[
{
"AllowedOrigins": [
"https://your-domain.com"
],
"AllowedMethods": [
"HEAD",
"GET",
"POST",
"PUT",
"DELETE"
],
"AllowedHeaders": [
"*"
]
}
]
```
1 change: 0 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1

ENV DOCKER_OUTPUT 1
Expand Down
8 changes: 1 addition & 7 deletions src/common/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
getPresignedPutUrl,
type getPresignedUrlOptions,
} from "@/server/file-uploads";
import { env } from "next-runtime-env";
/**
* usage
* ```js
Expand Down Expand Up @@ -47,12 +46,7 @@ export const uploadFile = async (
}

const { name, type, size } = file;
let fileUrl = bucketUrl;

const uploadDomain = env("NEXT_PUBLIC_UPLOAD_DOMAIN");
if (bucketMode === "publicBucket" && uploadDomain) {
fileUrl = `${uploadDomain}/${key}`;
}
const fileUrl = bucketUrl;

return {
key,
Expand Down
36 changes: 0 additions & 36 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";

// https://env.t3.gg/docs/recipes#booleans
const COERCED_BOOLEAN = z
.string()
// transform to boolean using preferred coercion logic
.transform((s) => s !== "false" && s !== "0");

// const ONLY_BOOLEAN = z
// .string()
// // only allow "true" or "false"
// .refine((s) => s === "true" || s === "false")
// // transform to boolean
// .transform((s) => s === "true");

export const env = createEnv({
/**
* Specify your server-side environment variables schema here. This way you can ensure the app
* isn't built with invalid env vars.
*/
server: {
BASE_URL: z.string().url(),
DATABASE_URL: z
.string()
.url()
Expand All @@ -36,25 +22,13 @@ export const env = createEnv({
EMAIL_SERVER: z.string().optional(),
EMAIL_FROM: z.string(),

/// smtp

EMAIL_SERVER_HOST: z.string(),
EMAIL_SERVER_PORT: z.coerce.number(),
EMAIL_SERVER_SECURE: COERCED_BOOLEAN,
EMAIL_SERVER_USERNAME: z.string().optional(),
EMAIL_SERVER_PASSWORD: z.string().optional(),

//flags

// upload

UPLOAD_ENDPOINT: z.string(),
UPLOAD_REGION: z.string(),
UPLOAD_BUCKET_PUBLIC: z.string(),
UPLOAD_BUCKET_PRIVATE: z.string(),
UPLOAD_ACCESS_KEY_ID: z.string().optional(),
UPLOAD_SECRET_ACCESS_KEY: z.string().optional(),
UPLOAD_PROVIDER: z.enum(["s3", "r2"]),

// google
GOOGLE_CLIENT_ID: z.string().optional(),
Expand All @@ -70,7 +44,6 @@ export const env = createEnv({
// NEXT_PUBLIC_CLIENTVAR: z.string(),
NEXT_PUBLIC_BASE_URL: z.string(),
NEXT_PUBLIC_NODE_ENV: z.string().default("development"),
NEXT_PUBLIC_UPLOAD_DOMAIN: z.string().optional(),
},

/**
Expand All @@ -79,7 +52,6 @@ export const env = createEnv({
*/
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
BASE_URL: process.env.BASE_URL,
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
NEXT_PUBLIC_NODE_ENV: process.env.NEXT_PUBLIC_NODE_ENV,
DATABASE_URL: process.env.DATABASE_URL,
Expand All @@ -88,20 +60,12 @@ export const env = createEnv({
EMAIL_SERVER: process.env.EMAIL_SERVER,
EMAIL_FROM: process.env.EMAIL_FROM,

EMAIL_SERVER_HOST: process.env.EMAIL_SERVER_HOST,
EMAIL_SERVER_PORT: process.env.EMAIL_SERVER_PORT,
EMAIL_SERVER_SECURE: process.env.EMAIL_SERVER_SECURE,
EMAIL_SERVER_USERNAME: process.env.EMAIL_SERVER_USERNAME,
EMAIL_SERVER_PASSWORD: process.env.EMAIL_SERVER_PASSWORD,

UPLOAD_ENDPOINT: process.env.UPLOAD_ENDPOINT,
UPLOAD_REGION: process.env.UPLOAD_REGION,
UPLOAD_BUCKET_PUBLIC: process.env.UPLOAD_BUCKET_PUBLIC,
UPLOAD_BUCKET_PRIVATE: process.env.UPLOAD_BUCKET_PRIVATE,
UPLOAD_ACCESS_KEY_ID: process.env.UPLOAD_ACCESS_KEY_ID,
UPLOAD_SECRET_ACCESS_KEY: process.env.UPLOAD_SECRET_ACCESS_KEY,
UPLOAD_PROVIDER: process.env.UPLOAD_PROVIDER,
NEXT_PUBLIC_UPLOAD_DOMAIN: process.env.NEXT_PUBLIC_UPLOAD_DOMAIN,

GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/auth-verification-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const sendAuthVerificationEmail = async (
payload: AuthVerificationPayloadType,
) => {
const { email, token } = payload;
const baseUrl = env.BASE_URL;
const baseUrl = env.NEXT_PUBLIC_BASE_URL;

const confirmLink = `${baseUrl}/verify-email/${token}`;

Expand Down
2 changes: 1 addition & 1 deletion src/jobs/esign-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type ExtendedEsignPayloadType = EsignEmailPayloadType &

export const sendEsignEmail = async (payload: ExtendedEsignPayloadType) => {
const { email, token, ...rest } = payload;
const baseUrl = env.BASE_URL;
const baseUrl = env.NEXT_PUBLIC_BASE_URL;
const html = await render(
EsignEmail({
signingLink: `${baseUrl}/esign/${token}`,
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/member-inivite-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const sendMemberInviteEmail = async (
) => {
const { email, token, verificationToken, company, user } = payload;

const baseUrl = env.BASE_URL;
const baseUrl = env.NEXT_PUBLIC_BASE_URL;
const callbackUrl = `${baseUrl}/verify-member/${verificationToken}`;

const params = new URLSearchParams({
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/password-reset-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const sendPasswordResetEmail = async (
payload: PasswordResetPayloadType,
) => {
const { email, token } = payload;
const baseUrl = env.BASE_URL;
const baseUrl = env.NEXT_PUBLIC_BASE_URL;

const confirmLink = `${baseUrl}/reset-password/${token}`;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { env } from "@/env";
* Extracts common fields to identify the RP (relying party)
*/
export const getAuthenticatorOptions = () => {
const webAppBaseUrl = new URL(env.BASE_URL);
const webAppBaseUrl = new URL(env.NEXT_PUBLIC_BASE_URL);
const rpId = webAppBaseUrl.hostname;

return {
rpName: "Captable",
rpId,
origin: env.BASE_URL,
origin: env.NEXT_PUBLIC_BASE_URL,
timeout: PASSKEY_TIMEOUT,
};
};
5 changes: 3 additions & 2 deletions src/trpc/routers/data-room-router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export const dataRoomRouter = createTRPCRouter({
data: room,
};
} catch (error) {
console.error(error);
return {
success: false,
message:
Expand Down Expand Up @@ -207,7 +208,7 @@ export const dataRoomRouter = createTRPCRouter({
const company = dataRoom.company;

const upsertManyRecipients = async () => {
const baseUrl = env.BASE_URL;
const baseUrl = env.NEXT_PUBLIC_BASE_URL;
const recipients = [...others, ...selectedContacts];

for (const recipient of recipients) {
Expand Down Expand Up @@ -251,7 +252,7 @@ export const dataRoomRouter = createTRPCRouter({
const link = `${baseUrl}/data-rooms/${dataRoom.publicId}?token=${token}`;

const payload: DataRoomEmailPayloadType = {
senderName: senderName!,
senderName: `${senderName}`,
recipientName: recipient.name,
companyName: company.name,
dataRoom: dataRoom.name,
Expand Down
4 changes: 2 additions & 2 deletions src/trpc/routers/update/procedures/share-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const shareUpdateProcedure = withAuth
const company = update.company;

const upsertManyRecipients = async () => {
const baseUrl = env.BASE_URL;
const baseUrl = env.NEXT_PUBLIC_BASE_URL;
const recipients = [...others, ...selectedContacts];

for (const recipient of recipients) {
Expand Down Expand Up @@ -84,7 +84,7 @@ export const shareUpdateProcedure = withAuth
const link = `${baseUrl}/updates/${update.publicId}?token=${token}`;

const payload: UpdateSharePayloadType = {
senderName: senderName!,
senderName: `${senderName}`,
recipientName: recipient.name,
companyName: company.name,
update: {
Expand Down
9 changes: 5 additions & 4 deletions src/trpc/shared.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server";
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
import superjson from "superjson";

import { type AppRouter } from "@/trpc/api/root";
import type { AppRouter } from "@/trpc/api/root";

export const transformer = superjson;

function getBaseUrl() {
if (typeof window !== "undefined") return "";
return process.env.BASE_URL;
return process.env.NEXT_PUBLIC_BASE_URL;
}

export function getUrl() {
return getBaseUrl() + "/api/trpc";
const baseUrl = getBaseUrl();
return `${baseUrl}/api/trpc`;
}

/**
Expand Down

0 comments on commit b30ac54

Please sign in to comment.