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


// Init database
connectWithRetry();

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

function connectWithRetry() {
    return mongoose.connect(config.database, { useUnifiedTopology: true, useNewUrlParser: true })
        .then(() => {
            // tslint:disable-next-line: no-console
            console.log("Connecting to database : " + config.database );
        });
}

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