import mongoose from "mongoose";
import WebSiteRepositoryMongo from "./_infra/WebSiteRepositoryMongo";
import RESTServer from "./_ui/RESTServer";
import WebSiteService from "./application/WebSiteService";
import config from "./_infra/config";
import WebSiteRepository from "./domain/WebSiteRepository";
import IdGeneratorServiceWithShortId from "./_infra/IdGeneratorServiceWithShortId";

// Init database
connectWithRetry();

const webSiteRepository: WebSiteRepository = new WebSiteRepositoryMongo();
const webSiteService = new WebSiteService(webSiteRepository);

function connectWithRetry(): Promise<void> {
    return mongoose.connect(config.database, { useUnifiedTopology: true, useNewUrlParser: true })
        .then(() => {
            console.log("Connecting to database : " + config.database );
        });
}

// Create RESTServer
const server = new RESTServer(config.port, webSiteService, new IdGeneratorServiceWithShortId());
server.start();
