forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (90 loc) · 2.92 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
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
var http = require("http"),
url = require("url"),
fs = require("fs"),
os = require("os"),
tachyon = require( './index' ),
proxyFile = require( './proxy-file' ),
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' ) );
}
config.clientArgs = config.clientArgs || {};
if ( process.env.AWS_S3_CLIENT_ARGS ) {
( new url.URLSearchParams( process.env.AWS_S3_CLIENT_ARGS ) ).forEach( ( value, key ) => config.clientArgs[ key ] = value );
}
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();
}
const key = decodeURIComponent( params.pathname.substr(1) ).replace( '/uploads/tachyon/', '/uploads/' );
const args = params.query || {};
if ( typeof args.webp === 'undefined' ) {
args.webp = !!( request.headers && request.headers['accept'] && request.headers['accept'].match( 'image/webp' ) );
}
return tachyon.s3( config, key, args, function( err, data, info ) {
if ( err ) {
function callback( error, rsp ) {
if ( error ) {
if ( debug ) {
console.error( Date(), error );
}
response.writeHead( error.statusCode ? error.statusCode : 500, {
'Cache-Control': 'no-cache',
} );
response.write( error.message );
return response.end();
}
response.writeHead( rsp.statusCode, Object.assign( {
'Content-Type': 'image/gif',
'Cache-Control': 'public, max-age=31557600',
} ) );
response.write( Buffer.from( rsp.body, 'base64' ) );
return response.end();
}
if ( err.message === 'fallback-to-original' ) {
const s3config = { region: config.region };
if ( config.endpoint ) {
s3config.endpoint = config.endpoint;
}
return proxyFile( s3config, config.bucket, key, callback );
}
return callback( err );
}
var resp = {
'Content-Type': 'image/' + info.format,
'Content-Length': info.size,
'Cache-Control': 'public, max-age=31557600',
}
if (info.errors) {
resp["X-Tachyon-Errors"] = info.errors;
}
response.writeHead( 200, resp );
response.write( data );
return response.end();
} );
} ).listen( parseInt( port, 10 ) );
console.log( "Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" );