Guide

December 1, 2023

Launch a Rust Actix App

Build and Deploy a Rust Web API Using the Actix Framework. This Template comes configured with a Postgres database.

Overview

Rust developent and deployment on Noop is straightforward and productive.

This guide is based on the Postgres example published by the Actix Framework maintainers.

If you’re interested in starting a Actix app from scratch, install Workshop and select the Rust Actix Template. The create-Template process in Workshop will automate everything outlined in this guide.

If, on the other hand, you want to modify an existing project to use Noop, the guide here can be used as reference.

The full source code constructed for this guide, including the Noop configuration, see the Template repository.

Setup

Noop Dependencies

Make sure you have Workshop installed locally or a Github account connected to Noop Cloud. The remaining setup is identical in both.

Project Setup

OTo start a new Actix project, follow their official getting started guide, or use your own project.

The complete Template and project source code for the example created in this guide can be found on Github.

Template Configuration

After you have the Actix project set up, you need to add the Noop Template configuration to create a local or cloud Deployment.

Create a .noop directory in your project root.

mkdir .noop

Next create a blueprint.yaml file to define the application setup.

The content below is the necessary config for our app:

---
components:
  - name: ActixApi
    type: service
    image: rust:latest
    port: 8080
    build:
      steps:
        - directory: /app
        - copy: [Cargo.toml, Cargo.lock]
          destination: ./
        - copy: src
          destination: ./src
        - copy: sql
          destination: ./sql
        - run: cargo build --release
    runtime:
      resources:
        - PgDatabase
      command: /app/target/release/actix-api
      variables:
        PG__USER:
          $resources: PgDatabase.username
        PG__PORT:
          $resources: PgDatabase.port
        PG__HOST:
          $resources: PgDatabase.host
        PG__PASSWORD:
          $resources: PgDatabase.password
        PG__DBNAME:
          $resources: PgDatabase.database
        PG__POOL__MAX_SIZE: "16"
        SERVER_ADDR: "rust.local.noop.app"
routes:
  - target:
      component: ActixApi
resources:
  - name: PgDatabase
    type: postgresql

Automating Cloud Setup

For Cloud users, most of the Application setup is automated, including Environment creation, database provisioning, building and deploying, etc., using a Runbook. To create a Runbook, make a file called quickstart.yaml in the .noop/runbooks directory.

Here is the Runbook content:

name: Quickstart Setup
description: Demo full stack setup of a Actix app with internet endpoint
workflow:
  inputs:
    - name: EnvironmentName
      description: What should we name your new environment?
      type: string
      required: true
      default: Quickstart Demo

    - name: Cluster
      type: Cluster
      description: Which cluster should the environment launch on?
      required: true

  timeout: 300

  steps:
    - name: Environment
      action: EnvironmentCreate
      params:
        name:
          $inputs: EnvironmentName
        production: false
        appId:
          $runbook: Application.id
        clusterId:
          $inputs: Cluster.id

    - name: Build
      action: BuildExecute
      params:
        sourceCodeId:
          $runbook: SourceCode.id
        appId:
          $runbook: Application.id

    - name: Resources
      action: ResourceLaunch
      params:
        envId:
          $steps: Environment.id
        sourceCodeId:
          $runbook: SourceCode.id

    - name: Deploy
      action: DeploymentExecute
      params:
        envId:
          $steps: Environment.id
        buildId:
          $steps: Build.id

    - name: Endpoint
      action: InternetEndpointRandom
      params:
        orgId:
          $runbook: Organization.id
        routes:
          - name: 'Demo Environment'
            target:
              environments:
                - $steps: Environment.id

Launch on Noop

Cloud users can use the Runbook to automate most of the setup described below.

Create an Application

Select “New Application” and assign an appropriate name for your new app.

Cloud Setup

In Cloud, once the Application is created you will be asked if you want to execute a Runbook. The name of the Runbook for this Template is “Quick Start”.

Choose the default cluster and run it. Behind the scenes this Runbook is:

  • Creating an Environment
  • Setting up placeholders for required environment variables
  • Launching the database
  • Building the source code
  • Deploying the build
  • And finally, creating a public-facing endpoint to serve the application from

Once complete, visit the Application page to get the generated endpoint. Your new Actix app is now running at that URL.

The Noop Template Blueprint contains all the details about the application. Including build process, required resources, application routing rules, runtime configuration, etc.

Workshop Setup

While workshop doesn’t have a Runbook, the setup is also automated.

In Workshop select your Application, which should have been automatically detected by Workshop, or select the appropriate project directory (must include a .noop directory).

Workshop will automatically create a default environment with all Resources launched.

Once done, you should see your application running on the associated endpoint.

Post-launch Setup

After the Environment is up and running in either Cloud or Workshop, this example requires a database schema to be in place before the application can function properly.

From the Console Environment page (the same in Workshop and Cloud), select the Postgres database resource. On the Postgres Resource page you will find a Resource Explorer where you can input a query.

Past the contents from the sql/schemas.sql file.

To verify the table was created run SELECT * FROM users; in the Resource Explorer. You should see an empty result set with the columns belonging to the just added users table.

Testing It Out

Once the app is deployed, it can be used by sending a POST request to the users endpoint. See curl example below (replace with the generated Noop Endpoint host):

curl -i -d '{"email": "hello@noop.dev", "first_name": "noop", "last_name": "dotdev", "username": "noop"}' -H 'Content-Type: application/json' https://<ENDPOINT>/users