Skip to content

Commit

Permalink
Move pub_stats_collector and proxy to this project
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Nov 20, 2023
1 parent 72fedc6 commit b1e686c
Show file tree
Hide file tree
Showing 30 changed files with 2,153 additions and 0 deletions.
9 changes: 9 additions & 0 deletions proxy/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.dart_tool/
.dockerignore
.git/
.github/
.gitignore
.idea/
.packages
Dockerfile
build/
4 changes: 4 additions & 0 deletions proxy/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gcloudignore
.git
.gitignore
#!include:.gitignore
9 changes: 9 additions & 0 deletions proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Files and directories created by pub.
.dart_tool/
.packages

# Conventional directory for build output.
build/

# macOS
.DS_Store
24 changes: 24 additions & 0 deletions proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Official Dart image: https://hub.docker.com/_/dart
# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.14)
FROM dart:stable AS build

# Resolve app dependencies.
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get

# Copy app source code and AOT compile it.
COPY . .
# Ensure packages are still up-to-date if anything has changed
RUN dart pub get --offline
RUN dart compile exe bin/server.dart -o bin/server

# Build minimal serving image from AOT-compiled `/server` and required system
# libraries and configuration files stored in `/runtime/` from the build stage.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/

# Start server.
EXPOSE 8080
ENTRYPOINT ["/app/bin/server"]
12 changes: 12 additions & 0 deletions proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Template for creating Google Cloud Run applications in Dart. Based on documentation from https://github.com/GoogleCloudPlatform/functions-framework-dart.

The functions framework is nice for very simple applications, but isn't great if you want more control over your server behavior.

## Prerequisites
- Have a general idea how to set up Cloud Run. This is not comprehensive documentation.

## Usage
1. Create a new project based on this template
2. Compose your server in `bin/server.dart`
3. Edit `deploy.sh` to use the desired parameters
4. Run `deploy.sh` to deploy your application
1 change: 1 addition & 0 deletions proxy/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:rexios_lints/dart/core.yaml
28 changes: 28 additions & 0 deletions proxy/bin/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_cors_headers/shelf_cors_headers.dart';
import 'package:shelf_proxy/shelf_proxy.dart';
import 'package:shelf_router/shelf_router.dart';

final pubHandler = Pipeline()
.addMiddleware(
corsHeaders(
originChecker:
originOneOf(['https://pubstats.dev', 'https://beta.pubstats.dev']),
),
)
.addHandler(
proxyHandler('https://pub.dartlang.org', proxyName: 'proxy.pubstats.dev'),
);

void main(List<String> arguments) async {
final app = Router()..mount('/pub', pubHandler);

final port = Platform.environment.containsKey('PORT')
? int.parse(Platform.environment['PORT']!)
: 8080;
final server = await shelf_io.serve(app.call, '0.0.0.0', port);

print('Listening on :${server.port}');
}
9 changes: 9 additions & 0 deletions proxy/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# gcloud run deploy NAME \ # the Cloud Run service name
# --source=PATH \ # can use $PWD or . for current dir
# --project=PROJECT \ # the Google Cloud project ID
# --region=REGION \ # ex: us-central1
# --platform=managed \ # for Cloud Run
# --allow-unauthenticated # for public access

# ex: gcloud run deploy my-service --source=. --project=my-project --region=us-central1 --platform=managed --allow-unauthenticated
gcloud run deploy pub-stats-collector-nginx --source=. --project=pub-stats-collector --region=us-central1 --platform=managed --allow-unauthenticated
Loading

0 comments on commit b1e686c

Please sign in to comment.