Deploy a Shelf Server

To get started, we're going to create a simple Dart application that will be deployed to Globe. We'll use Shelf to start an HTTP server that can handle requests and log some information about our application.

In order for Globe to correctly run your backend application, it must be able to listen to the PORT environment variable, for example, using the Platform.environment['PORT']

Create a new Dart project#

Let's go ahead and create a basic Dart application. In your terminal, run the following command where you'd like to create a project:

dart create my_shelf_app

Rename the file lib/my_shelf_app.dart to lib/main.dart - this is the entrypoint for our application. Next, install the shelf package via pub:

dart pub add shelf

Copy the following code into lib/main.dart:

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,
            int.tryParse(Platform.environment['PORT'] ?? '8080') ?? 8080,
            );

    print('Serving at http://${server.address.host}:${server.port}');
}

Response _echoRequest(Request request) =>
Response.ok('Request for "${request.url}"... worked!');

To test your application out locally, run dart run lib/main.dart and visit http://0.0.0.0:8080/hello in your browser. You should see the following: Request for "hello"... worked!.

This is a basic HTTP server that will respond to any request with a 200 OK response and the body Request for "${request.url}"... worked!, whilst also logging out information about the request. From here you could build out a full application yourself, adding in routing, database calls, middleware and more. For this example, we'll keep it simple.

Deployment#

Now that we have a basic application, we can deploy it to Globe. Deploying to Globe is simple; run the globe deploy command from your project root in the terminal:

globe deploy

The first time you deploy, you'll:

  1. Be prompted to continue with the setup of the deployment (press Y)
  2. Enter a name for your project: Enter: my-shelf-app

After waiting for a couple of seconds, you'll be shown that your new deployment has been queued and be provided a unique URL for that deployment. You can visit this URL in your browser to view the build logs and deployment status. You can also use the build-logs command to view the logs directly from the CLI.

Viewing your deployment logs#

Once complete, your deployment will be available via the URL shown in the dashboard. Each deployment has its own unique URL, with a globeapp.dev domain. Click the URL and you'll be shown the Request for "hello"... worked! message that you saw when running your application locally.

Since our application has both a print statement and a middleware logger (provided by Shelf), we're able to inspect those logs in realtime from the dashboard. Click the "Logs" tab in the dashboard to view the logs for your deployment. Each time you refresh the deployment URL, a new log will appear in the dashboard! Any errors (which emit to stderr) will be highlighted in red.