#!/usr/bin/env coffee
#
# Crafting Guide - server
#
# Copyright (c) 2014-2015 by Redwood Labs
# All rights reserved.
#

express = require 'express'
path    = require 'path'

########################################################################################################################

ROOT = './dist/'

########################################################################################################################

app = express()

serveIndex = (request, response)->
    console.log ">>> index.html (for #{request.path})"
    response.set 'Content-Type', 'text/html'
    response.sendFile 'index.html', root:ROOT

app.get '/crafting(/*)?', serveIndex
app.get '/item(/*)?', serveIndex
app.get '/mod(/*)?', serveIndex

app.get '*', (request, response)->
    extension = path.extname(request.path)
    contentType = {
        '.css':  'text/css'
        '.html': 'text/html'
        '.js':   'text/javascript'
        '.json': 'application/json'
        '.map':  'application/json'
        '.png':  'image/png'
        '.ttf':  'application/octet-stream'
        '.scss': 'text/css'
    }[extension]
    console.log ">>> #{request.path}"

    response.set 'Content-Type', contentType

    sendFile = -> response.sendFile request.path, root:ROOT

    setTimeout sendFile, 100 # change the zero to post files after a short delay

server = app.listen 8000, ->
    console.log "listening at: #{server.address().address}:#{server.address().port}"
