-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.coffee
50 lines (44 loc) · 1.4 KB
/
web.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
express = require 'express'
tmp = require 'tmp'
fs = require 'fs'
{spawn} = require 'child_process'
app = express.createServer()
app.use express.logger()
app.use express.bodyParser()
app.use require('connect-assets')()
app.use express.compress()
app.get '/', (req, res) ->
res.render 'index.jade'
app.post '/echo', (req, res) ->
res.send output: req.body.input
gmshToThree = require './gmshToThree'
app.post '/mesh/gmsh', (req, res, next) ->
tmp.file (err, inputPath) ->
if err
next err
return
fs.writeFile inputPath, req.body.input, (err) ->
if err
next err
return
tmp.file (err, outputPath) ->
if err
next err
return
gmsh = spawn 'gmsh',
['-3', inputPath, '-o', outputPath, '-format', 'msh']
logs = ""
gmsh.stdout.on 'data', (data) -> logs += data
gmsh.stderr.on 'data', (data) -> logs += data
gmsh.on 'exit', (code) ->
if code != 0
next "Gmsh exited with code #{code}"
return
fs.unlink inputPath, (err) -> console.log err if err
fs.readFile outputPath, "UTF-8", (err, data) ->
console.log err if err
res.send msh: data, three: gmshToThree(data), logs: logs
fs.unlink outputPath, (err) -> console.log err if err
port = process.env.PORT || 5000
app.listen port, ->
console.log "Listening on " + port