{"size": 1326, "ext": "js", "max_stars_repo_path": "src/server.js", "max_stars_repo_name": "LendMark-sk/API", "max_stars_repo_stars_event_min_datetime": "2021-11-04T15:42:04.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-04T15:42:04.000Z", "max_issues_repo_path": "src/server.js", "max_issues_repo_name": "LendMark-sk/API", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/server.js", "max_forks_repo_name": "LendMark-sk/API", "max_forks_repo_forks_event_min_datetime": "2021-11-04T15:09:38.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-05T14:29:50.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 25.5, "max_line_length": 88, "alphanum_fraction": 0.4577677225}
const express = require("express");
const fs = require('fs');
const MySQL = require('./managers/MySQL');
const format = require("./managers/Formatter");

class Server {
    constructor() {
        this.app = express();
        this.app.format = format;

        this.routes = new Map();
        this.mysql = new MySQL();

        this.loadRoutes();
        this.route();

        this.app.listen(80);
    }

    loadRoutes() {
        let dir = fs.readdirSync(`${__dirname}/routes/`).filter(f => f !== "Base.js");
        for(let route of dir) {
            const file = new(require(`./routes/${route}`));
            this.routes.set(file.name, file);
        }
    
        return true;
    }

    route() {
        this.app.get('/', (req, res) => {
            this.app.format(req, res, {
                status: '200',
                statusText: 'OK'
            })
        })
        
        this.app.get(`/api/v1/*`, (req, res) => {
            let url = req.url.slice(8).split('?')[0];
            this.routes?.get(url)?.run({
                app: this.app,
                mysql: this.mysql,
                format: (data, status = 200) => this.app.format(req, res, data, status),
                query: req.query,
                req,
                res
            });
        });
    }
}

module.exports = Server;