forked from krasilnikov-dmitriy/presentation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnobone.coffee
executable file
·104 lines (74 loc) · 2.01 KB
/
nobone.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
###
This is the main entrance of the project.
###
_init_timestamp = Date.now()
global.NB = {}
require './sys/module'
class NB.Nobone extends NB.Module
constructor: ->
super
NB.nobone = @
# Must be init first.
@init_config()
@init_modules()
@init_global_router()
init_config: ->
# Set the body parser.
NB.app.use(NB.express.json())
NB.app.use(NB.express.urlencoded())
init_global_router: ->
NB.app.use(NB.express.static('bower_components'))
@set_static_dir('assets')
NB.app.use('/usr', NB.express.static('usr'))
NB.app.use(NB.express.favicon('assets/img/NB.png'))
NB.app.use(@show_404)
init_modules: ->
for name, path of NB.conf.modules
m = name.match /^(.+)\.(.+)$/
namespace = m[1]
class_name = m[2]
ns = global[namespace] ?= {}
require path
ns[class_name.toLowerCase()] = new ns[class_name]
console.log ">> Load module: #{name}:#{path}".c('green')
init_database: ->
require './sys/database'
@db = new NB.Database
init_storage: ->
require './sys/storage'
NB.storage = new NB.Storage
init_api: ->
require './sys/api'
NB.api = new NB.Api
init_plugins: ->
require './sys/plugin'
NB.Plugins = {}
NB.plugins = {}
_.walk_files 'plugins', (path) ->
require '../' + path
for name, Plugin of NB.Plugins
_.valid_class(Plugin)
plugin = new Plugin
NB.plugins[name.toLowerCase()] = plugin
show_404: (req, res, next) =>
if _.find_route(NB.app.routes, req.path)
next()
return
data = {
head: @r.render('assets/ejs/head.ejs')
url: req.originalUrl
}
res.status(404)
res.send @r.render('assets/ejs/404.ejs', data)
console.error ('>> 404: ' + req.originalUrl).c('red')
launch: ->
NB.server.listen NB.conf.port
console.log ("""
*** #{NB.package.name.toUpperCase()} #{NB.package.version} ***
>> Node version: #{process.version}
>> Start at port: #{NB.conf.port}
""").c('cyan')
# Launch the application.
new NB.Nobone
NB.nobone.launch()
console.log ">> Took #{Date.now() - _init_timestamp}ms to startup.".c('cyan')