From e8865ae607d8ea4ba67b11805fb39575316fddbe Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 19 Mar 2021 10:24:17 +0100 Subject: [PATCH] continue v2 --- .gitignore | 2 ++ README.md | 5 ++++- docker-compose.yml | 20 ++++++++++++++++++-- index.js | 43 +++++++++++++++++++++++++------------------ routes/user.js | 4 ---- utils/repository.js | 1 - 6 files changed, 49 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 6d1513b..2e03175 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .env repositories/ repo/ +db_backups +message.txt # Created by https://www.gitignore.io/api/node # Edit at https://www.gitignore.io/?templates=node diff --git a/README.md b/README.md index 08945e4..ba771a7 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,14 @@ GITHUB_TOKEN= CLIENT_ID= CLIENT_SECRET= PORT=5000 +DB_USERNAME= +DB_PASSWORD= +AUTH_CALLBACK=http://localhost:5000/github/auth, ``` `GITHUB_TOKEN` can be generate here: https://github.com/settings/tokens/new with `repo` scope. `CLIENT_ID` and `CLIENT_SECRET` are the tokens are generated when you create a new GitHub app https://github.com/settings/applications/new. -The callback of the GitHub app needs to be defined as `https:///github/auth`. +The callback of the GitHub app needs to be defined as `https:///github/auth` (the same as defined in AUTH_CALLBACK). 3. Run Anonymous Github ```bash diff --git a/docker-compose.yml b/docker-compose.yml index 16eb7ed..2489588 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,8 +27,8 @@ services: ports: - "27017:27017" environment: - MONGO_INITDB_ROOT_USERNAME: root - MONGO_INITDB_ROOT_PASSWORD: rootpassword + MONGO_INITDB_ROOT_USERNAME: $DB_USERNAME + MONGO_INITDB_ROOT_PASSWORD: $DB_PASSWORD volumes: - mongodb_data_container:/data/db command: --quiet @@ -42,5 +42,21 @@ services: timeout: 10s retries: 5 + mongodb-backup: + image: tiredofit/db-backup + links: + - mongodb + volumes: + - ./db_backups:/backup + environment: + - DB_TYPE=mongo + - DB_HOST=mongodb + - DB_DUMP_FREQ=60 + - DB_CLEANUP_TIME=240 + - COMPRESSION=XZ + - DB_USER=$DB_USERNAME + - DB_PASS=$DB_PASSWORD + + restart: always volumes: mongodb_data_container: diff --git a/index.js b/index.js index d8803f7..34b00a9 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const path = require("path"); - +const ofs = require("fs"); +const fs = require("fs").promises; const redis = require("redis"); const RateLimit = require("express-rate-limit"); const RedisStore = require("rate-limit-redis"); @@ -22,16 +23,6 @@ const PORT = process.env.PORT || 5000; const app = express(); app.use(bodyParser.json()); app.use(compression()); -app.use( - new RateLimit({ - store: new RedisStore({ - client: rediscli, - }), - windowMs: 15 * 60 * 1000, // 15 minutes - max: 200, // limit each IP to 100 requests per windowMs - // delayMs: 0, // disable delaying - full speed until the max limit is reached - }) -); app.set("trust proxy", 1); // handle session and connection @@ -39,15 +30,24 @@ app.use(connection.session); app.use(connection.passport.initialize()); app.use(connection.passport.session()); -app.use("/github", connection.router); +const rateLimit = new RateLimit({ + store: new RedisStore({ + client: rediscli, + }), + windowMs: 15 * 60 * 1000, // 15 minutes + max: 200, // limit each IP to 100 requests per windowMs + // delayMs: 0, // disable delaying - full speed until the max limit is reached +}); + +app.use("/github", rateLimit, connection.router); // app routes -app.use("/api/user", require("./routes/user")); -app.use("/api/repo", require("./routes/file")); -app.use("/api/repo", require("./routes/repositoy")); +app.use("/api/user", rateLimit, require("./routes/user")); +app.use("/api/repo", rateLimit, require("./routes/file")); +app.use("/api/repo", rateLimit, require("./routes/repositoy")); // wesite view -app.use("/w/", require("./routes/webview")); +app.use("/w/", rateLimit, require("./routes/webview")); app.use(express.static(__dirname + "/public")); @@ -62,7 +62,7 @@ function exploreAppResponse(req, res) { res.sendFile(path.resolve(__dirname, "public", "explore.html")); } -app.get("/api/supportedTypes", async (req, res) => { +app.get("/api/supportedTypes", async (_, res) => { res.json( require("textextensions") .default.concat(fileUtils.additionalExtensions) @@ -70,7 +70,14 @@ app.get("/api/supportedTypes", async (req, res) => { ); }); -app.get("/api/stat", async (req, res) => { +app.get("/api/message", async (_, res) => { + if (ofs.existsSync("./message.txt")) { + return res.sendFile(path.resolve(__dirname, "message.txt")); + } + res.sendStatus(404); +}); + +app.get("/api/stat", async (_, res) => { const nbRepositories = await db .get("anonymized_repositories") .estimatedDocumentCount(); diff --git a/routes/user.js b/routes/user.js index 4fd3ba1..efd2b4d 100644 --- a/routes/user.js +++ b/routes/user.js @@ -41,10 +41,6 @@ router.get("/anonymized_repositories", async (req, res) => { repo.options.expirationDate != null && repo.options.expirationDate < new Date() ) { - console.log( - repo.options.expirationDate, - repo.options.expirationDate < new Date() - ); await repoUtils.updateStatus({ repoId: repo.repoId }, "expired"); repo.status = "expired"; } else { diff --git a/utils/repository.js b/utils/repository.js index eba0750..206b9e3 100644 --- a/utils/repository.js +++ b/utils/repository.js @@ -202,7 +202,6 @@ module.exports.getRepoCommit = async (options) => { force: options.force, }); if (!branches[repoConfig.branch]) { - console.log(branches, repoConfig.branch); throw "branch_not_found"; } return branches[repoConfig.branch].commit.sha;