Deploying your Dart server applications around the 
Deploying your Dart server applications around the 

?

Mike Diarmid

provider "about_me" {
  name     = "Mike Diarmid"
  location = "Cheshire, England"
  default  = "CTO @ Invertase"
  founder  = "Invertase"
  homepage = "https://invertase.io"

  socials = {
    twitter  = "mikediarmid"
    github   = "salakar"
    linkedin = "mikediarmid"
  }

  projects = {
    melos           = "melos.invertase.dev"
    zapp            = "zapp.run"
    flutterfire     = "github.com/firebase/flutterfire"
    flutterfire_cli = "github.com/invertase/flutterfire_cli"
    
    # TODO hide RN shame from bio, no one has to know
    react_native_firebase = "rnfirebase.io"
    notifee               = "notifee.app"
  }
}
$ terraform apply
 about_me.tf [X]

CTO @ Invertase

Majid Hajian

import 'package:flutter/material.dart';
MaterialApp(
   ThemeData(
     name: "Majid Hajian",
     location: "Oslo, Norway",
     description: '''
       Google Developer Expert
       Passionate Software engineer, 
       Community Leader, Author and international Speaker
     ''',
     main: "Head of DevRel at Invertase.io",
     homepage: "https://www.majidhajian.com",
     socials: {
       twitter: "https://www.twitter.com/mhadaily",
       github: "https://www.github.com/mhadaily"
     },
     author: {
       Pluralsight: "www.pluralsight.com/authors/majid-hajian",
       Apress: "Progressive Web App with Angular, Book",
       PacktPub: "PWA development",
       Udemy: "PWA development",
     },
     founder: "Softiware As (www.Softiware.com)"
     devDependencies: {
       tea: "Ginger", 
       mac: "10.14+",
     },
     community: {
       FlutterVikings: "Orginizer", 
       FlutterCommunity: "Admin/Lead",
    },
  ),
);
 about_me.dart [X]

Head of DevRel @ Invertase

invertase.io

Dart on the server

Why?

- Null-safe & type safe language

- Reduced errors at runtime

- Compiler effeciency

- Portability

$ dart compile exe lib/main.dart --output=myapp
~150mb
~10mb

- Growing ecosystem

- Dart Edge, Dart Frog, Serverpod & Shelf

- Same language as your apps, code reusability, and more...

Dart on the server

How do we deploy

Dart on the Edge
Dart in the Cloud
Something else?

Dart on the Edge

What are Edge Functions?

$ dart compile js lib/main.dart -o bundle.js

- Run close to your users = decreased latency

Serverless functions that run on Edge networks

Benefits

- Minimal to no cold boot times vs traditional serverless functions

- Cloudflare Workers, Vercel Edge, Deno Deploy, Supabase Functions

Downsides

- Runs on the JavaScript V8 runtime

   - dart2js compilation required:

   - DIY dart js interop layer required to interface with 100's of Web APIs

     (can't use dart:HTML or dart:io)

- Stricter limits on compute usage & request processing time.

- Platforms provide powerful low-latency runtime APIs like KV & Storage

Dart on the Edge

The easy (experimental) way

In the future

invertase.link/edge

$ edge new vercel_edge myproject
$ edge new supabase_functions myproject
$ edge new cloudflare_workers myproject

invertase.link/dart2wasm

Dart in the Cloud

FROM dart:stable AS build

WORKDIR /app
COPY pubspec.* ./
RUN dart pub get

# Copy app source code and AOT compile it.
COPY . .
# Ensure packages are still up-to-date.
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
CMD ["/app/bin/server"]
 Dockerfile [X]
import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;

void main() async {
  final handler =
      const Pipeline()
      	.addMiddleware(logRequests())
        .addHandler(_echoRequest);

  final server = await shelf_io.serve(
    handler,
    InternetAddress.anyIPv4,
    8080,
  );
}

Response _echoRequest(Request request) => 
	Response.ok('Hello at "${request.url}"!');
 bin/server.dart [X]

Cloud Build

Artifact Registry

??

Cloud Run

Ready?

Kubernetes Engine

Artifact Registry

Cloud Run

Ready?

region1

Cloud Run

region2

Cloud Run

region3

...

Cloud Run

region30?

Load Balancer

resource "google_cloud_run_service" "dart_app" {
  provider = google-beta
  count    = length(var.regions)
  name     = "cool-app-${var.regions[count.index]}"
  location = var.regions[count.index]
  # ...
  template {
    spec {
      container_concurrency = var.config.container_concurrency
      timeout_seconds       = var.config.timeout_seconds
      containers {
        ports {
          container_port = 8080
        }
        image = "TODO image artifact registry"
        resources {
          limits = {
            cpu    = var.config.cpu_limit
            memory = var.config.memory_limit
          }
        }
      }
    }
  }
}
$ terraform apply
cool_app_infra.tf [X]
variable "regions" {
  type        = list(string)
  default = [
    "us-central1",
    "europe-west2", # ...more
  ]
}
variable "config" {
  type = object({
    timeout_seconds       = number
    container_concurrency = number
    cpu_limit             = string
    memory_limit          = string
  })
  default = {
    timeout_seconds       = 120
    container_concurrency = 50
    cpu_limit             = "2"
    memory_limit          = "2048Mi"
  }
}
cool_app_config.tf [X]

- automate end to end release and deployment?

- multiple environments?

- commit previews?

- cost?

Dart on the Edge + Cloud

globe.dev

DEMO

Early access @ globe.dev

/slides

/github

/twitter

invertase.link

Thank you!

Deploying your Dart server applications around the Globe.

By Mike Diarmid

Deploying your Dart server applications around the Globe.

  • 705