add mongo and infisical

This commit is contained in:
Lee 2023-11-20 14:43:55 +00:00
parent 3f7b723311
commit 0430186f7e
8 changed files with 171 additions and 2 deletions

1
.env-example Normal file
View File

@ -0,0 +1 @@
INFISCAL_TOKEN=set me

View File

@ -10,7 +10,9 @@
"dev": "nodemon --exec ts-node src/index.ts"
},
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"infisical-node": "^1.5.0",
"mongoose": "^8.0.1",
"tsup": "^8.0.0",
"typescript": "^5.2.2"

79
pnpm-lock.yaml generated
View File

@ -5,9 +5,15 @@ settings:
excludeLinksFromLockfile: false
dependencies:
dotenv:
specifier: ^16.3.1
version: 16.3.1
express:
specifier: ^4.18.2
version: 4.18.2
infisical-node:
specifier: ^1.5.0
version: 1.5.0
mongoose:
specifier: ^8.0.1
version: 8.0.1
@ -523,6 +529,20 @@ packages:
engines: {node: '>=8'}
dev: false
/asynckit@0.4.0:
resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
dev: false
/axios@1.6.2:
resolution: {integrity: sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==}
dependencies:
follow-redirects: 1.15.3
form-data: 4.0.0
proxy-from-env: 1.1.0
transitivePeerDependencies:
- debug
dev: false
/balanced-match@1.0.2:
resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
@ -609,6 +629,13 @@ packages:
optionalDependencies:
fsevents: 2.3.3
/combined-stream@1.0.8:
resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==}
engines: {node: '>= 0.8'}
dependencies:
delayed-stream: 1.0.0
dev: false
/commander@4.1.1:
resolution: {integrity: sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==}
engines: {node: '>= 6'}
@ -694,6 +721,11 @@ packages:
has-property-descriptors: 1.0.1
dev: false
/delayed-stream@1.0.0:
resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==}
engines: {node: '>=0.4.0'}
dev: false
/depd@2.0.0:
resolution: {integrity: sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==}
engines: {node: '>= 0.8'}
@ -715,6 +747,11 @@ packages:
path-type: 4.0.0
dev: false
/dotenv@16.3.1:
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
engines: {node: '>=12'}
dev: false
/ee-first@1.1.1:
resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==}
dev: false
@ -855,6 +892,25 @@ packages:
- supports-color
dev: false
/follow-redirects@1.15.3:
resolution: {integrity: sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==}
engines: {node: '>=4.0'}
peerDependencies:
debug: '*'
peerDependenciesMeta:
debug:
optional: true
dev: false
/form-data@4.0.0:
resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==}
engines: {node: '>= 6'}
dependencies:
asynckit: 0.4.0
combined-stream: 1.0.8
mime-types: 2.1.35
dev: false
/forwarded@0.2.0:
resolution: {integrity: sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==}
engines: {node: '>= 0.6'}
@ -989,6 +1045,17 @@ packages:
engines: {node: '>= 4'}
dev: false
/infisical-node@1.5.0:
resolution: {integrity: sha512-mMdhhu50J7pI4Y89M2HMbNWRyRY6ian3oBIXW+pAWz1z8XWn5yT0ge6/YNXwTPKLnBuR1py+GwiNQUaqxkOXZw==}
dependencies:
axios: 1.6.2
dotenv: 16.3.1
tweetnacl: 1.0.3
tweetnacl-util: 0.15.1
transitivePeerDependencies:
- debug
dev: false
/inflight@1.0.6:
resolution: {integrity: sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==}
dependencies:
@ -1356,6 +1423,10 @@ packages:
ipaddr.js: 1.9.1
dev: false
/proxy-from-env@1.1.0:
resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==}
dev: false
/pstree.remy@1.1.8:
resolution: {integrity: sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==}
dev: true
@ -1702,6 +1773,14 @@ packages:
- ts-node
dev: false
/tweetnacl-util@0.15.1:
resolution: {integrity: sha512-RKJBIj8lySrShN4w6i/BonWp2Z/uxwC3h4y7xsRrpP59ZboCd0GpEVsOnMDYLMmKBpYhb5TgHzZXy7wTfYFBRw==}
dev: false
/tweetnacl@1.0.3:
resolution: {integrity: sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==}
dev: false
/type-is@1.6.18:
resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==}
engines: {node: '>= 0.6'}

11
src/database/mongo.ts Normal file
View File

@ -0,0 +1,11 @@
import mongoose from "mongoose";
import { Secrets } from "../secrets/secrets";
/**
* Connects to the mongo database
*
* @returns a promise that resolves when the connection is established
*/
export function connectMongo() {
return mongoose.connect(Secrets.MONGO_URI);
}

View File

@ -1,4 +1,27 @@
import dotenv from "dotenv";
import { connectMongo } from "./database/mongo";
import { initSecrets } from "./secrets/secrets";
import { SsrServer } from "./server/impl/ssrServer";
import { checkEnvironmentVariables } from "./util/envVariables";
import { createInfisicalClient } from "./util/secrets";
// Init the SSR Server
const server = new SsrServer();
// Load the environment variables
dotenv.config();
const isValid = checkEnvironmentVariables("INFISCAL_TOKEN");
if (!isValid) {
process.exit(1);
}
export const InfisicalClient = createInfisicalClient(
process.env.INFISCAL_TOKEN!
);
// Load the secrets first so we ensure they are valid before starting the server
initSecrets().then(async () => {
await connectMongo();
console.log("Connected to mongo");
// Init the SSR Server
new SsrServer();
});

22
src/secrets/secrets.ts Normal file
View File

@ -0,0 +1,22 @@
import { InfisicalClient } from "..";
let MONGO_URI: string;
/**
* Initialize the secrets
*/
export async function initSecrets() {
const mongoUri = (await InfisicalClient.getSecret("MONGO_URI")).secretValue;
if (!mongoUri) {
throw new Error("MONGO_URI not set in secrets");
}
MONGO_URI = mongoUri;
}
export const Secrets = {
get MONGO_URI() {
return MONGO_URI;
},
};

15
src/util/envVariables.ts Normal file
View File

@ -0,0 +1,15 @@
/**
* Checks if all environment variables are set
*
* @param variables the environment variables to check
* @returns true if all variables are set, false otherwise
*/
export function checkEnvironmentVariables(...variables: string[]): boolean {
let allVariablesSet = true;
variables.forEach((variable) => {
if (!process.env[variable]) {
throw new Error(`${variable} not set in environment variables`);
}
});
return allVariablesSet;
}

16
src/util/secrets.ts Normal file
View File

@ -0,0 +1,16 @@
import InfisicalClient from "infisical-node";
/**
* Create an infisical client
*
* @param token the infisical token
* @returns the infisical client
*/
function createInfisicalClient(token: string) {
return new InfisicalClient({
token,
siteURL: "https://secrets.fascinated.cc",
});
}
export { createInfisicalClient };