Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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