65 lines
2.1 KiB
CoffeeScript
Executable File
65 lines
2.1 KiB
CoffeeScript
Executable File
#!/usr/bin/env coffee
|
|
#
|
|
# Crafting Guide - serve-build
|
|
#
|
|
# Copyright (c) 2014-2015 by Redwood Labs
|
|
# All rights reserved.
|
|
#
|
|
|
|
express = require 'express'
|
|
fs = require 'fs'
|
|
path = require 'path'
|
|
|
|
########################################################################################################################
|
|
|
|
ROOT = './dist'
|
|
DATA_ROOT = '../crafting-guide-data'
|
|
PRERENDER_ROOT = '../crafting-guide-prerender/static'
|
|
|
|
CONTENT_TYPE = {
|
|
'.cg': 'text/plain'
|
|
'.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'
|
|
}
|
|
|
|
########################################################################################################################
|
|
|
|
app = express()
|
|
|
|
serveIndex = (request, response)->
|
|
response.set 'Content-Type', 'text/html'
|
|
filePath = "#{request.path}index.html".replace('index.htmlindex.html', 'index.html')
|
|
fs.stat "#{PRERENDER_ROOT}#{filePath}", (err, stats)->
|
|
if not err?
|
|
console.log ">>> #{request.path} ==> #{PRERENDER_ROOT}#{filePath}"
|
|
response.sendFile filePath, root:PRERENDER_ROOT
|
|
else
|
|
console.log ">>> #{request.path} ==> #{ROOT}app.html"
|
|
response.sendFile '/app.html', root:ROOT
|
|
|
|
serveData = (request, response)->
|
|
response.set 'Content-Type', CONTENT_TYPE[path.extname request.path]
|
|
console.log ">>> #{request.path} ==> #{DATA_ROOT}#{request.path}"
|
|
response.sendFile request.path, root:DATA_ROOT
|
|
|
|
app.get '/data/*', serveData
|
|
|
|
app.get '/craft/*', serveIndex
|
|
app.get '/login', serveIndex
|
|
app.get '*.html', serveIndex
|
|
app.get '*/$', serveIndex
|
|
|
|
app.get '*', (request, response)->
|
|
response.set 'Content-Type', CONTENT_TYPE[path.extname request.path]
|
|
console.log ">>> #{request.path} ==> #{ROOT}#{request.path}"
|
|
response.sendFile request.path, root:ROOT
|
|
|
|
server = app.listen 8000, '127.0.0.1', ->
|
|
console.log "listening at: #{server.address().address}:#{server.address().port}"
|