1: UI works, receiving location updates

This commit is contained in:
metacryst
2025-10-31 19:51:09 -05:00
commit 152faaecee
13 changed files with 1253 additions and 0 deletions

46
server/auth.js Normal file
View File

@@ -0,0 +1,46 @@
import dotenv from 'dotenv';
import chalk from 'chalk';
import jwt from 'jsonwebtoken'
import { randomUUID } from 'node:crypto'
dotenv.config();
export default class AuthHandler {
ips = new Map()
#secret
constructor() {
this.#secret = process.env.JWT_SECRET || 'random-id-for-now-123013123u1o23o12i3ukjdsbvkfndijnx1ijs';
}
check(req, res) {
if(this.ips.get(req.headers["x-forwarded-for"])) {
console.log(chalk.green(" ", req.headers["x-forwarded-for"]))
return true
} else {
console.log(chalk.red(" ", req.headers["x-forwarded-for"]))
return false
}
}
login(req, res) {
const { login } = req.body;
if(login === process.env.LOGIN) {
this.ips.set(req.headers["x-forwarded-for"], new Date())
return true;
} else {
return false
}
}
sign(payload, options = {}) {
return jwt.sign(
payload,
this.#secret,
{ expiresIn: '30d', ...options }
);
}
verify(token) {
return jwt.verify(token, this.#secret)
}
}

77
server/db.js Normal file
View File

@@ -0,0 +1,77 @@
import chalk from 'chalk'
import path from 'path';
import fs from 'fs/promises';
import { pathToFileURL } from 'url';
import Node from "../model/Node.js"
export default class Database {
#nodes;
#edges;
#labels = {}
constructor() {
this.getData()
}
async getData() {
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
let dbJson;
try {
dbJson = JSON.parse(dbData);
} catch {
dbJson = []
}
this.#nodes = dbJson["nodes"];
this.#edges = dbJson["edges"];
console.log(chalk.yellow("DB established."))
Object.preventExtensions(this);
}
// superKey = "nodes" || "edges"
async writeData(superKey, key, value) {
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
let dbJson;
try {
dbJson = JSON.parse(dbData);
} catch {
dbJson = []
}
dbJson[superKey][key] = value;
await fs.writeFile(path.join(process.cwd(), 'db/db.json'), JSON.stringify(dbJson, null, 2), 'utf8')
}
async getLabelModels() {
const labelHandlers = {};
const labelDir = path.join(process.cwd(), 'src/model/labels');
const files = await fs.readdir(labelDir);
for (const file of files) {
if (!file.endsWith('.js')) continue;
const label = path.basename(file, '.js');
const modulePath = path.join(labelDir, file);
const module = await import(pathToFileURL(modulePath).href);
labelHandlers[label] = module.default;
if (!this.#labels[label]) {
this.#labels[label] = [];
}
}
return labelHandlers
}
generateUserID() {
let id = this.#labels["User"].length + 1;
while (this.get.user(`user-${id}`)) {
id++;
}
return `user-${id}`; // O(1) most of the time
}
async getAll() {
return { nodes: this.#nodes }
}
}

7
server/handlers.js Normal file
View File

@@ -0,0 +1,7 @@
const handlers = {
updateLocation(req, res) {
console.log("req received")
}
}
export default handlers;

152
server/index.js Normal file
View File

@@ -0,0 +1,152 @@
import express from 'express';
import cors from 'cors'
import http from 'http'
import chalk from 'chalk'
import moment from 'moment'
import path from 'path';
import httpProxy from 'http-proxy';
const proxy = httpProxy.createProxyServer({});
import { fileURLToPath } from 'url';
import Database from "./db.js"
import AuthHandler from './auth.js';
import handlers from "./handlers.js";
// Get __dirname in ES6 environment
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class Server {
db;
auth;
UIPath = path.join(__dirname, '../ui')
registerRoutes(router) {
router.post('/api/location', handlers.updateLocation)
router.post('/login', this.login)
router.get('/*', this.get)
return router
}
authMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: 'Authorization token required.' });
}
const [scheme, token] = authHeader.split(' ');
if (scheme !== 'Bearer' || !token) {
return res.status(401).json({ error: 'Malformed authorization header.' })
}
try {
const payload = this.auth.verify(token);
req.user = payload;
return next();
} catch (err) {
return res.status(403).json({ error: 'Invalid or expired token.' });
}
}
login = async (req, res) => {
if(this.auth.login(req, res)) {
res.writeHead(302, { 'Location': "/" }).end()
// res.status(200).send();
} else {
res.status(400).send();
}
}
get = async (req, res) => {
if(!this.auth.check(req, res)) {
if(req.url === "/") {
res.sendFile(path.join(this.UIPath, 'auth/auth.html'));
return;
} else if(req.url === "/betsyross.svg") {
res.sendFile(path.join(this.UIPath, '_/betsyross.svg'));
return
} else {
return
}
} else {
let url = req.url
if(url === "/") {
url = "/index.html"
}
let filePath;
if(url.startsWith("/_")) {
filePath = path.join(this.UIPath, url);
} else {
filePath = path.join(this.UIPath, "app", url);
}
res.sendFile(filePath, (err) => {
if (err) {
console.error(`Error serving ${filePath}:`, err);
res.status(err.status || 500).send('File not found or error serving file.');
}
});
}
}
logRequest(req, res, next) {
const formattedDate = moment().format('M.D');
const formattedTime = moment().format('h:mma');
if(req.url.includes("/api/")) {
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
} else {
if(req.url === "/")
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
}
next();
}
logResponse(req, res, next) {
const originalSend = res.send;
res.send = function (body) {
if(res.statusCode >= 400) {
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
} else {
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
}
originalSend.call(this, body);
};
next();
}
constructor() {
this.db = new Database()
this.auth = new AuthHandler()
const app = express();
app.use(cors({ origin: '*' }));
app.use(express.json());
app.use(this.logRequest);
app.use(this.logResponse);
let router = express.Router();
this.registerRoutes(router)
app.use('/', router);
const server = http.createServer(app);
const PORT = 3008;
server.listen(PORT, () => {
console.log("\n")
console.log(chalk.yellow("**************America****************"))
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
console.log(chalk.yellow("***************************************"))
console.log("\n")
});
process.on('SIGINT', async () => {
console.log(chalk.red('Closing server...'));
console.log(chalk.green('Database connection closed.'));
process.exit(0);
});
Object.preventExtensions(this);
}
}
const server = new Server()