This repository was archived by the owner on Oct 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathweb.coffee
More file actions
124 lines (102 loc) · 4.24 KB
/
web.coffee
File metadata and controls
124 lines (102 loc) · 4.24 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
builder = require("./lib/builder")
coffee = require("coffee-script")
crypto = require("crypto")
express = require("express")
fs = require("fs")
log = require("./lib/logger")
manifest = require("./lib/manifest")
storage = require("./lib/storage").init()
util = require("util")
require("http").globalAgent.maxSockets = 50
express.logger.format "method", (req, res) ->
req.method.toLowerCase()
express.logger.format "url", (req, res) ->
req.url.replace('"', '"')
express.logger.format "user-agent", (req, res) ->
(req.headers["user-agent"] || "").replace('"', '')
app = express.createServer(
express.logger
buffer: false
format: "subject=\"http\" method=\":method\" url=\":url\" status=\":status\" elapsed=\":response-time\" from=\":remote-addr\" agent=\":user-agent\""
express.bodyParser())
app.get "/", (req, res) ->
res.send "ok"
app.get "/heartbeat", (req, res) ->
res.send "ok"
app.post "/build", (req, res) ->
log "api.build"
type: "url"
source: req.body.source
buildpack: req.body.buildpack
app: req.headers["x-heroku-app"]
user: req.headers["x-heroku-user"]
(logger) ->
builder.init().build_request req, res, logger
app.get "/cache/:id.tgz", (req, res) ->
storage.get "/cache/#{req.params.id}.tgz", (err, get) ->
get.on "data", (chunk) -> res.write chunk
get.on "end", -> res.end()
app.put "/cache/:id.tgz", (req, res) ->
storage.create_stream "/cache/#{req.params.id}.tgz", fs.createReadStream(req.files.data.path), (err) ->
res.send("ok")
app.get "/exit/:id", (req, res) ->
storage.get "/exit/#{req.params.id}", (err, get) ->
get.on "data", (chunk) -> res.write chunk
get.on "end", -> res.end()
app.get "/file/:hash", (req, res) ->
log "api.file.get", hash:req.params.hash, (logger) ->
storage.get "/hash/#{req.params.hash}", (err, get) ->
res.writeHead get.statusCode,
"Content-Length": get.headers["content-length"]
get.on "data", (chunk) -> res.write chunk
get.on "end", -> logger.finish(); res.end()
app.post "/file/:hash", (req, res) ->
log "api.file.post", hash:req.params.hash, (logger) ->
storage.verify_hash req.files.data.path, req.params.hash, (err) ->
return res.send(err, 403) if err
storage.create_stream "/hash/#{req.params.hash}", fs.createReadStream(req.files.data.path), (err) ->
res.send "ok"
app.post "/manifest", (req, res) ->
manifest.init(JSON.parse(req.body.manifest)).save (err, manifest_url) ->
res.header "Location", manifest_url
res.send "ok"
app.post "/manifest/build", (req, res) ->
log "api.build"
type: "manifest"
buildpack: req.body.buildpack
app: req.headers["x-heroku-app"]
user: req.headers["x-heroku-user"]
(logger) ->
manifest.init(JSON.parse(req.body.manifest)).save (err, manifest_url) ->
delete req.body.manifest
req.body.source = manifest_url
builder.init().build_request req, res, logger
app.post "/manifest/diff", (req, res) ->
manifest.init(JSON.parse(req.body.manifest)).missing_hashes (hashes) ->
res.contentType "application/json"
res.send JSON.stringify(hashes)
app.get "/manifest/:id.json", (req, res) ->
storage.get "/manifest/#{req.params.id}.json", (err, get) =>
get.on "data", (chunk) -> res.write chunk
get.on "end", -> res.end()
app.get "/slugs/:id.deb", (req, res) ->
storage.get "/slug/#{req.params.id}.deb", (err, get) ->
res.writeHead get.statusCode,
"Content-Length": get.headers["content-length"]
get.on "data", (chunk) -> res.write chunk
get.on "end", -> res.end()
app.get "/slugs/:id.img", (req, res) ->
storage.get "/slug/#{req.params.id}.img", (err, get) ->
res.writeHead get.statusCode,
"Content-Length": get.headers["content-length"]
get.on "data", (chunk) -> res.write chunk
get.on "end", -> res.end()
app.get "/slugs/:id.tgz", (req, res) ->
storage.get "/slug/#{req.params.id}.tgz", (err, get) ->
res.writeHead get.statusCode,
"Content-Length": get.headers["content-length"]
get.on "data", (chunk) -> res.write chunk
get.on "end", -> res.end()
port = process.env.PORT || 5000
app.listen port, ->
console.log "listening on port #{port}"