Deno by example: Content server — Part 6 Deployment on Deno Deploy

Mayank C
Tech Tonic

--

Welcome to the medium course:

Learn Deno by example — Content server.

This is a medium course that takes the user from writing, testing, formatting, logging, documenting, and deploying an application in Deno. This rapid course consists of six sections.

Here are the course contents:

In section 1, we’ve gone through the content server application along with some manual tests. In section 2, we’ve used Deno’s built-in formatter and linter to improvise on the content server application. In section 3, we’ve identified, and written unit tests to test the smallest possible units in the application. In section 4, we’ve written integration & basic performance tests to validate the application as a black box. In section 5, we’ve enabled logging and tracking in the application. Also, we’ve added a small documentation for public APIs of ES modules. In this last section, we’ll deploy this application on Deno Deploy and download content via public URLs. We’re progressively enhancing the application. It’s advisable to read sections 1, 2, 3, 4, & 5 before proceeding with the last section. Let’s get started!

Deno Deploy

Deno Deploy is like a Runtime as a service (RaaS). Deno deploy allows deployment of Deno applications at multiple datacenters across the world. It is tightly integrated with GitHub, enabling faster & continuous deployments. The official Deno Deploy docs are here. An introduction of Deno Deploy can be read here.

In their own words:

Deno Deploy is a distributed system that allows you to run JavaScript, TypeScript, and WebAssembly close to users, at the edge, worldwide. Deeply integrated with the V8 runtime, our servers provide minimal latency and eliminate unnecessary abstractions. You can develop your script locally using the Deno CLI, and then deploy it to our managed infrastructure in less than a second, without the need to configure anything.

Built on the same modern systems as the Deno CLI, Deno Deploy provides the latest and greatest in web technologies in a globally scalable way:

Builds on the Web: use fetch, WebSocket, or URL just like in the browser

Built-in support for TypeScript and JSX: type safe code, and intuitive server side rendering without a build step

Web compatible ES modules: import dependencies just like in a browser, without the need for explicit installation

Direct GitHub integration: push to a branch, review a deployed preview, and merge to release to production

Extremely fast: deploy in less than a second, serve globally close to users

Deploy from URL: deploy code with nothing more than a URL

Deno Deploy uses Deno CLI, but has a limited set of APIs. All the Deno APIs can’t be used in Deploy compatible applications.

Regions

When deployment is requested, Deno Deploy pushes the applications to 28 data centers across the world. This way the deployment stays close to the users wherever they are located. This enables them to receive faster responses. A list of available data centers (or regions as Deploy calls it) is available here.

Stateless

Deno Deploy is completely stateless. The deployed application can hold some amount of data in memory, but this data would be lost as soon as the application is restarted for new deployment, crash, etc. Deno Deploy relies on persistent storage services (like Mongo atlas, fauna db, etc.) for data storage that survives across restarts. Deno Deploy doesn’t officially support any particular data storage.

Hello world

A simple Deno Deploy hello world program is just like a regular hello world program in Deno. While low level APIs like listen() and serveHttp() are available for use, there is no need to use them directly. The best & easiest way is to use the serve() API from Deno’s standard library’s http module.

By default, a Deploy application listens on port 8000

import { serve } from "https://deno.land/std/http/server.ts";
serve((req) =>
new Response("Hello World!", {
headers: { "content-type": "text/plain" },
})
);

The above application can be deployed through following simple steps:

  • Go to dash.deno.com & sign in
  • Create a new project
  • Create new project
  • Use ‘Deploy URL’ to deploy a Deno Deploy application

The application is deployed in 28 data centers with a single click. The allocated domains are publicly available. Let’s use curl command to test it out:

$ curl https://close-oyster-72.deno.dev
< HTTP/2 200
< content-type: text/plain
< content-length: 12
< server: deploy/us-west2-a
Hello World!

The simple hello world application works perfectly with us-west2-a region serving our request.

That’s all about the introduction. Let’s make required changes and deploy the content server application on Deno Deploy.

Deployment of content server

As mentioned above, Deno Deploy doesn’t support all the APIs that are supported by Deno CLI. Therefore, the content server application needs to be updated to make it deployable.

Content server application

Instead of modifying the original application, we’ll create a directory called deploy and copy/modify the application to suit Deno Deploy.

We need to make the following changes:

  • Use serve instead of listenAndServe (default port is 8000)
  • Remove API keys as this is an open deployment
  • Use a static directory to host all the static content
  • Remove logger as Deno Deploy doesn’t support writing to files
  • Remove Deno.stat as it is not supported by Deploy
  • Remove Deno.open as it is not supported by Deploy
  • Use Deno.readFile to get file contents (inefficent, but that’s the only way with Deploy at the time of writing)

The updated content server code suitable for Deploy looks like this:

The Deploy compatible code isn’t very different from original code. It’s just a bit inefficient as the file has to be read into memory.

Deployment

Unlike hello world which was deployed using Deploy URL, the content server needs to be deployed with GitHub linking as we’re going to serve static assets. Without GitHub linking, Deno Deploy app will not be able to access the static asset directory.

Let’s start with the steps:

  • Link GitHub repo and install Deploy app
  • Define the entry point of the application (deploy/main.ts)
  • Complete the setup
  • Verify that the application is running by checking the logs

The server is up and running. With a single click, the application is deployed at 28 data centers!

With GitHub linking, any updates to the repo will update deployment automatically

Testing

Let’s test the deployed application in a number of ways.

First, we’ll use curl (running from US west coast):

$ curl https://deno-course-content-server.deno.dev/textFile.txt
< HTTP/2 200
< content-length: 22
< content-type: text/plain
< x-tracking-id: 995c9d9b-453b-40cd-a2ad-fe79bdeb93e7
< date: Wed, 01 Dec 2021 23:07:06 GMT
< server: deploy/us-west2-a
Learning Deno Is Fun!

Next, we can open the same in browser:

All the requests have been served by the closest data center (us-west). Just to test the geographical distribution, we’ll run it through reqbin that can submit an HTTP request from a different country:

The request was indeed serve by one of the European data center.

That’s all about application deployment!

This is the end of the medium course: Learn Deno by example — Content server. We’ve gone through the following parts:

Thanks for going through the six part course!

If you like these type of courses, watch for the upcoming Deno course where we’ll be building a user signup, login, and logout application using Oak & ETA templating engine.

--

--