OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** | |
6 * This file runs a trivial HTTP server to serve locale data files from the | |
7 * intl/lib/src/data directory. The code is primarily copied from the dart:io | |
8 * example at http://www.dartlang.org/articles/io/ | |
9 */ | |
10 | |
11 #import('dart:io'); | |
12 #import('dart:isolate'); | |
13 | |
14 var server = new HttpServer(); | |
15 | |
16 _send404(HttpResponse response) { | |
17 response.statusCode = HttpStatus.NOT_FOUND; | |
18 response.outputStream.close(); | |
19 } | |
20 | |
21 startServer(String basePath) { | |
22 server.listen('127.0.0.1', 8000); | |
23 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | |
24 var path = request.path; | |
25 if (path == '/terminate') { | |
26 server.close(); | |
27 return; | |
28 } | |
29 final File file = new File('${basePath}${path}'); | |
30 file.exists().then((bool found) { | |
31 if (found) { | |
32 // Set the CORS header to allow us to issue requests to localhost when | |
33 // the HTML was opened from a file. | |
34 response.headers.set("Access-Control-Allow-Origin", "*"); | |
35 file.fullPath().then((String fullPath) { | |
36 file.openInputStream().pipe(response.outputStream); | |
37 }); | |
38 } else { | |
39 _send404(response); | |
40 } | |
41 }); | |
42 }; | |
43 } | |
44 | |
45 main() { | |
46 // Compute base path for the request based on the location of the | |
47 // script and then start the server. | |
48 File script = new File(new Options().script); | |
49 script.directory().then((Directory d) { | |
50 startServer('${d.path}/../lib/src/data'); | |
51 }); | |
52 // After 60 seconds, if we haven't already been told to terminate, shut down | |
53 // the server, at which point the program exits. | |
54 new Timer(60000, (t) {server.close();}); | |
55 } | |
OLD | NEW |