Skip to content

Commit

Permalink
Merge pull request #22 from joepbuhre/feat/set-proper-fallback-env
Browse files Browse the repository at this point in the history
Feat/set proper fallback env
  • Loading branch information
joepbuhre authored Sep 30, 2023
2 parents bfcfeaf + f3b5c1f commit 9fc85c7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
9 changes: 9 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs ? import <nixpkgs> {} }:
let
in
pkgs.mkShell {
buildInputs = [
pkgs.nodejs_20
];

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev": "npm run dev:frontend && npm run dev:server",
"pm": "dotenv -e .env -- npx prisma migrate dev && prisma generate",
"dev:frontend": "vite ./frontend",
"dev:server": "chcp 65001 && nodemon ./server/index.ts | pino-pretty",
"dev:server": "nodemon ./server/index.ts | pino-pretty",
"build": "npm i && npm run build:frontend && npm run build:server",
"build:frontend": "vue-tsc -p ./frontend/tsconfig.json --noEmit && vite build ./frontend --emptyOutDir",
"build:server": "npx tsc -p ./server/tsconfig.json",
Expand Down
7 changes: 7 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import health from "./routes/health.route";
import { checkauth } from "./controllers/auth.controller";
import auth from "./routes/auth.route";
import { getSavePath } from "./utils/helpers";
import { randomBytes } from "crypto";

const startApp = async (): Promise<Router> => {
if(process.env.API_KEY === undefined || process.env.API_KEY === "") {
logger.warn("API_KEY has not been set. Generating one ourselves. WATCH OUT: this changes on every restart")
process.env.API_KEY = randomBytes(48).toString('hex')
logger.warn(`api key is '${process.env.API_KEY}'`)
}

const router = Router();
const app = express();

Expand Down
2 changes: 1 addition & 1 deletion server/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const startEmail = () => {
let emailPort = parseInt(process.env?.EMAIL_PORT || "25");

if (process.env.ALLOWED_FROM === undefined || process.env.ALLOWED_TO === undefined) {
logger.error("Env variables are not set correctly");
logger.error("Env variables are not set correctly, please check 'ALLOWED_FROM' & 'ALLOWED_TO'");
process.exit(1);
}

Expand Down
5 changes: 5 additions & 0 deletions server/frontend.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import express, { static as _static, Request, Response, Router } from "express";
import { logger } from "./utils/logger";
import { resolve } from "path";
import { randomBytes } from "crypto";

const startFrontend = (): void => {
const router = Router();
const app = express();

// Check if we have a api key otherwise we set it here
// TODO: can we always just set it here?
process.env.VITE_PUBLIC_KEY = randomBytes(48).toString('hex')

router.get("/env.js", (req: Request, res: Response) => {
res.set({ "Content-Type": "text/javascript" }).send(
`window.VITE_PUBLIC_KEY = "${process.env.VITE_PUBLIC_KEY}"`
Expand Down
6 changes: 3 additions & 3 deletions server/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { join } from "path";
import { logger } from "./logger";

export const getSavePath = (filename: string): string => {
if (process.env.UPLOAD_PATH === undefined) {
logger.error("Environmental variable upload path has not been set");
return "";
if (process.env.UPLOAD_PATH === undefined || process.env.UPLOAD_PATH === "") {
logger.warn("Environmental variable upload path has not been set, defaulting to /tmp");
process.env.UPLOAD_PATH = "/tmp"
}
const path: string = process.env.UPLOAD_PATH;

Expand Down

0 comments on commit 9fc85c7

Please sign in to comment.