Skip to content

Commit

Permalink
[FIX] Static routing
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarrc committed Feb 7, 2024
1 parent 30e6cd0 commit 6e9d52f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/node_modules
**/package-lock.json
**/.env
dockefile
8 changes: 5 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Db, Server, Socket } from "./server.js";

import Router from "./router.js";
import chalk from 'chalk';
import cluster from "cluster"
import dotenv from "dotenv";
import http from "http";
import os from "os";
import router from "./router.js";
import { setupMaster } from "@socket.io/sticky";
import { setupPrimary } from "@socket.io/cluster-adapter";

Expand Down Expand Up @@ -48,8 +48,10 @@ if (cluster.isPrimary) {

Db(MONGO_URL, purge).then(() => {
console.log(`${chalk.green.bold("[DB]")} ${chalk.green(`[${cluster.worker.id}]`)} Connection ready`);

const server = Server(router, PRODUCTION).listen(port, () => {

const router = Router(PRODUCTION);

const server = Server(router).listen(port, () => {
console.log(`${chalk.green.bold("[HTTP]")} ${chalk.green(`[${cluster.worker.id}]`)} Ready on ${BASE_URL}:${port}`);
Socket(server);
console.log(`${chalk.green.bold("[SOCKET]")} ${chalk.green(`[${cluster.worker.id}]`)} Ready on ${BASE_URL}:${port}`);
Expand Down
48 changes: 28 additions & 20 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ import express from "express";
import fs from 'fs';
import { join } from 'path'

const router = express.Router();

router.get("/ad", AdController.get.bind(AdController))

router.get("/ban", BanController.get.bind(BanController))
router.post("/ban", BanController.ban.bind(BanController))

router.post("/chat", ChatController.find.bind(ChatController))
router.get("/chat", ChatController.count.bind(ChatController))

router.get("/", (req,res) => {
const file = join(__dirname, "../../www", "index.html");
console.log(file)
process.env.NODE_ENV === "production" && fs.existsSync(file) ?
res.sendFile(file) :
res.send("OK");
})


export default router
const Router = (production) => {
const router = express.Router();

router.get("/ad", AdController.get.bind(AdController))

router.get("/ban", BanController.get.bind(BanController))
.post("/ban", BanController.ban.bind(BanController))

router.get("/chat", ChatController.count.bind(ChatController))
.post("/chat", ChatController.find.bind(ChatController))

if(production){
router.use('/', express.static(join(__dirname, "../../www")))
.use('/model', express.static(join(__dirname, "../../model")))
}

router.get("*", (req,res) => {
const file = join(__dirname, "../../www", "index.html");
console.log(file)
process.env.NODE_ENV === "production" && fs.existsSync(file) ?
res.sendFile(file) :
res.send("OK");
})

return router;
}

export default Router
6 changes: 1 addition & 5 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import chalk from 'chalk';
import cors from "cors";
import { createAdapter } from "@socket.io/cluster-adapter";
import express from "express";
import { join } from 'path'
import mongoose from "mongoose";
import { setupWorker } from "@socket.io/sticky";

Expand All @@ -28,15 +27,12 @@ const Db = (url, purge) => {
})
}

const Server = (router, production) => {
const Server = (router) => {
app.use(bodyparser.urlencoded({extended: true}))
.use(bodyparser.json())
.use(cors())
.use(router)

production && app.use('/', express.static(join(__dirname, "../../www")))
.use('/model', express.static(join(__dirname, "../../model")))

return app
}

Expand Down

0 comments on commit 6e9d52f

Please sign in to comment.