OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:appengine/appengine.dart'; | 8 import 'package:appengine/appengine.dart'; |
9 import 'package:gcloud/db.dart'; | 9 import 'package:gcloud/db.dart'; |
10 import 'package:gcloud/service_scope.dart'; | 10 import 'package:gcloud/service_scope.dart'; |
(...skipping 11 matching lines...) Expand all Loading... |
22 import 'package:pub_dartlang_org/templates.dart'; | 22 import 'package:pub_dartlang_org/templates.dart'; |
23 import 'package:pub_dartlang_org/upload_signer_service.dart'; | 23 import 'package:pub_dartlang_org/upload_signer_service.dart'; |
24 | 24 |
25 import 'configuration.dart'; | 25 import 'configuration.dart'; |
26 import 'server_common.dart'; | 26 import 'server_common.dart'; |
27 | 27 |
28 void main() { | 28 void main() { |
29 useLoggingPackageAdaptor(); | 29 useLoggingPackageAdaptor(); |
30 | 30 |
31 withAppEngineServices(() async { | 31 withAppEngineServices(() async { |
32 return fork(() async { | 32 return withCorrectDatastore((DatastoreDB savedDb) async { |
33 DatastoreDB savedDb; | |
34 if (Platform.isMacOS) { | |
35 savedDb = await initializeApiaryDatastore(); | |
36 } | |
37 final shelf.Handler apiHandler = await setupServices(activeConfiguration); | 33 final shelf.Handler apiHandler = await setupServices(activeConfiguration); |
38 | 34 |
39 var requestHandler = (HttpRequest ioRequest) async { | 35 await runAppEngine((HttpRequest ioRequest) async { |
40 if (context.isProductionEnvironment && | 36 return withCorrectDatastore((_) { |
41 ioRequest.requestedUri.scheme != 'https') { | 37 if (context.isProductionEnvironment && |
42 final secureUri = ioRequest.requestedUri.replace(scheme: 'https'); | 38 ioRequest.requestedUri.scheme != 'https') { |
43 ioRequest.response | 39 final secureUri = ioRequest.requestedUri.replace(scheme: 'https'); |
44 ..redirect(secureUri) | 40 ioRequest.response |
45 ..close(); | 41 ..redirect(secureUri) |
46 } else { | 42 ..close(); |
47 try { | 43 } else { |
48 return shelf_io.handleRequest(ioRequest, | 44 try { |
49 (shelf.Request request) async { | 45 return shelf_io.handleRequest(ioRequest, |
50 logger.info('Handling request: ${request.requestedUri}'); | 46 (shelf.Request request) async { |
51 await registerLoggedInUserIfPossible(request); | 47 logger.info('Handling request: ${request.requestedUri}'); |
52 try { | 48 await registerLoggedInUserIfPossible(request); |
53 final sanitizedRequest = sanitizeRequestedUri(request); | 49 try { |
54 return await appHandler(sanitizedRequest, apiHandler); | 50 final sanitizedRequest = sanitizeRequestedUri(request); |
55 } catch (error, s) { | 51 return await appHandler(sanitizedRequest, apiHandler); |
56 logger.severe('Request handler failed', error, s); | 52 } catch (error, s) { |
57 return new shelf.Response.internalServerError(); | 53 logger.severe('Request handler failed', error, s); |
58 } finally { | 54 return new shelf.Response.internalServerError(); |
59 logger.info('Request handler done.'); | 55 } finally { |
60 } | 56 logger.info('Request handler done.'); |
61 }); | 57 } |
62 } catch (error, stack) { | 58 }); |
63 logger.severe('Request handler failed', error, stack); | 59 } catch (error, stack) { |
| 60 logger.severe('Request handler failed', error, stack); |
| 61 } |
64 } | 62 } |
65 } | 63 }, savedDb); |
66 }; | 64 }); |
67 if (Platform.isMacOS) { | 65 }, null); |
68 final origHandler = requestHandler; | |
69 requestHandler = (ioRequest) { | |
70 fork(() async { | |
71 registerDbService(savedDb); | |
72 await origHandler(ioRequest); | |
73 }); | |
74 }; | |
75 } | |
76 await runAppEngine(requestHandler); | |
77 }); | |
78 }); | 66 }); |
79 } | 67 } |
80 | 68 |
81 Future<shelf.Handler> setupServices(Configuration configuration) async { | 69 Future<shelf.Handler> setupServices(Configuration configuration) async { |
82 registerTemplateService( | 70 registerTemplateService( |
83 new TemplateService(templateDirectory: TemplateLocation)); | 71 new TemplateService(templateDirectory: TemplateLocation)); |
84 | 72 |
85 final bucket = storageService.bucket(configuration.packageBucketName); | 73 final bucket = storageService.bucket(configuration.packageBucketName); |
86 final tarballStorage = new TarballStorage(storageService, bucket, null); | 74 final tarballStorage = new TarballStorage(storageService, bucket, null); |
87 registerTarballStorage(tarballStorage); | 75 registerTarballStorage(tarballStorage); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // "GET //api/..." :-/ ) | 115 // "GET //api/..." :-/ ) |
128 final changedUri = uri.replace(path: normalizedResource); | 116 final changedUri = uri.replace(path: normalizedResource); |
129 return new shelf.Request(request.method, changedUri, | 117 return new shelf.Request(request.method, changedUri, |
130 protocolVersion: request.protocolVersion, | 118 protocolVersion: request.protocolVersion, |
131 headers: request.headers, | 119 headers: request.headers, |
132 body: request.read(), | 120 body: request.read(), |
133 encoding: request.encoding, | 121 encoding: request.encoding, |
134 context: request.context); | 122 context: request.context); |
135 } | 123 } |
136 } | 124 } |
OLD | NEW |