Cleanup
This commit is contained in:
parent
ec5a31cbc9
commit
88a7e70ead
11 changed files with 135 additions and 112 deletions
18
backend/.envrc
Normal file
18
backend/.envrc
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
export APP_NAME='OpenCommerce Dev'
|
||||||
|
export LOGO="https://pbs.twimg.com/profile_images/1347273728141500418/mClA3m5v_400x400.png"
|
||||||
|
export URL=localhost:3000
|
||||||
|
|
||||||
|
export MONGO_URI=localhost
|
||||||
|
export MONGO_COLLECTION=OpenCommerce
|
||||||
|
|
||||||
|
export JWT_SECRET=asdf
|
||||||
|
|
||||||
|
export URL=localhost:5000
|
||||||
|
|
||||||
|
export NODEMAILER_ADDRESS=keycloak@forgeserv.net
|
||||||
|
export NODEMAILER_PASSWORD=crah.spur4vaf.HOUD
|
||||||
|
|
||||||
|
export AWS_REGION=us-east-2
|
||||||
|
export AWS_BUCKET=jstovall-opencommerce-dev
|
||||||
|
export AWS_ACCESS_KEY_ID=AKIAT6IFHPMGHY3TOAAG
|
||||||
|
export AWS_SECRET_ACCESS_KEY=dt9aSva+39cqKP8N2m7YIM3Aqxi9eg/Y9scAkqjR
|
||||||
|
|
@ -25,7 +25,6 @@
|
||||||
"aws-sdk": "^2.1116.0",
|
"aws-sdk": "^2.1116.0",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.0.0",
|
|
||||||
"email-validator": "^2.0.4",
|
"email-validator": "^2.0.4",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,10 @@
|
||||||
import { config as LoadDotenv } from "dotenv";
|
|
||||||
|
|
||||||
import { json, urlencoded } from "body-parser";
|
import { json, urlencoded } from "body-parser";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import winston from "winston";
|
|
||||||
import Routes from "./app/controllers";
|
import Routes from "./app/controllers";
|
||||||
import AccessLog from "./app/middleware/AccessLog";
|
import AccessLog from "./app/middleware/AccessLog";
|
||||||
import UserInjection from "./app/middleware/UserInjection";
|
import UserInjection from "./app/middleware/UserInjection";
|
||||||
import { UserModel } from "./app/models/User";
|
import { UserModel } from "./app/models/User";
|
||||||
import { LOGGER } from "./app/util/Logger";
|
|
||||||
import Multify from "./multer";
|
|
||||||
|
|
||||||
interface AuthenticatableRequest {
|
interface AuthenticatableRequest {
|
||||||
user?: UserModel;
|
user?: UserModel;
|
||||||
|
|
@ -21,46 +16,36 @@ declare global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "production") {
|
export default () => {
|
||||||
LoadDotenv();
|
const app = express();
|
||||||
LOGGER.add(
|
|
||||||
new winston.transports.Console({
|
|
||||||
format: winston.format.simple(),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const app = express();
|
app.use(cors());
|
||||||
|
app.use(json());
|
||||||
|
app.use(urlencoded({ extended: true }));
|
||||||
|
app.use(UserInjection());
|
||||||
|
app.use(AccessLog());
|
||||||
|
|
||||||
app.use(cors());
|
Routes.forEach(({ path, httpMethod, middleware, handler }) => {
|
||||||
app.use(json());
|
switch (httpMethod) {
|
||||||
app.use(urlencoded({ extended: true }));
|
case "GET":
|
||||||
app.use(UserInjection());
|
app.get(path, ...(middleware || []), handler);
|
||||||
app.use(AccessLog());
|
break;
|
||||||
|
case "POST":
|
||||||
console.log("-------------------------------------------------");
|
app.post(path, ...(middleware || []), handler);
|
||||||
Multify(app);
|
break;
|
||||||
Routes.forEach(({ path, httpMethod, handler }) => {
|
case "DELETE":
|
||||||
switch (httpMethod) {
|
app.delete(path, ...(middleware || []), handler);
|
||||||
case "GET":
|
break;
|
||||||
app.get(path, handler);
|
case "PUT":
|
||||||
break;
|
app.put(path, ...(middleware || []), handler);
|
||||||
case "POST":
|
break;
|
||||||
app.post(path, handler);
|
case "PATCH":
|
||||||
break;
|
app.patch(path, ...(middleware || []), handler);
|
||||||
case "DELETE":
|
break;
|
||||||
app.delete(path, handler);
|
}
|
||||||
break;
|
console.log("-------------------------------------------------");
|
||||||
case "PUT":
|
console.log(`${httpMethod}: ${path}`);
|
||||||
app.put(path, handler);
|
});
|
||||||
break;
|
|
||||||
case "PATCH":
|
|
||||||
app.patch(path, handler);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
console.log("-------------------------------------------------");
|
console.log("-------------------------------------------------");
|
||||||
console.log(`${httpMethod}: ${path}`);
|
return app;
|
||||||
});
|
};
|
||||||
console.log("-------------------------------------------------");
|
|
||||||
|
|
||||||
export default app;
|
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,20 @@ import { Request, Response } from "express";
|
||||||
import ProductRoutes from "./product";
|
import ProductRoutes from "./product";
|
||||||
import ReviewRoutes from "./review";
|
import ReviewRoutes from "./review";
|
||||||
import UserRoutes from "./user";
|
import UserRoutes from "./user";
|
||||||
|
import S3Routes from "./s3";
|
||||||
|
|
||||||
export type RouteCollection = {
|
export type RouteCollection = {
|
||||||
httpMethod: "GET" | "POST" | "DELETE" | "PUT" | "PATCH";
|
httpMethod: "GET" | "POST" | "DELETE" | "PUT" | "PATCH";
|
||||||
path: string;
|
path: string;
|
||||||
handler: (arg1: Request, arg2: Response) => any;
|
handler: (arg1: Request, arg2: Response) => any;
|
||||||
|
middleware?: any[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const routes: RouteCollection[] = [
|
const routes: RouteCollection[] = [
|
||||||
...ProductRoutes,
|
...ProductRoutes,
|
||||||
...UserRoutes,
|
...UserRoutes,
|
||||||
...ReviewRoutes,
|
...ReviewRoutes,
|
||||||
|
...S3Routes,
|
||||||
];
|
];
|
||||||
|
|
||||||
export default routes;
|
export default routes;
|
||||||
|
|
|
||||||
16
backend/src/app/controllers/s3/crud/get.ts
Normal file
16
backend/src/app/controllers/s3/crud/get.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import getS3Instace from "../../../util/S3";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
if (!req.params.key) {
|
||||||
|
return res.status(404).send();
|
||||||
|
}
|
||||||
|
|
||||||
|
const signedUrl = await getS3Instace().getSignedUrlPromise("getObject", {
|
||||||
|
Bucket: process.env.AWS_BUCKET,
|
||||||
|
Key: req.params.key,
|
||||||
|
Expires: 604800,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.redirect(signedUrl);
|
||||||
|
};
|
||||||
8
backend/src/app/controllers/s3/crud/post.ts
Normal file
8
backend/src/app/controllers/s3/crud/post.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import S3FileLink from "../../../models/S3FileLink";
|
||||||
|
|
||||||
|
export default async (req: Request, res: Response) => {
|
||||||
|
// const f = new S3FileLink({ key: (req.file as any).key });
|
||||||
|
// await f.save();
|
||||||
|
return res.status(200).json({ output: (req.file as any).key });
|
||||||
|
};
|
||||||
36
backend/src/app/controllers/s3/index.ts
Normal file
36
backend/src/app/controllers/s3/index.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { RouteCollection } from "..";
|
||||||
|
import post from "./crud/post";
|
||||||
|
import get from "./crud/get";
|
||||||
|
import moment from "moment";
|
||||||
|
import multer from "multer";
|
||||||
|
import multerS3 from "multer-s3";
|
||||||
|
import getS3Instace from "../../util/S3";
|
||||||
|
|
||||||
|
const uploader = multer({
|
||||||
|
storage: multerS3({
|
||||||
|
s3: getS3Instace(),
|
||||||
|
bucket: process.env.AWS_BUCKET,
|
||||||
|
key: function (req, file, cb) {
|
||||||
|
cb(null, `${moment().toISOString()}-${file.originalname}`);
|
||||||
|
},
|
||||||
|
metadata: function (req, file, cb) {
|
||||||
|
cb(null, { fieldName: file.fieldname });
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const routes: RouteCollection[] = [
|
||||||
|
{
|
||||||
|
httpMethod: "GET",
|
||||||
|
path: "/api/v1/fileupload/:key",
|
||||||
|
handler: get,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
httpMethod: "POST",
|
||||||
|
path: "/api/v1/fileupload",
|
||||||
|
handler: post,
|
||||||
|
middleware: [uploader.single("file")],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export default routes;
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
import { Document, model, Schema } from "mongoose";
|
|
||||||
|
|
||||||
export type FileModel = Document & { key: string };
|
|
||||||
|
|
||||||
const FileSchema = new Schema<FileModel>({
|
|
||||||
key: { type: String, required: true },
|
|
||||||
});
|
|
||||||
|
|
||||||
export default model<FileModel>("files", FileSchema, "files");
|
|
||||||
20
backend/src/app/util/S3.ts
Normal file
20
backend/src/app/util/S3.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import aws from "aws-sdk";
|
||||||
|
|
||||||
|
let cached: aws.S3 | null = null;
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
const S3 = new aws.S3({
|
||||||
|
region: process.env.AWS_REGION,
|
||||||
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||||
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||||
|
apiVersion: "2006-03-01",
|
||||||
|
signatureVersion: "v4",
|
||||||
|
});
|
||||||
|
|
||||||
|
cached = S3;
|
||||||
|
return cached;
|
||||||
|
};
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
import aws from "aws-sdk";
|
|
||||||
import { Express } from "express";
|
|
||||||
import moment from "moment";
|
|
||||||
import multer from "multer";
|
|
||||||
import multers3 from "multer-s3";
|
|
||||||
import S3FileLink from "./app/models/S3FileLink";
|
|
||||||
|
|
||||||
export default (app: Express) => {
|
|
||||||
const s3 = new aws.S3({
|
|
||||||
region: process.env.AWS_REGION,
|
|
||||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
||||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
||||||
apiVersion: "2006-03-01",
|
|
||||||
signatureVersion: "v4",
|
|
||||||
});
|
|
||||||
|
|
||||||
const uploader = multer({
|
|
||||||
storage: multers3({
|
|
||||||
s3,
|
|
||||||
bucket: process.env.AWS_BUCKET,
|
|
||||||
key: function (req, file, cb) {
|
|
||||||
cb(null, `${moment().toISOString()}-${file.originalname}`);
|
|
||||||
},
|
|
||||||
metadata: function (req, file, cb) {
|
|
||||||
cb(null, { fieldName: file.fieldname });
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Special endpoint for the image upload
|
|
||||||
app.post("/api/v1/fileupload", uploader.single("file"), async (req, res) => {
|
|
||||||
const f = new S3FileLink({ key: (req.file as any).key });
|
|
||||||
await f.save();
|
|
||||||
return res.status(200).json({ output: f.key });
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api/v1/fileupload/:key", async (req, res) => {
|
|
||||||
if (!req.params.key) {
|
|
||||||
return res.status(404).send();
|
|
||||||
}
|
|
||||||
|
|
||||||
const signedUrl = await s3.getSignedUrlPromise("getObject", {
|
|
||||||
Bucket: process.env.AWS_BUCKET,
|
|
||||||
Key: req.params.key,
|
|
||||||
Expires: 604800,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.redirect(signedUrl);
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("GET /api/v1/fileupload/:key");
|
|
||||||
console.log("-------------------------------------------------");
|
|
||||||
console.log("POST /api/v1/fileupload");
|
|
||||||
};
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import app from "./app";
|
import BuildApp from "./app";
|
||||||
import { LOGGER } from "./app/util/Logger";
|
import { LOGGER } from "./app/util/Logger";
|
||||||
import ConnectDb from "./db";
|
import ConnectDb from "./db";
|
||||||
|
|
||||||
|
const app = BuildApp();
|
||||||
ConnectDb();
|
ConnectDb();
|
||||||
|
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue