Created
June 19, 2017 08:18
-
-
Save botic/cec05bc0253700f27f73c325ab9d2271 to your computer and use it in GitHub Desktop.
Example to start HTTPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @fileoverview The main application module. Start the application with the | |
* following command line: | |
* | |
* ringo path/to/main.js | |
* | |
* This will start the application using default configuration values. To customize | |
* create a directory containing the files `config.json`, `log4j.properties`. | |
* Then start the application using the following command line: | |
* ringo path/to/main.js -h path/to/config/directory | |
*/ | |
const fs = require("fs"); | |
const engine = require("ringo/engine"); | |
const strings = require("ringo/utils/strings"); | |
const config = require("./config"); | |
const logging = require("ringo/logging"); | |
logging.setConfig(config.get("logging"), true); | |
const log = logging.getLogger(module.id); | |
const httpServer = require("httpserver"); | |
let server = null; | |
let store = null; | |
/** | |
* Initializes the http server. If run as a daemon, this function | |
* is called on daemon initialization. For daemons it runs as superuser | |
* to perform privileged actions like binding privileged ports < 1024. | |
*/ | |
const init = exports.init = function() { | |
log.info("Initializing application"); | |
// add all jar files in jars directory to classpath | |
getRepository(module.resolve("../jars/")).getResources().filter(function(r) { | |
return strings.endsWith(r.name, ".jar"); | |
}).forEach(function(file) { | |
addToClasspath(file); | |
}); | |
// configure the server | |
server = httpServer.build() | |
// serve applications | |
.serveApplication("/", module.resolve("./router-backend"), { | |
"virtualHosts": config.get("backend:vhosts") | |
}) | |
.serveApplication("/", module.resolve("./router-guestpass"), { | |
"virtualHosts": config.get("guestpass:vhosts") | |
}); | |
if (config.get("server:http") && config.get("server:http:host")) { | |
server.http({ | |
"host": config.get("server:http:host"), | |
"port": config.get("server:http:port") | |
}); | |
} | |
if (config.get("server:https") && config.get("server:https:host")) { | |
server.https({ | |
"host": config.get("server:https:host"), | |
"port": config.get("server:https:port"), | |
"keyStore": config.get("server:https:keyStore"), | |
"keyStorePassword": config.get("server:https:keyStorePassword"), | |
"keyManagerPassword": config.get("server:https:keyManagerPassword"), | |
"includeCipherSuites": config.get("server:https:includeCipherSuites") | |
}); | |
} | |
server.start(); | |
engine.addShutdownHook(function() { | |
stop(); | |
}); | |
log.info("Server started."); | |
}; | |
/** | |
* Called when the engine is started. | |
*/ | |
const start = exports.start = function() { | |
log.info("Initializing sql store ..."); | |
store = require("./data/store"); | |
let models = require("./data/models"); | |
if (engine.getRhinoEngine().getConfig().isReloading()) { | |
if (typeof(store.syncTables) === "function") { | |
log.info("Syncing tables ..."); | |
store.syncTables(); | |
} | |
} | |
log.info("Store initialized."); | |
}; | |
/** | |
* Called when the engine is shut down. | |
*/ | |
const stop = exports.stop = function() { | |
if (server !== null) { | |
server.stop(); | |
} | |
if (store !== null) { | |
store.connectionPool.close(); | |
} | |
log.info("Stopped application"); | |
}; | |
// script run from command line | |
if (require.main === module) { | |
init(); // prepares the http server and binds all ports | |
start(); // starts the sql store | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment