Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 171 lines (121 sloc) 4.036 kb
af5b345 @FGRibreau First commit
authored
1 express = require 'express'
2 async = require 'async'
3 fs = require 'fs'
4 forever = require 'forever'
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
5 _ = require 'underscore'
b02dc49 @FGRibreau [Server] Dépendance ejs
authored
6 ejs = require('ejs')
af5b345 @FGRibreau First commit
authored
7
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
8 process.on "uncaughtException", (err) ->
9 console.log "Caught exception: " + err
10
af5b345 @FGRibreau First commit
authored
11 #todo
12 class foreverUI
13
14 constructor: ->
15
d19ab26 @FGRibreau Refactoring server-side
authored
16
17 findProcessByUID: (uid, cb) ->
18 forever.list("", (err, processes) ->
af5b345 @FGRibreau First commit
authored
19
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
20 return cb(err, null) if(err)
d19ab26 @FGRibreau Refactoring server-side
authored
21
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
22 cb(null, _.find(processes, (o) -> o.uid == uid))
d19ab26 @FGRibreau Refactoring server-side
authored
23 )
24
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
25 findProcIndexByUID: (uid, cb) ->
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
26 forever.list("", (err, processes) ->
af5b345 @FGRibreau First commit
authored
27
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
28 return cb(err, null) if err
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
29
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
30 i = -1
31
32 while processes[++i]
33
34 if(processes[i].uid == uid)
35 return cb(null, i)
36
37 cb("Process '#{uid}' not found", null)
d19ab26 @FGRibreau Refactoring server-side
authored
38 )
39
af5b345 @FGRibreau First commit
authored
40
d19ab26 @FGRibreau Refactoring server-side
authored
41 # Get process logs
42 info: (uid, cb) ->
43
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
44 @findProcessByUID(uid, (err, proc) ->
45
46 return cb(err, null) if err
47
48 return cb("Undefined proc", null) if !proc
af5b345 @FGRibreau First commit
authored
49
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
50 async.map([proc.logFile, proc.outFile, proc.errFile].filter((s) -> s != undefined), (filename, cb) ->
af5b345 @FGRibreau First commit
authored
51
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
52 fs.readFile(filename, (err, data) ->
53 d = data.toString().trim()
af5b345 @FGRibreau First commit
authored
54
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
55 if(!d || d == '\n')
56 cb(null, [filename, 'Empty log'])
57 else
58 cb(null, [filename, data.toString()])
59 )
af5b345 @FGRibreau First commit
authored
60
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
61 , (err, results) ->
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
62 cb err, results
af5b345 @FGRibreau First commit
authored
63 )
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
64 )
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
65
66 # stop a process by it's uid
d19ab26 @FGRibreau Refactoring server-side
authored
67 stop: (uid, cb) ->
af5b345 @FGRibreau First commit
authored
68
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
69 @findProcIndexByUID(uid, (err, index) ->
70
71 return cb(err, null) if err
72
73 forever.stop(index)
74 .on('stop', (res) -> cb(null, true))
75 .on('error', (err) -> cb(err, null))
d19ab26 @FGRibreau Refactoring server-side
authored
76
77 )
af5b345 @FGRibreau First commit
authored
78
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
79 # restart a process by it's uid
d19ab26 @FGRibreau Refactoring server-side
authored
80 restart: (uid, cb) ->
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
81
82 @findProcIndexByUID(uid, (err, index) ->
83
84 return cb(err, null) if err
85
86 forever.restart(index)
87 .on('restart', (res) ->
88
89 cb(null, true)
90 )
91 .on('error', (err) -> cb(err, null))
92 )
af5b345 @FGRibreau First commit
authored
93
94 UI = new foreverUI()
95
96 app = express.createServer()
97
98 app.configure ->
99 app.use express.bodyParser()
100 app.use express.cookieParser()
b02dc49 @FGRibreau [Server] Dépendance ejs
authored
101 app.register('.ejs', ejs)
af5b345 @FGRibreau First commit
authored
102 app.set('views', __dirname + '/views')
103 app.set('view engine', 'html')
104 app.use express.methodOverride()
105 app.use app.router
106
107 app.configure "development", ->
108 app.use express.errorHandler(
109 dumpExceptions: true
110 showStack: true
111 )
112 app.use express.static(__dirname + "/public")
113
114 app.configure "production", ->
115 app.use express.errorHandler()
116 app.use(express.static(__dirname + '/public', { maxAge: oneYear }))
117
118
119 app.set 'view options',
120 layout: false
121
122
123 app.get('/', (req, res) ->
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
124 forever.list("", (err, results) ->
125 res.render('index.ejs', {process: results})
126 )
127
af5b345 @FGRibreau First commit
authored
128 )
129
130 app.get('/refresh/', (req, res) ->
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
131 forever.list("", (err, results) ->
132 res.send JSON.stringify(results), { 'Content-Type': 'text/javascript' }, 200
133 )
134
af5b345 @FGRibreau First commit
authored
135 )
136
137 app.get('/processes', (req, res) ->
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
138 forever.list("", (err, results) ->
139 res.send JSON.stringify(results), { 'Content-Type': 'text/javascript' }, 200
af5b345 @FGRibreau First commit
authored
140 )
141 )
142
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
143 #todo : refactoring needed here
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
144 app.get('/restart/:uid', (req, res) ->
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
145 UI.restart req.params.uid, (err, results) ->
146 if err
147 res.send JSON.stringify(status:'error', details:err), { 'Content-Type': 'text/javascript' }, 500
148 else
149 res.send JSON.stringify(status:'success', details:results), { 'Content-Type': 'text/javascript' }, 200
af5b345 @FGRibreau First commit
authored
150 )
151
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
152 app.get('/stop/:uid', (req, res) ->
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
153 UI.stop req.params.uid, (err, results) ->
154 if err
155 res.send JSON.stringify(status:'error', details:err), { 'Content-Type': 'text/javascript' }, 500
156 else
157 res.send JSON.stringify(status:'success', details:results), { 'Content-Type': 'text/javascript' }, 200
af5b345 @FGRibreau First commit
authored
158 )
159
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
160 app.get('/info/:uid', (req, res) ->
d887bd5 @FGRibreau Uniformisation du serveur (besoin de refactoring)
authored
161 UI.info req.params.uid, (err, results) ->
162 if err
163 res.send JSON.stringify(status:'error', details:err), { 'Content-Type': 'text/javascript' }, 500
164 else
165 res.send JSON.stringify(status:'success', details:results), { 'Content-Type': 'text/javascript' }, 200
af5b345 @FGRibreau First commit
authored
166 )
167
b1252ad @FGRibreau Utilisation de l'API de forever (support de la dernière version)
authored
168 #todo
169 app.listen 8085, "127.0.0.1"
170 console.log "Listening on 127.0.0.1:8085"
Something went wrong with that request. Please try again.