forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (59 loc) · 1.77 KB
/
server.js
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
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
os = require("os"),
tachyon= require( './index' ),
args = process.argv.slice(2),
port = Number( args[0] ) ? args[0] : 8080,
debug = args.indexOf( '--debug' ) > -1
var config = {}
if ( process.env.AWS_REGION && process.env.AWS_S3_BUCKET ) {
config = {
region: process.env.AWS_REGION,
bucket: process.env.AWS_S3_BUCKET,
endpoint: process.env.AWS_S3_ENDPOINT,
}
} else if ( fs.existsSync( 'config.json' ) ) {
config = JSON.parse( fs.readFileSync( 'config.json' ) )
}
http.createServer( function( request, response ) {
var params = url.parse( request.url, true )
if ( debug ) {
console.log( Date(), request.url )
}
// healthcheck file
if ( params.pathname === '/healthcheck.php' ) {
response.writeHead( 200 )
response.write( 'All good.' )
return response.end()
}
// robots.txt
if ( params.pathname === '/robots.txt' ) {
response.writeHead( 200, {
'Content-Type': 'text/plain'
} );
response.write( 'User-agent: *' + os.EOL + 'Allow: /' )
return response.end()
}
return tachyon.s3( config, decodeURIComponent( params.pathname.substr(1) ), params.query, function( err, data, info ) {
if ( err ) {
if ( debug ) {
console.error( Date(), err )
}
response.writeHead( err.statusCode ? err.statusCode : 500, {
'Cache-Control': 'no-cache'
} )
response.write( err.message )
return response.end()
}
response.writeHead( 200, {
'Content-Type': 'image/' + info.format,
'Content-Length': info.size,
'Cache-Control': 'public, max-age=31557600'
})
response.write( data )
return response.end()
} );
}).listen( parseInt( port, 10 ) )
console.log( "Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" )