Refactor website with new design & structure
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#
|
||||
# Crafting Guide - crafting_guide_server.coffee
|
||||
#
|
||||
# Copyright © 2014-2016 by Redwood Labs
|
||||
# All rights reserved.
|
||||
#
|
||||
|
||||
express = require 'express'
|
||||
http = require 'http'
|
||||
middleware = require './middleware'
|
||||
routes = require './routes'
|
||||
|
||||
############################################################################################################
|
||||
|
||||
module.exports = class CraftingGuideServer
|
||||
|
||||
constructor: (port)->
|
||||
if not port? then throw new Error 'port is mandatory'
|
||||
if not _.isNumber port then throw new Error 'port must be a number'
|
||||
@port = parseInt port
|
||||
|
||||
app = express()
|
||||
app.disable 'etag'
|
||||
|
||||
middleware.installBefore app
|
||||
app.use routes
|
||||
middleware.installAfter app
|
||||
|
||||
@httpServer = http.createServer app
|
||||
|
||||
start: ->
|
||||
w.promise (resolve, reject)=>
|
||||
@httpServer.once 'error', (error)-> reject error
|
||||
@httpServer.listen @port, =>
|
||||
console.log "http://... ready on port #{@port}"
|
||||
console.log "CraftingGuide is ready.\n\n"
|
||||
resolve this
|
||||
|
||||
stop: ->
|
||||
w.promise (resolve, reject)=>
|
||||
console.log "CraftingGuide is shutting down"
|
||||
@httpServer.close() =>
|
||||
resolve this
|
||||
@@ -0,0 +1,73 @@
|
||||
#
|
||||
# Crafting Guide - middleware.coffee
|
||||
#
|
||||
# Copyright © 2014-2016 by Redwood Labs
|
||||
# All rights reserved.
|
||||
#
|
||||
|
||||
bodyParser = require 'body-parser'
|
||||
clientSessions = require 'client-sessions'
|
||||
express = require 'express'
|
||||
w = require 'when'
|
||||
|
||||
############################################################################################################
|
||||
|
||||
exports.installBefore = (app)->
|
||||
app.use m for m in [
|
||||
bodyParser.json
|
||||
limit: '1024kb'
|
||||
clientSessions
|
||||
cookieName: 'session-cookie'
|
||||
duration: 1000 * 60 * 60 * 24 * 7 * 2 # 2 weeks in ms
|
||||
secret: '8QmZ9KbBtN,^%9QT'
|
||||
logRequest
|
||||
prevent304
|
||||
promisify
|
||||
]
|
||||
|
||||
exports.installAfter = (app)->
|
||||
app.use '/data', express.static '../../crafting-guide-data/data/', etag:false, maxAge: 0
|
||||
app.use express.static './static', etag: false, maxAge: 0
|
||||
|
||||
# Middleware Functions #####################################################################################
|
||||
|
||||
logRequest = (request, response, next)->
|
||||
console.log ">>>>> #{request.method} #{request.url}"
|
||||
|
||||
receivedAt = Date.now()
|
||||
totalBytes = 0
|
||||
|
||||
originalWrite = response.write
|
||||
response.write = (chunk)->
|
||||
totalBytes += chunk.length if chunk?.length?
|
||||
originalWrite.apply response, arguments
|
||||
|
||||
originalEnd = response.end
|
||||
response.end = (chunk)->
|
||||
totalBytes += chunk.length if chunk?.length?
|
||||
|
||||
duration = Date.now() - receivedAt
|
||||
sizeText = if totalBytes > 1024 then "#{(totalBytes / 1024.0).toFixed(2)} kB" else "#{totalBytes} B"
|
||||
console.log "<<<<< #{request.method} #{request.url} responded " +
|
||||
"#{response.statusCode} after #{duration}ms with #{sizeText}\n"
|
||||
originalEnd.apply response, arguments
|
||||
|
||||
next()
|
||||
|
||||
prevent304 = (request, response, next)->
|
||||
response.setHeader 'Last-Modified', new Date().toUTCString()
|
||||
next()
|
||||
|
||||
promisify = (request, response, next)->
|
||||
response.sendPromise = (func)->
|
||||
w.try func
|
||||
.then (result)->
|
||||
responseText = JSON.stringify result
|
||||
console.log "#{responseText}"
|
||||
response.send responseText
|
||||
.catch (error)->
|
||||
console.error "Request failed with error:\n#{error.stack}"
|
||||
response.status 500
|
||||
response.send message:error.message, stack:error.stack
|
||||
.done()
|
||||
next()
|
||||
@@ -0,0 +1,20 @@
|
||||
#
|
||||
# Crafting Guide - routes.coffee
|
||||
#
|
||||
# Copyright © 2014-2016 by Redwood Labs
|
||||
# All rights reserved.
|
||||
#
|
||||
|
||||
express = require 'express'
|
||||
|
||||
############################################################################################################
|
||||
|
||||
module.exports = router = express.Router()
|
||||
|
||||
sendIndex = (request, response)->
|
||||
response.sendFile 'index.html', root: 'static'
|
||||
|
||||
router.get '/browse*', sendIndex
|
||||
router.get '/craft*', sendIndex
|
||||
router.get '/login*', sendIndex
|
||||
router.get '/news*', sendIndex
|
||||
@@ -0,0 +1,26 @@
|
||||
#
|
||||
# Crafting Guide - server.coffee
|
||||
#
|
||||
# Copyright © 2014-2016 by Redwood Labs
|
||||
# All rights reserved.
|
||||
#
|
||||
|
||||
CraftingGuideServer = require './crafting_guide_server'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
global._ = require './underscore'
|
||||
global.c = require './constants'
|
||||
global.w = require 'when'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
port = parseInt process.argv[2]
|
||||
port = if _.isNaN port then c.server.defaultPort else port
|
||||
|
||||
server = new CraftingGuideServer port
|
||||
server.start()
|
||||
|
||||
process.on 'SIGINT', ->
|
||||
server.stop().finally ->
|
||||
process.exit()
|
||||
Reference in New Issue
Block a user