main.ts 2.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env node
/*
 * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blendb.
 *
 * blendb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * blendb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
 */

// external libraries
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
23
import express = require("express");
24
/** @hidden */
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
25
const osprey = require("osprey");
26
/** @hidden */
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
27
const ramlParser = require("raml-parser");
28 29

// load router
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
30
import { router } from "./api/router-v1";
31

32
// create a new express app
33
/** @hidden */
34 35
const app = module.exports = express();

36 37 38
// Load configuration file

import { ConfigParser } from "./util/configParser";
39 40 41

let configPath;

42
/** @hidden */
43
if (process.env.BLENDB_SCHEMA_FILE) {
44 45 46 47 48 49
    configPath = process.env.BLENDB_SCHEMA_FILE;
}
else{
    throw new Error("BLENDB_SCHEMA_FILE wasn't informed");
}

50
/** @hidden */
51 52 53 54
const config = ConfigParser.parse(configPath);

// include middlewares
import { EngineMw } from "./api/middlewares/engine";
55
import { PostgresMw, MonetMw } from "./api/middlewares/adapter";
56
import { ErrorMw } from "./api/middlewares/error";
57
import { CsvMw } from "./api/middlewares/csv";
Rafael Dias's avatar
Rafael Dias committed
58
import { LogMw } from "./api/middlewares/log"
59 60

app.use(EngineMw(config));
61
app.use(CsvMw());
Rafael Dias's avatar
Rafael Dias committed
62
app.use(LogMw());
63 64
if (config.adapters[0] === "postgres") {
    app.use(PostgresMw(config.connections[0]));
65 66
}

67 68
else if (config.adapters[0] === "monet") {
    app.use(MonetMw(config.connections[0]));
69 70
}

71
else {
72
    console.error("Invalid adapter. Options available are: postgres and monet");
73 74
    process.exit(1);
}
75

76
// parse the RAML spec and load osprey middleware
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
77
ramlParser.loadFile("specs/blendb-api-v1.raml")
78
    .then((raml: any) => {
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
79
        app.use("/v1",
80 81 82 83
            osprey.security(raml),
            osprey.server(raml),
            router);

84 85
        app.use("/v1", ErrorMw("v1"));

86 87
        let port = process.env.PORT || 3000;
        app.listen(port);
88

89 90
        if (app.get("env") === "development") {
            console.log("Server listening on port " + port + ".");
91 92 93
        }
    },
    (err: any) => {
Eduardo L. Buratti's avatar
Eduardo L. Buratti committed
94
        console.error("RAML Parsing Error: " + err.message);
95 96
        process.exit(1);
    });