mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-08-15 08:20:33 +02:00
feat: add optional MongoDB replica deployment (#765)
This commit is contained in:
+10
-1
@@ -26,6 +26,12 @@ interface Config {
|
||||
DB_USERNAME: string;
|
||||
DB_PASSWORD: string;
|
||||
DB_HOSTNAME: string;
|
||||
/**
|
||||
* Complete MongoDB connection string. When set, this takes precedence over
|
||||
* DB_USERNAME, DB_PASSWORD, and DB_HOSTNAME and supports replica-set seed
|
||||
* lists and options.
|
||||
*/
|
||||
MONGODB_URI: string;
|
||||
FOLDER: string;
|
||||
additionalExtensions: string[];
|
||||
S3_BUCKET: string | null;
|
||||
@@ -73,6 +79,7 @@ const config: Config = {
|
||||
DB_USERNAME: "admin",
|
||||
DB_PASSWORD: "password",
|
||||
DB_HOSTNAME: "mongodb",
|
||||
MONGODB_URI: "",
|
||||
REDIS_HOSTNAME: "redis",
|
||||
REDIS_PORT: 6379,
|
||||
FOLDER: resolve(__dirname, "..", "repositories"),
|
||||
@@ -142,8 +149,10 @@ if (isProduction) {
|
||||
const insecureDefaults: [string, string][] = [
|
||||
["CLIENT_ID", "CLIENT_ID"],
|
||||
["CLIENT_SECRET", "CLIENT_SECRET"],
|
||||
["DB_PASSWORD", "password"],
|
||||
];
|
||||
if (!config.MONGODB_URI) {
|
||||
insecureDefaults.push(["DB_PASSWORD", "password"]);
|
||||
}
|
||||
for (const [key, badValue] of insecureDefaults) {
|
||||
if ((config as unknown as Record<string, unknown>)[key] === badValue) {
|
||||
throw new Error(
|
||||
|
||||
+10
-4
@@ -8,7 +8,12 @@ import PullRequest from "../core/PullRequest";
|
||||
import AnonymizedGistModel from "../core/model/anonymizedGists/anonymizedGists.model";
|
||||
import Gist from "../core/Gist";
|
||||
|
||||
const MONGO_URL = `mongodb://${config.DB_USERNAME}:${config.DB_PASSWORD}@${config.DB_HOSTNAME}:27017/`;
|
||||
export function getMongoUrl(): string {
|
||||
return (
|
||||
config.MONGODB_URI ||
|
||||
`mongodb://${config.DB_USERNAME}:${config.DB_PASSWORD}@${config.DB_HOSTNAME}:27017/production`
|
||||
);
|
||||
}
|
||||
|
||||
export const database = mongoose.connection;
|
||||
|
||||
@@ -16,11 +21,12 @@ export let isConnected = false;
|
||||
|
||||
export async function connect() {
|
||||
mongoose.set("strictQuery", false);
|
||||
await mongoose.connect(MONGO_URL + "production", {
|
||||
authSource: "admin",
|
||||
const options: ConnectOptions = {
|
||||
appName: "Anonymous GitHub Server",
|
||||
compressors: "zstd",
|
||||
} as ConnectOptions);
|
||||
};
|
||||
if (!config.MONGODB_URI) options.authSource = "admin";
|
||||
await mongoose.connect(getMongoUrl(), options);
|
||||
isConnected = true;
|
||||
|
||||
return database;
|
||||
|
||||
Reference in New Issue
Block a user