import consola from "consola";
import "reflect-metadata";
import { createExpressServer } from "routing-controllers";
import { config, e as errors } from './config';
import loaders from "./loaders/index";
import AuthChecker from './middlewares/AuthChecker';
import { ErrorHandler } from './middlewares/ErrorHandler';

const CONTROLLERS_FILE_EXTENSION = process.env.NODE_ENV === 'production' ? 'js' : 'ts';
const app = createExpressServer({
    middlewares: [ErrorHandler],
    authorizationChecker: AuthChecker,
    development: config.development,
    cors: true,
    controllers: [`${__dirname}/controllers/*.${CONTROLLERS_FILE_EXTENSION}`],
});

async function main() {
    await loaders(app);
    app.listen(config.internal_port, () => {
        consola.success(
            `⚡️[server]: Server is running at http://localhost:${config.internal_port}`
        );
    });
}
if (errors === 0) {
    main();
} else {
    consola.error("error");
    // something's wrong
}
