Skip to content

Commit

Permalink
Add new IS_UNSAFE_SELF_HOSTED env variable
Browse files Browse the repository at this point in the history
Setting this variable to any non-empty value will log verification codes instead of emailing them, and create "lifetime" accounts on signup instead of trials, effectively making it easier to self-host without setting up Brevo or Stripe.

Closes #4
  • Loading branch information
BrunoBernardino committed Sep 25, 2023
1 parent b66f00e commit cefa323
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ POSTGRESQL_CAFILE=""
BREVO_API_KEY="fake"

STRIPE_API_KEY="fake"

# Uncomment this variable to log verification codes instead of emailing them, and create "lifetime" accounts on signup instead of trials
# IS_UNSAFE_SELF_HOSTED="true" # any non-empty value will be considered true
6 changes: 3 additions & 3 deletions lib/data-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Database, { sql } from './interfaces/database.ts';
import { Budget, Expense, User, UserSession, VerificationCode } from './types.ts';
import { generateRandomCode, splitArrayInChunks } from './utils.ts';
import { generateRandomCode, IS_UNSAFE_SELF_HOSTED, splitArrayInChunks } from './utils.ts';

const db = new Database();

Expand All @@ -25,7 +25,7 @@ export async function getUserById(id: string) {
}

export async function createUser(email: User['email'], encryptedKeyPair: User['encrypted_key_pair']) {
const trialDays = 30;
const trialDays = IS_UNSAFE_SELF_HOSTED ? 30_000 : 30;
const now = new Date();
const trialEndDate = new Date(new Date().setUTCDate(new Date().getUTCDate() + trialDays));

Expand All @@ -47,7 +47,7 @@ export async function createUser(email: User['email'], encryptedKeyPair: User['e
[
email,
JSON.stringify(subscription),
'trial',
IS_UNSAFE_SELF_HOSTED ? 'active' : 'trial',
encryptedKeyPair,
JSON.stringify({}),
],
Expand Down
8 changes: 7 additions & 1 deletion lib/providers/brevo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'std/dotenv/load.ts';

import { helpEmail } from '/lib/utils.ts';
import { helpEmail, IS_UNSAFE_SELF_HOSTED } from '/lib/utils.ts';

const BREVO_API_KEY = Deno.env.get('BREVO_API_KEY') || '';

Expand Down Expand Up @@ -62,6 +62,12 @@ async function sendEmailWithTemplate(
email.cc = [{ email: cc }];
}

if (IS_UNSAFE_SELF_HOSTED) {
console.log('Email not sent!');
console.log(JSON.stringify(email, null, 2));
return;
}

const brevoResponse = await fetch('https://api.brevo.com/v3/smtp/email', {
method: 'POST',
headers: getApiRequestHeaders(),
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const STRIPE_MONTHLY_URL = 'https://buy.stripe.com/eVa01H57C3MB6CQ14s';
export const STRIPE_YEARLY_URL = 'https://buy.stripe.com/28o5m1dE896V0es8wV';
export const STRIPE_CUSTOMER_URL = 'https://billing.stripe.com/p/login/4gw15w3G9bDyfWU6oo';
export const PAYPAL_CUSTOMER_URL = 'https://www.paypal.com';
export const IS_UNSAFE_SELF_HOSTED = Boolean(Deno.env.get('IS_UNSAFE_SELF_HOSTED') || '');

export interface PageContentResult {
htmlContent: string;
Expand Down Expand Up @@ -186,7 +187,7 @@ export function splitArrayInChunks<T = any>(array: T[], chunkLength: number) {

// Because new URLSearchParams(Object.entries(object)).toString() doesn't work recursively
export function jsonToFormUrlEncoded(object: any, key = '', list: string[] = []) {
if (typeof (object) === 'object') {
if (typeof object === 'object') {
for (const subKey in object) {
jsonToFormUrlEncoded(object[subKey], key ? `${key}[${subKey}]` : subKey, list);
}
Expand Down
8 changes: 6 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { serve } from 'std/http/server.ts';
import routes, { Route } from './routes.ts';
import { IS_UNSAFE_SELF_HOSTED } from './lib/utils.ts';

function handler(request: Request) {
const routeKeys = Object.keys(routes);
Expand All @@ -22,4 +22,8 @@ export const abortController = new AbortController();

const PORT = Deno.env.get('PORT') || 8000;

serve(handler, { port: PORT as number, signal: abortController.signal });
if (IS_UNSAFE_SELF_HOSTED) {
console.log('IS_UNSAFE_SELF_HOSTED enabled! No emails will be sent and all signups will be forever.');
}

Deno.serve({ port: PORT as number, signal: abortController.signal }, handler);

0 comments on commit cefa323

Please sign in to comment.