A sample Dart HTTP server that acts as middleware between the client and the services.
It is used to simplify the client code and to provide a single point of access to the services.
- Auth service: Firebase Auth REST API
- Database service: Cloud Firestore NoSQL database
- Served on: Google Cloud Run
This server is meant to be deployed on Google Cloud Run. It is a simple HTTP server that acts as a proxy between the client and the services.
It allows for the following features:
-
Authentication: The client can authenticate with the server. The server uses the Firebase Auth REST API to authenticate the user with the Firebase Auth service. The server then returns a JWT token that can be used to authenticate the user with the services.
Authentication methods:- Email/Password login or signup
- Anonymous login
-
Database: The client can read and write to the database. The server uses the Cloud Firestore REST API to read and write to the database.
See source code for more details. Each endpoint is documented with a description and a sample request.
- GET /
- POST /login
- POST /loginAnonymously
- POST /signup
- GET /verifyIdToken
- GET /getProfile
- POST /updateProfile
- GET /issues
- A Google Cloud Platform account
- A Google Cloud Project
- Billing enabled on your Google Cloud Platform project
- A service account for the GCP project
- Cloud Run Invoker role
- A Firebase project
- An API key for the Firebase project
- A Cloud Firestore database
- Firebase Auth service enabled in the Firebase project with the following sign-in methods enabled:
- Email/Password
- Anonymous
- Google Cloud SDK
- Dart SDK
See: Deploying on Google Cloud Run from source
In a nutshell:
- Deploy the service using gcloud sdk:
gcloud run deploy- For region, use
us-central1 - To publish revisions, use
gcloud run deploy --source .
This builds the Docker image and deploys it to Cloud Run. See Dockerfile for more details.
- For region, use
- Set the environment variables
- I used Secret Manager to store the environment variables. Then set the environment variables in Cloud Run
First set the following environment variables:
GOOGLE_APPLICATION_CREDENTIALS- path to the service account key fileGCP_PROJECT- the project IDFIREBASE_API_KEY- the API key for the Firebase project (see here
Note: For convenience, you can use this script to set the environment variables from a file. See sample env file here.
$ source set_env.sh <path-to-env-file>
dart pub getdart run bin/server.dart
or as an executable:
- get the dependencies:
dart pub get - build:
dart compile exe bin/server.dart -o bin/server - run:
bin/server
Create a .env file with the following environment variables:
GOOGLE_APPLICATION_CREDENTIALS- path to the service account key file- The file should be in the root directory of the project and should be named
.google_application_default_credentials.json
- The file should be in the root directory of the project and should be named
GCP_PROJECT- the project IDFIREBASE_API_KEY- the API key for the Firebase project (see here
You will need a service account key file to build the image. Make sure that it is named .google_application_default_credentials.json and placed in the root directory. The
debugflag ensure that the service account key file to the image.
docker image build -t sample_server --build-arg build_env=debug .
Run the container with env variables from a env file called .env in the root directory
docker run --env-file .env -p 8080:8080 sample_server
- The Dart project authors: Dart HTTP server tutorial