2023-12-25 06:47:16 +00:00
|
|
|
import express from "express";
|
|
|
|
import mongoose from "mongoose";
|
|
|
|
import ImageModel from "./ImageModel";
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
app.get("/", (_, res) => {
|
|
|
|
res.json({ message: "Blazing fast 🚀" });
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/images", async (req, res) => {
|
|
|
|
try {
|
|
|
|
const allImages = await ImageModel.find();
|
|
|
|
res.json(allImages);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({ message: error });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-12-25 10:17:56 +00:00
|
|
|
app.post("/images", async (req, res) => {
|
|
|
|
try {
|
|
|
|
// Should add auth here before doing stuff
|
|
|
|
// Thowing a 401 if not auth provided
|
|
|
|
// throwing a 403 for incorrect auth
|
|
|
|
const image = await ImageModel.create(req.body);
|
|
|
|
res.json(image);
|
|
|
|
} catch (error) {
|
|
|
|
// Should return 409 Conflict for existing urls
|
|
|
|
// Should return 500 For other server errors
|
|
|
|
res.status(500).json({ message: error });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-12-25 06:47:16 +00:00
|
|
|
// Set the default port to 8080, or use the PORT environment variable
|
|
|
|
|
|
|
|
const start = async () => {
|
|
|
|
const port = process.env.PORT || 8080;
|
|
|
|
const mongo_uri: string = process.env.MONGODB_URI || "";
|
|
|
|
const mongo_user = process.env.MONGODB_USER;
|
|
|
|
const mongo_pass = process.env.MONGODB_PASS;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await mongoose.connect(mongo_uri, {
|
|
|
|
authSource: "admin",
|
|
|
|
user: mongo_user,
|
|
|
|
pass: mongo_pass
|
|
|
|
});
|
|
|
|
app.listen(port, () => console.log(`Express server listening on port ${port}`));
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start();
|
|
|
|
|