NOTE: This is a community-supported project, meaning there is no official level of support. The code is not covered by any SLA or deprecation policy.
Feel free to start a discussion to share thoughts or open issues for bugs and feature requests.
Utilities for running Dart code correctly on the Google Cloud Platform.
Features
This package is split into two main libraries:
General GCP Features (package:google_cloud/general.dart)
- Project Discovery: Automatically discover the Google Cloud Project ID using multiple strategies:
- Environment variables (e.g.,
GOOGLE_CLOUD_PROJECT). - Service account credentials file (
GOOGLE_APPLICATION_CREDENTIALS). gcloudCLI configuration.- Google Cloud Metadata Server.
- Environment variables (e.g.,
- Metadata API: Flexible access to the Metadata Server with built-in
caching (
getMetadataValue) or direct fetching (fetchMetadataValue). - Identity Discovery: Retrieve the default service account email.
- Core Structured Logging: Low-level utilities for creating structured logs that integrate with Google Cloud Logging.
HTTP Serving (package:google_cloud/http_serving.dart)
- Port Discovery: Access the configured listening port via
listenPortFromEnvironment(). - Structured Logging: Shelf middleware for structured logging that integrates with Google Cloud Logging.
- Process Lifecycle: Utilities for handling termination signals (
SIGINT,SIGTERM) to allow for graceful shutdown.
NOTE:
package:google_cloud/google_cloud.dartexports both libraries for convenience.
Usage
Project Discovery
import 'package:google_cloud/general.dart';
void main() async {
// Discovers the project ID using all available strategies.
// Discovery via the Metadata Server is cached for the lifetime of the process.
final projectId = await computeProjectId();
print('Running in project: $projectId');
}
Structured Logging and Serving
import 'package:google_cloud/general.dart';
import 'package:google_cloud/http_serving.dart';
import 'package:shelf/shelf.dart';
void main() async {
final projectId = await computeProjectId();
final handler = const Pipeline()
.addMiddleware(createLoggingMiddleware(projectId: projectId))
.addHandler((request) {
currentLogger.info('Handling request: ${request.url}');
return Response.ok('Hello, World!');
});
// Automatically listens on the correct port and handles termination signals.
await serveHandler(handler);
}
Libraries
- constants
- Constants for Google Cloud Platform integration.
- general
- General Google Cloud Platform integration features.
- google_cloud
- Utilities for running Dart code correctly on the Google Cloud Platform.
- http_serving
- Features for serving HTTP requests on Google Cloud Platform.