Config file can now be in /etc/xo-server & basic help message.

This commit is contained in:
Julien Fontanet
2014-08-14 16:05:23 +02:00
parent 7d72e196e0
commit f07cbe0087
5 changed files with 77 additions and 44 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/node_modules/
npm-debug.log
.xo-server.*

View File

@@ -23,31 +23,36 @@
"url": "git://github.com/vatesfr/xo-server.git"
},
"dependencies": {
"app-conf": "^0.1.3",
"backoff": "~2.4.0",
"bluebird": "^2.2.2",
"chalk": "^0.5.1",
"coffee-script": "~1.7.1",
"connect": "^3.1.0",
"event-to-promise": "^0.3.0",
"exec-promise": "^0.3.0",
"compiled-accessors": "^0.2.0",
"connect": "^2.25.5",
"event-to-promise": "^0.3.1",
"exec-promise": "^0.4.0",
"extendable": "~0.0.6",
"fibers": "~1.0.1",
"hashy": "~0.3.6",
"http-server-plus": "^0.2.3",
"js-yaml": "~3.1.0",
"lodash.isfunction": "^2.4.1",
"lodash.map": "^2.4.1",
"nconf": "~0.6.9",
"require-tree": "~0.3.3",
"schema-inspector": "^1.4.5",
"serve-static": "^1.4.0",
"schema-inspector": "^1.4.8",
"serve-static": "^1.5.1",
"then-redis": "~0.3.12",
"underscore": "~1.6.0",
"ws": "~0.4.31",
"ws": "~0.4.32",
"xml2js": "~0.4.4",
"xmlrpc": "~1.2.0"
},
"devDependencies": {
"chai": "~1.9.1",
"glob": "~4.0.4",
"mocha": "^1.21.0",
"glob": "~4.0.5",
"mocha": "^1.21.4",
"node-inspector": "^0.7.4",
"sinon": "^1.10.3"
},

View File

@@ -1,4 +1,13 @@
# Note: Relative paths will be resolved from XO-Server's directory.
# Example XO-Server configuration.
# This file is automatically looking for at the following places:
# - `./.xo-server.yaml` up to `/.xo-server.yaml`
# - `$HOME/.config/xo-server/config.yaml`
# - `/etc/xo-server/config.yaml`
#
# The first entries have priority.
# Note: paths are relative to the configuration file.
#=====================================================================

View File

@@ -6,15 +6,16 @@ $fs = require 'fs'
# Low level tools.
$_ = require 'underscore'
$appConf = require 'app-conf'
$chalk = require 'chalk'
# HTTP(s) middleware framework.
$connect = require 'connect'
$serveStatic = require 'serve-static'
$eventToPromise = require 'event-to-promise'
# Configuration handling.
$nconf = require 'nconf'
$Promise = require 'bluebird'
$Promise.longStackTraces()
@@ -33,6 +34,8 @@ $XO = require './xo'
# Helpers for dealing with fibers.
{$fiberize, $promisify, $waitEvent, $wait} = require './fibers-utils'
{$fileExists, $wrap} = require './utils'
# HTTP/HTTPS server which can listen on multiple ports.
$WebServer = require 'http-server-plus'
@@ -89,48 +92,36 @@ $handleJsonRpcCall = (api, session, encodedRequest) ->
#=====================================================================
# Main.
module.exports = $promisify (args) ->
exports = module.exports = $promisify (args) ->
return exports.help() unless (
(args.indexOf '--help') is -1 and
(args.indexOf '-h') is -1
)
# Relative paths in the configuration are relative to this
# directory's parent.
process.chdir "#{__dirname}/.."
# Loads the environment.
$nconf.env()
# Parses process' arguments.
$nconf.argv()
# Loads the configuration files.
format =
stringify: $YAML.safeDump
parse: $YAML.safeLoad
$nconf.use 'file', {
file: "#{__dirname}/../config/local.yaml"
format
}
# Defines defaults configuration.
$nconf.defaults {
# Default config.
opts = {
http: {
listen: [
port: 80
]
mounts: []
mounts: {}
}
redis: {
# Default values are handled by `redis`.
}
}
# Loads config files.
opts = $wait $appConf.load 'xo-server', opts
# Prints a message if deprecated entries are specified.
for entry in ['users', 'servers']
if $nconf.get entry
if entry of opts
console.warn "[Warn] `#{entry}` configuration is deprecated."
# Creates the web server according to the configuration.
webServer = new $WebServer()
$wait $Promise.map ($nconf.get 'http:listen'), (options) ->
$wait $Promise.map opts.http.listen, (options) ->
# Reads certificate and key if necessary.
if options.certificate? and options.key?
options.certificate = $wait $readFile options.certificate
@@ -151,10 +142,8 @@ module.exports = $promisify (args) ->
# Now the web server is listening, drop privileges.
try
if (group = $nconf.get 'group')?
process.setgid group
if (user = $nconf.get 'user')?
process.setuid user
process.setgid opts.group if opts.group?
process.setuid opts.user if opts.user?
catch error
console.warn "[WARN] Failed to change the user or group: #{error.message}"
@@ -170,13 +159,13 @@ module.exports = $promisify (args) ->
# Starts it.
xo.start {
redis: {
uri: $nconf.get 'redis:uri'
uri: opts.redis?.uri
}
}
# Static file serving (e.g. for XO-Web).
connect = $connect()
for urlPath, filePaths of $nconf.get 'http:mounts'
for urlPath, filePaths of opts.http.mounts
filePaths = [filePaths] unless $_.isArray filePaths
for filePath in filePaths
connect.use urlPath, $serveStatic filePath
@@ -228,3 +217,17 @@ module.exports = $promisify (args) ->
console.log "[INFO] Default user: “#{email}” with password “#{password}"
return $eventToPromise webServer, 'close'
exports.help = do (pkg = require '../package') ->
name = $chalk.bold pkg.name
return $wrap '''
Usage: $name
$name v$version
'''.replace /<([^>]+)>|\$(\w+)/g, (_, arg, key) ->
return '<'+ ($chalk.yellow arg) +'>' if arg
return name if key is 'name'
return pkg[key]

View File

@@ -1,4 +1,4 @@
$done = {}
$done = exports.$done = {}
# Similar to `$_.each()` but can be interrupted by returning the
# special value `done` provided as the forth argument.
@@ -71,3 +71,18 @@ exports.$mapInPlace = (col, iterator, ctx) ->
# The collection is returned.
col
# Wraps a value in a function.
exports.$wrap = (val) -> -> val
#=====================================================================
$fs = require 'fs'
$Promise = require 'bluebird'
exports.$fileExists = (path) ->
return new Promise (resolve) ->
$fs.exists path, resolve
exports.$readFile = $Promise.promisify $fs.readFile