Skip to content
This repository
tag: v0.8.1
Fetching contributors…

Cannot retrieve contributors at this time

file 77 lines (64 sloc) 2.572 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
coffee = require("coffee-script")
express = require("express")
fs = require("fs")
logger = require("logger")
rest = require("restler")
spawner = require("spawner").init()
url = require("url")
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_attachment = (source, cb) ->
      source.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"
          cb())

    build = ->
      res.write "done\n"
      res.write "Building with: #{command}\n"
      log.info "spawning build"
      make = spawner.spawn "bin/vulcan-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()

    if (req.files.code)
      log.info "saving attachment - [id:#{doc.id} rev:#{doc.rev}]"
      save_attachment fs.createReadStream(req.files.code.path), ->
        build()

    else if (req.body.code_url)
      log.info "downloading attachment: #{req.body.code_url}"
      parts = url.parse(req.body.code_url)
      client = if parts.protocol is "https:" then require("https") else require("http")
      get = client.request parts, (res) ->
        save_attachment res, ->
          build()
      get.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.