This commit is contained in:
@@ -1,39 +1,67 @@
|
||||
const express = require("express");
|
||||
const cors = require('cors');
|
||||
const mongoose = require('mongoose');
|
||||
const cors = require("cors");
|
||||
const mongoose = require("mongoose");
|
||||
const { createServer } = require("http");
|
||||
const { Server } = require("socket.io");
|
||||
|
||||
const dotenv = require('dotenv');
|
||||
const dotenv = require("dotenv");
|
||||
|
||||
if(process.env.NODE_ENV) {
|
||||
if (process.env.NODE_ENV) {
|
||||
dotenv.config({
|
||||
path: `.env.${process.env.NODE_ENV}`
|
||||
path: `.env.${process.env.NODE_ENV}`,
|
||||
});
|
||||
} else {
|
||||
dotenv.config();
|
||||
}
|
||||
|
||||
const app = express();
|
||||
const httpServer = createServer(app);
|
||||
const io = new Server(httpServer, {
|
||||
cors: {
|
||||
origin: true,
|
||||
credentials: true,
|
||||
},
|
||||
});
|
||||
|
||||
const connectDB = require("./db");
|
||||
const gridCellsRouter = require("./routes/gridCells");
|
||||
|
||||
gridCellsRouter.setIO(io);
|
||||
|
||||
// connect database
|
||||
connectDB();
|
||||
|
||||
app.use(cors({
|
||||
origin: 'http://localhost:3000',
|
||||
credentials: true, // if using cookies/auth
|
||||
}));
|
||||
app.use(
|
||||
cors({
|
||||
origin: true,
|
||||
credentials: true,
|
||||
})
|
||||
);
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
// Socket.IO
|
||||
io.on("connection", (socket) => {
|
||||
console.log(`Socket connected: ${socket.id}`);
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log(`Socket disconnected: ${socket.id}`);
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/api/test", (req, res) => {
|
||||
console.log("Hey");
|
||||
res.json({"message": "Hello from backend!"});
|
||||
res.json({ message: "Hello from backend!" });
|
||||
});
|
||||
|
||||
app.use("/api/grid-cells", gridCellsRouter);
|
||||
|
||||
app.get("/api/status", (req, res) => {
|
||||
const mem = process.memoryUsage();
|
||||
const uptime = Math.floor(process.uptime());
|
||||
const hours = Math.floor(uptime / 3600);
|
||||
const minutes = Math.floor((uptime % 3600) / 60);
|
||||
|
||||
|
||||
mongoose.connection.readyState === 1
|
||||
? res.json({
|
||||
status: "online",
|
||||
@@ -55,6 +83,7 @@ app.get("/api/status", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(5000, () => {
|
||||
const PORT = process.env.PORT || 5000;
|
||||
httpServer.listen(PORT, () => {
|
||||
console.log("Server running on port 5000");
|
||||
});
|
||||
67
backend/src/routes/gridCells.js
Normal file
67
backend/src/routes/gridCells.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const express = require("express");
|
||||
const GridCell = require("../schemas/GridCell");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
let io;
|
||||
router.setIO = (_io) => {
|
||||
io = _io;
|
||||
};
|
||||
|
||||
// GET /api/grid-cells
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const { minx, miny, maxx, maxy } = req.query;
|
||||
if (minx != null && miny != null && maxx != null && maxy != null) {
|
||||
const query = {};
|
||||
if (minx !== "" && !isNaN(parseInt(minx))) query.x = { $gte: parseInt(minx) };
|
||||
if (miny !== "" && !isNaN(parseInt(miny))) query.y = { $gte: parseInt(miny) };
|
||||
if (maxx !== "" && !isNaN(parseInt(maxx))) {
|
||||
query.x = query.x || {};
|
||||
query.x.$lte = parseInt(maxx);
|
||||
}
|
||||
if (maxy !== "" && !isNaN(parseInt(maxy))) {
|
||||
query.y = query.y || {};
|
||||
query.y.$lte = parseInt(maxy);
|
||||
}
|
||||
const cells = await GridCell.find(query)
|
||||
.sort({ x: 1, y: 1 })
|
||||
.limit(10000)
|
||||
.lean();
|
||||
return res.json(cells);
|
||||
}
|
||||
const limit = Math.min(parseInt(req.query.limit) || 1000, 1000);
|
||||
const cells = await GridCell.find()
|
||||
.sort({ x: 1, y: 1 })
|
||||
.limit(limit)
|
||||
.lean();
|
||||
res.json(cells);
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/grid-cells/paint — upsert (create or update a grid cell)
|
||||
router.post("/paint", async (req, res) => {
|
||||
console.log("Hola")
|
||||
try {
|
||||
const { x, y, color } = req.body;
|
||||
if (x == null || y == null || color == null)
|
||||
return res.status(400).json({ error: "x, y, and color are required" });
|
||||
const validated = await GridCell.findOneAndUpdate(
|
||||
{ x, y },
|
||||
{ x, y, color },
|
||||
{ returnDocument: 'after', upsert: true }
|
||||
);
|
||||
|
||||
if (io) {
|
||||
io.emit("grid-cell-paint", { x, y, color });
|
||||
}
|
||||
|
||||
res.json(validated);
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
21
backend/src/schemas/GridCell.js
Normal file
21
backend/src/schemas/GridCell.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const gridCellSchema = new mongoose.Schema(
|
||||
{
|
||||
x: { type: Number, required: true, min: 1, max: 90 },
|
||||
y: { type: Number, required: true, min: 1, max: 90},
|
||||
color: { type: Number, required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
gridCellSchema.index({ x: 1, y: 1 }, { unique: true });
|
||||
|
||||
gridCellSchema.methods.toDisplayColor = function () {
|
||||
if (this.color.startsWith("#")) return this.color;
|
||||
return `#${this.color}`;
|
||||
};
|
||||
|
||||
const GridCell = mongoose.model("GridCell", gridCellSchema);
|
||||
|
||||
module.exports = GridCell;
|
||||
Reference in New Issue
Block a user