Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Move files to /src
Browse files Browse the repository at this point in the history
  • Loading branch information
Sw1ndlers committed Nov 8, 2023
1 parent 9a64964 commit e645935
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 103 deletions.
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{
"name": "zumo-cat-backend",
"version": "1.0.0",
"main": "main.js",
"main": "src/main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "cross-env NODE_ENV=development node main.js",
"prod": "cross-env NODE_ENV=production node main.js",
"dev": "cross-env NODE_ENV=development node ./src/main.js",
"prod": "cross-env NODE_ENV=production node ./src/main.js",
"pm2": "pm2 start npm --name \"main.js\" -- run prod"
},
"_moduleAliases": {
"@root": ".",
"@static": "./static"
},
"imports": {},
"author": "sw1ndler",
"license": "MIT",
"dependencies": {
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

178 changes: 89 additions & 89 deletions main.js → src/main.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
// -- Pre Init

import { environment } from "./src/environment.js";

// -- Imports

import Logger from "./src/logger.js";
import { InitMongo } from "./src/mongoose.js";
import { requireAuth } from "./src/routes/auth.js";

import cors from "cors";
import path from "path";
import express from "express";
import request from "sync-request";
import busboy from "connect-busboy";
import rateLimit from "express-rate-limit";

// -- Constants --

const env = process.env
const rootPath = path.resolve(path.dirname("."));

const SERVER_PORT = parseInt(env.PORT) || 4040;
const IS_PRODUCTION = env.PRODUCTION == "true" || false;

const RATE_LIMIT_MAX = parseInt(env.RATE_LIMIT_MAX) || 100;
const RATE_LIMIT_DELAY = parseInt(env.RATE_LIMIT_DELAY) || 60 * 5000;

global.ROOT_PATH = rootPath;

Logger.info("Environment Variables: ")
Logger.info(` Port: ${SERVER_PORT}`)
Logger.info(` Production: ${IS_PRODUCTION}`)
Logger.info(` Rate Limit:`)
Logger.info(` Max: ${RATE_LIMIT_MAX}`)
Logger.info(` Delay: ${RATE_LIMIT_DELAY}\n`)

Logger.info(`[Server] Running in ${environment} mode`);

// -- Main --

const app = express();

app.use(busboy()) // Parse multipart/form-data
app.use(cors()) // Allow cross origin requests

app.use(express.json({limit: '8mb'}));
app.use(express.urlencoded({limit: '8mb', extended: true }));

// -- Routes --

const rateLimiter = rateLimit({
windowMs: 60 * 5000, // 5 minute
max: 100, // 100 requests per 5 minutes
handler: (_req, res) => {
res.status(429).send(
JSON.stringify({ code: 429, message: "Too many requests" })
);
},
});

const routes = {
root: (await import("./src/routes/root.js")).default,
upload: (await import("./src/routes/upload.js")).default,
random: (await import("./src/routes/random.js")).default,
getLatest: (await import("./src/routes/latest.js")).default,
};

app.get("/", routes.root);
app.get("/random", rateLimiter, routes.random);
app.get("/latest", rateLimiter, routes.getLatest);
app.post("/upload", requireAuth, routes.upload);

// -- Init --

async function main() {
await InitMongo(); // Connect to mongodb

app.listen(SERVER_PORT, () => {
let publicIp = request("GET", "https://api.ipify.org").getBody()
let ipAddress = IS_PRODUCTION
? `${publicIp}:${SERVER_PORT}`
: `localhost:${SERVER_PORT}`;

Logger.info(`[Server] Running on ${ipAddress}\n`);
});
}

main();
// -- Pre Init

import { environment } from "./utils/environment.js";

// -- Imports

import Logger from "./utils/logger.js";
import { InitMongo } from "./utils/mongoose.js";
import { requireAuth } from "./routes/auth.js";

import cors from "cors";
import path from "path";
import express from "express";
import request from "sync-request";
import busboy from "connect-busboy";
import rateLimit from "express-rate-limit";

// -- Constants --

const env = process.env
const rootPath = path.resolve(path.dirname("."));

const SERVER_PORT = parseInt(env.PORT) || 4040;
const IS_PRODUCTION = env.PRODUCTION == "true" || false;

const RATE_LIMIT_MAX = parseInt(env.RATE_LIMIT_MAX) || 100;
const RATE_LIMIT_DELAY = parseInt(env.RATE_LIMIT_DELAY) || 60 * 5000;

global.ROOT_PATH = rootPath;

Logger.info("Environment Variables: ")
Logger.info(` Port: ${SERVER_PORT}`)
Logger.info(` Production: ${IS_PRODUCTION}`)
Logger.info(` Rate Limit:`)
Logger.info(` Max: ${RATE_LIMIT_MAX}`)
Logger.info(` Delay: ${RATE_LIMIT_DELAY}\n`)

Logger.info(`[Server] Running in ${environment} mode`);

// -- Main --

const app = express();

app.use(busboy()) // Parse multipart/form-data
app.use(cors()) // Allow cross origin requests

app.use(express.json({limit: '8mb'}));
app.use(express.urlencoded({limit: '8mb', extended: true }));

// -- Routes --

const rateLimiter = rateLimit({
windowMs: 60 * 5000, // 5 minute
max: 100, // 100 requests per 5 minutes
handler: (_req, res) => {
res.status(429).send(
JSON.stringify({ code: 429, message: "Too many requests" })
);
},
});

const routes = {
root: (await import("./routes/root.js")).default,
upload: (await import("./routes/upload.js")).default,
random: (await import("./routes/random.js")).default,
getLatest: (await import("./routes/latest.js")).default,
};

app.get("/", routes.root);
app.get("/random", rateLimiter, routes.random);
app.get("/latest", rateLimiter, routes.getLatest);
app.post("/upload", requireAuth, routes.upload);

// -- Init --

async function main() {
await InitMongo(); // Connect to mongodb

app.listen(SERVER_PORT, () => {
let publicIp = request("GET", "https://api.ipify.org").getBody()
let ipAddress = IS_PRODUCTION
? `${publicIp}:${SERVER_PORT}` // Display public ip if in production
: `localhost:${SERVER_PORT}`; // Display localhost if in development

Logger.info(`[Server] Running on ${ipAddress}\n`);
});
}

main();
4 changes: 2 additions & 2 deletions src/routes/latest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CatModel } from "../mongoose.js";
import { getModelJson } from "../mongoose.js";
import { CatModel } from "../utils/mongoose.js";
import { getModelJson } from "../utils/mongoose.js";

export default async function (_req, res) {
let cats = await CatModel.find({}).sort({ uploadTime: -1 }).limit(30);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/random.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CatModel } from "../mongoose.js";
import { storage } from "../firebase.js";
import { CatModel } from "../utils/mongoose.js";
import { storage } from "../utils/firebase.js";
import { ref, getDownloadURL } from "firebase/storage";

export default async function (_req, res) {
Expand Down
1 change: 0 additions & 1 deletion src/routes/root.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';


const content = fs.readFileSync(global.ROOT_PATH + "/static/index.html", 'utf8');

export default function (_req, res) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/upload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { uploadImage } from "../firebase.js";
import { uploadImage } from "../utils/firebase.js";

export default function (req, res, _next) {
// Pipe the request to busboy
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/firebase.js → src/utils/firebase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getStorage, ref, uploadBytes } from "firebase/storage";
import { initializeApp } from "firebase/app";
import { assertWarn } from "./utility.js";
import { assertWarn } from "./functions.js";
import { CatModel } from "./mongoose.js";
import Logger from "./logger.js";

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e645935

Please sign in to comment.