Skip to content

Commit

Permalink
Integrate backend question service with frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
guanquann committed Sep 20, 2024
1 parent 3f1072c commit 976fcf1
Show file tree
Hide file tree
Showing 13 changed files with 1,832 additions and 1,014 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ dist-ssr

# Environment files
.env
firebase.json
4 changes: 4 additions & 0 deletions backend/question-service/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import dotenv from "dotenv";
import swaggerUi from "swagger-ui-express";
import yaml from "yaml";
import fs from "fs";
import cors from "cors";

import connectDB from "./config/db.ts";
import questionRoutes from "./src/routes/questionRoutes.ts";
Expand All @@ -16,6 +17,9 @@ const app = express();

connectDB();

app.use(cors());
app.options("*", cors());

app.use(express.json());
app.use("/api", questionRoutes);
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
Expand Down
42 changes: 42 additions & 0 deletions backend/question-service/config/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import admin from "firebase-admin";
import { v4 as uuidv4 } from "uuid";
import serviceAccount from "../firebase.json";

admin.initializeApp({
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount),
storageBucket: "gs://peerprep-c3bd1.appspot.com",
});

const bucket = admin.storage().bucket();

const uploadFileToFirebase = async (
file: Express.Multer.File,
): Promise<string> => {
return new Promise((resolve, reject) => {
const fileName = uuidv4();
const ref = bucket.file(fileName);

const blobStream = ref.createWriteStream({
metadata: {
contentType: file.mimetype,
},
});

blobStream.on("error", (error) => {
reject(error);
});

blobStream.on("finish", async () => {
try {
await ref.makePublic();
resolve(`https://storage.googleapis.com/${bucket.name}/${fileName}`);
} catch (error) {
reject(error);
}
});

blobStream.end(file.buffer);
});
};

export { bucket, uploadFileToFirebase };
6 changes: 6 additions & 0 deletions backend/question-service/config/multer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import multer from "multer";

const storage = multer.memoryStorage();
const upload = multer({ storage }).array("images[]");

export { upload };
Loading

0 comments on commit 976fcf1

Please sign in to comment.