Chromium Code Reviews| 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 library appengine.internal; | 5 library appengine.internal; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:gcloud/service_scope.dart' as ss; | 10 import 'package:gcloud/service_scope.dart' as ss; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 return new Future.value(); | 90 return new Future.value(); |
| 91 } | 91 } |
| 92 } else { | 92 } else { |
| 93 return auth.clientViaMetadataServer().then((client) { | 93 return auth.clientViaMetadataServer().then((client) { |
| 94 _authClient = client; | 94 _authClient = client; |
| 95 return new storage.Storage(client, context.applicationID); | 95 return new storage.Storage(client, context.applicationID); |
| 96 }); | 96 }); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 var context = getDockerContext(); | 100 if (_contextRegistry != null) { |
| 101 var rpcService = initializeRPC(); | 101 return new Future.value(_contextRegistry); |
| 102 } else { | |
| 103 var context = getDockerContext(); | |
| 104 var rpcService = initializeRPC(); | |
| 102 | 105 |
| 103 return getStorage(context).then((storage) { | 106 return getStorage(context).then((storage) { |
| 104 return new ContextRegistry(rpcService, storage, context); | 107 _contextRegistry = new ContextRegistry(rpcService, storage, context); |
| 105 }); | 108 return _contextRegistry; |
| 109 }); | |
| 110 } | |
| 106 } | 111 } |
| 107 | 112 |
| 108 void initializeContext(Services services) { | 113 void initializeContext(Services services) { |
| 109 db.registerDbService(services.db); | 114 db.registerDbService(services.db); |
| 110 datastore.registerDatastoreService(services.db.datastore); | 115 datastore.registerDatastoreService(services.db.datastore); |
| 111 storage.registerStorageService(services.storage); | 116 storage.registerStorageService(services.storage); |
| 112 logging.registerLoggingService(services.logging); | 117 logging.registerLoggingService(services.logging); |
| 113 modules.registerModulesService(services.modules); | 118 modules.registerModulesService(services.modules); |
| 114 memcache.registerMemcacheService(services.memcache); | 119 memcache.registerMemcacheService(services.memcache); |
| 115 | 120 |
| 116 if (_authClient != null) { | 121 if (_authClient != null) { |
| 117 gcloud_http.registerAuthClientService(_authClient); | 122 gcloud_http.registerAuthClientService(_authClient); |
| 118 | 123 |
| 119 // This will automatically close the authenticated HTTP client when the | 124 // This will automatically close the authenticated HTTP client when the |
| 120 // HTTP server shuts down. | 125 // HTTP server shuts down. |
| 121 ss.registerScopeExitCallback(() => _authClient.close()); | 126 ss.registerScopeExitCallback(() => _authClient.close()); |
| 122 } | 127 } |
| 123 } | 128 } |
| 124 | 129 |
| 125 void initializeRequestSpecificServices(Services services) { | 130 void initializeRequestSpecificServices(Services services) { |
| 126 logging.registerLoggingService(services.logging); | 131 logging.registerLoggingService(services.logging); |
| 127 users.registerUserService(services.users); | 132 users.registerUserService(services.users); |
| 128 } | 133 } |
| 129 | 134 |
| 135 Future withAppEngineServices(Future callback()) { | |
| 136 return initializeAppEngine().then((ContextRegistry contextRegistry) { | |
| 137 return ss.fork(() { | |
| 138 var backgroundServices = _contextRegistry.newBackgroundServices(); | |
| 139 initializeContext(backgroundServices); | |
| 140 return callback(); | |
| 141 }); | |
| 142 }); | |
| 143 } | |
| 144 | |
| 130 Future runAppEngine(void handler(request, context), void onError(e, s)) { | 145 Future runAppEngine(void handler(request, context), void onError(e, s)) { |
| 131 return initializeAppEngine().then((ContextRegistry contextRegistry) { | 146 return initializeAppEngine().then((ContextRegistry contextRegistry) { |
|
wibling
2014/12/03 11:11:03
Why call initializeAppEngine here?
Isn't it enou
kustermann
2014/12/03 11:14:42
Sure. Forgot to remove that wrapper.
Done
| |
| 132 _contextRegistry = contextRegistry; | 147 return withAppEngineServices(() { |
| 133 var appengineServer = new AppEngineHttpServer(_contextRegistry); | 148 var appengineServer = new AppEngineHttpServer(_contextRegistry); |
| 134 var backgroundServices = _contextRegistry.newBackgroundServices(); | |
| 135 | |
| 136 ss.fork(() { | |
| 137 initializeContext(backgroundServices); | |
| 138 appengineServer.run((request, context) { | 149 appengineServer.run((request, context) { |
| 139 ss.fork(() { | 150 ss.fork(() { |
| 140 initializeRequestSpecificServices(context.services); | 151 initializeRequestSpecificServices(context.services); |
| 141 handler(request, context); | 152 handler(request, context); |
| 142 return request.response.done; | 153 return request.response.done; |
| 143 }, onError: (error, stack) { | 154 }, onError: (error, stack) { |
| 144 var context = _contextRegistry.lookup(request); | 155 var context = _contextRegistry.lookup(request); |
| 145 if (context != null) { | 156 if (context != null) { |
| 146 try { | 157 try { |
| 147 context.services.logging.error( | 158 context.services.logging.error( |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 163 } on StateError catch (_) {} | 174 } on StateError catch (_) {} |
| 164 request.response.close().catchError((closeError, closeErrorStack) { | 175 request.response.close().catchError((closeError, closeErrorStack) { |
| 165 onError('Forcefully closing response, due to error in request ' | 176 onError('Forcefully closing response, due to error in request ' |
| 166 'handler zone, resulted in an error: $closeError', | 177 'handler zone, resulted in an error: $closeError', |
| 167 closeErrorStack); | 178 closeErrorStack); |
| 168 }); | 179 }); |
| 169 }); | 180 }); |
| 170 }); | 181 }); |
| 171 return appengineServer.done; | 182 return appengineServer.done; |
| 172 }); | 183 }); |
| 173 | |
| 174 return new Future.value(); | |
| 175 }); | 184 }); |
| 176 } | 185 } |
| OLD | NEW |