Deno REST API on Azure WebApp Service + CI/CD through Github Action
Deno is a secure runtime for javascript and typescript that uses the V8 engine, the same as that of Node.js. The focus of this article is to dive deep into Deno, to create a REST API for Books Storage, based on a web framework and deploy it to Azure Webapp service and enable Continous Code deployment through Github Actions
Intro to Deno
Deno is a secure runtime, created by the same creator of Nodejs, it addresses the security issues Nodejs was having, therefore it is very secure from the built. It has a typescript compiler built-in. To install deno on your machine, follow the instructions mentioned in the official website
Prerequisites
- VS Code
- Deno
- Postman
- Azure Account
- Azure CLI
Getting Started
- Create a new folder, name it as — deno-books-api
- create a new file — name it as server.ts
console.log('Hello from Deno')
write the above line of code in server.ts, in order to run deno, we use the command
deno run server.ts
This will ouput the message “hello from deno in the console”.
Jumping to Web Server
Deno doesn’t use a centralized repository to store 3rd party packages, unlike npm, everything is resolved by the URL. Here we use oak middleware framework for the webserver. It is quite similar to the express in nodejs world, oak is the popular choice.
import { Application } from "https://deno.land/x/oak/mod.ts";const app = new Application();app.use((ctx) => {ctx.response.body = "Hello From Deno!";});console.log("App running on port 8000...");await app.listen({ port: 8000 });
Change the contents of server.ts to the above, a simple web server example, now if we run
deno run server.ts
it will throw an error Uncaught PermissionDenied
As mentioned earlier, deno is secure by default, so unless specified explicitly, it can’t access network, file, environment. Therefore we specify to access the network through this command: -
deno run --allow-net server.ts
Now the app outputs App running on port 8000 in the console. When we head to the browser and open localhost://8000
Yey! Our web server is up and running in minutes!!
Part 1: Create a Structure for Book API
In this example, I’m going to create a book management REST API, which includes operations such as adding a book, with title and author, retrieving a single book, getting all books, updating a book, deleting a book. In this example, I am going to use the Router supplied by the oak framework. Create a new file router.ts which will handle all our routes. and a controller folder which contains all the controllers, in this case, a book controller, so in the folder controller, create a new file, book.ts.
https://gist.github.com/jishnukoottala/71adca688ed95caada83f19496c3fb05
Create all the functions as shown in the above gist
To run the deno application, right after we save any file, we can use denon. It is like nodemon for deno. First install denon, and run
denon --init
this will create a denon.json file, where we can store our scripts, in our case the start script would be :
deno run — allow-net server.ts
Testing our API in Postman
open postman and test all requests
Like the above create a collection to test all requests.
Part — 2 — Deployment
- Create a new GitHub repository and push all the changes
- Go to Azure Portal and Create a new WebApp Service
Choose an appropriate name for your web app and select Docker Container in the publish, select appropriate region, and then click on Review + Create. This will create a new WebApp service resource in the resource group that you have mentioned.
Create secret credentials for Github
From here on, we are letting Github actions to build and deploy our application, therefore we need to create a communication bridge from GitHub to our Azure portal, Github needs certain authentication to deploy the app to your Azure portal, so the first step is to create a service principle, you can check out the documentation here
The easiest way is to run the below command in your Azure CLI (remember to get it installed beforehand)
az ad sp create-for-rbac --name "deno-rest-api-demo" --role contributor \
--scopes /subscriptions/<your-azure-subscription-id>/resourceGroups/<your-resource-group> \
--sdk-auth
This will ouput a JSON as mentioned here in the docs
Go to GitHub, your repository, click Settings, and click on secrets, click on add a secret, give the name as AZURE_CREDENTIALS. The value will the output JSON of the command we ran earlier. then add a secret.
Continuous Deployment with Github Actions
Go to Actions tab in your GitHub repository, click on a new workflow, select simple workflow
Step 1:
- name: Set up Deno
uses: denolib/setup-deno@master
with: deno-version: 1.0.1
Step 2:
- name: Bundle and zip Deno app
run: |
deno bundle server.ts server.bundle.js zip app.zip server.bundle.js
Step 3:
Get Azure Login from Marketplace
- uses: azure/login@v1.1
with: creds: ${{ secrets.AZURE_CREDENTIALS }}
Here we use, the AZURE_CREDENTIALS, we set up earlier to authenticate.
Step 4:
Search for Deploy Deno to Azure App Service
- name: Deploy Deno to Azure App Service
uses: anthonychu/azure-webapps-deno-deploy@v0.1.0
with:
# Name of the Azure Web App
app-name: <ur-app-name>
# Name of the resource group
resource-group: <ur-resource-group-name>
# Path to zip package to deploy
package: app.zip
script-file: server.bundle.js
# Version of deno to use
deno-version: 1.0.1
Now all the steps are done
Click on start commit to make the action file commit to the master branch
Now when the commit is done, it might have already started the process, you can go to the actions tab and check the process.
Step 5:
Open the resource in the Azure portal, go to overview and get the App URL
Test the production API in Postman
In postman, add a new environment, in the manage environments,
Now select the Book API Prod environment from the environment dropdown, and modify the request from our collection as {{url}}/api as shown in the below image
Conclusion
Deno, the new javascript runtime is very easy to get started, and it is on the rise, however, it is in continuous development, It would be nice to get it stable so that we can use it in our production environment.
If you like my article, please give a clap and share it with others.
Github repo of this article
Resources to learn Deno
- Traversy media — Deno Crash Course
- Deno OAK REST API Tutorial
- Github Action to deploy Deno on Azure