process.env.NODE_ENV = 'development'

nobone = require 'nobone'

port = 8219

# If you want to init without a specific module,
# for example 'db' and 'service' module, just exclude them:
# 	nobone {
# 		renderer: {}
# 	}
# By default it only loads two modules: `service` and `renderer`.
nb = nobone {
	db: { dbPath: './test.db' }
	proxy: {}
	renderer: {}
	service: {}
	lang: {
		langPath: 'examples/fixtures/lang'
		current: 'cn'
	}
}

# Service
nb.service.get '/', (req, res) ->
	# Renderer
	# It will auto-find the 'examples/fixtures/index.tpl', and render it to html.
	# You can also render jade, coffee, stylus, less, sass,markdown,
	# or define custom handlers.
	# When you modify the `examples/fixtures/index.tpl`, the page will auto-reload.
	nb.renderer.render('examples/fixtures/index.html')
	.then (tplFn) ->
		res.send tplFn({ name: 'nobone' })

# Launch express.js
nb.service.listen port, ->
	# Kit
	# A smarter log helper.
	nb.kit.log 'Listen port ' + port

	# Open default browser.
	nb.kit.xopen 'http://127.0.0.1:' + port

# Static folder for auto-service of coffeescript and stylus, etc.
nb.service.use nb.renderer.static('examples/fixtures')

# Database
# Nobone has a build-in file database.
# Here we save 'a' as value 1.
nb.db.loaded.then ->
	nb.db.exec (db) ->
		db.doc.a = 1
		db.save('DB OK')
	.then (data) ->
		nb.kit.log data

	# Get data 'a'.
	nb.kit.log nb.db.doc.a

# Proxy
# Proxy path to specific url.
nb.service.get '/proxy.*', (req, res) ->
	# If you visit "http://127.0.0.1:8013/proxy.js",
	# it'll return the "http://127.0.0.1:8013/main.js" from the remote server,
	# though here we just use a local server for test.
	nb.proxy.url req, res, "http://127.0.0.1:#{port}/main." + req.params[0]

# Globalization
nb.kit.log 'human'.l # -> '人类'
nb.kit.log 'open|formal'.l # -> '开启'
nb.kit.log nb.lang('find %s men', [10], 'jp') # -> '10人が見付かる'

close = ->
	# Release all the resources.
	nb.close().then ->
		nb.kit.log 'Peacefully closed.'
