Skip to content
This repository
branch: recipe
Fetching contributors…

Cannot retrieve contributors at this time

file 62 lines (49 sloc) 2.088 kb
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
coffee = require("coffee-script")
express = require("express")
fs = require("fs")
logger = require("logger")
spawner = require("spawner").init()
util = require("util")
uuid = require("node-uuid")

db = require("cloudant").connect("make")

app = express.createServer(
  express.logger()
  express.cookieParser()
  express.bodyParser())

app.post "/make", (req, res, next) ->
  id = uuid()
  command = req.body.command
  prefix = req.body.prefix
  deps = if req.body.deps then JSON.parse(req.body.deps) else []
  log = logger.init(res, next, id)

  unless req.body.secret is process.env.SECRET
    return log.error "invalid secret"

  # return build id as a header
  res.header "X-Make-Id", id

  # keep the response alive
  setInterval (-> res.write(String.fromCharCode(0) + String.fromCharCode(10))), 1000

  # save build to couchdb
  log.info "saving to couchdb"
  db.save id, command:command, prefix:prefix, deps:deps, (err, doc) ->
    return log.error(util.inspect(err)) if err

    # save uploaded code as an attachment
    log.info "saving attachment - [id:#{doc.id} rev:#{doc.rev}]"
    fs.createReadStream(req.files.code.path).pipe(
      db.saveAttachment {id:doc.id, rev:doc.rev}, {name:"input", "Content-Type":"application/octet-stream"}, (err, data) ->
        return log.error(err.reason) if err && err.error != "conflict"

        res.write "done\n"
        res.write "Building with: #{command}\n"
        log.info "spawning build"

        make = spawner.spawn "bin/make \"#{id}\"", env:
          CLOUDANT_URL: process.env.CLOUDANT_URL
          PATH: process.env.PATH

        make.on "error", (err) -> log.error(err)
        make.on "data", (data) -> res.write data
        make.on "end", (code) -> res.end())

app.get "/output/:id", (req, res, next) ->
  log = logger.init(res, next, req.params.id)
  stream = db.getAttachment req.params.id, "output"
  stream.on "error", (err) -> log.error(err)
  stream.on "data", (chunk) -> res.write chunk, "binary"
  stream.on "end", -> res.end()

app.listen process.env.PORT or 3000
Something went wrong with that request. Please try again.