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';
import UserChecker from './middlewares/UserChecker';

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

async function main() {
  await loaders(app);
  if (config.testing) {
    consola.info("🛠[config]: Discovered testing env. Mailing errors will get ignored!")
  }
  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
}
