Creating a Simple API with CloudFlareWorkers (Rust)

Goal

We will create an API that takes a unixtime and returns the time.

Preparation

# Set the target
rustup target add wasm32-unknown-unknown

# A browser will open, so click on "Allow"
yarn global add wrangler
wrangler login

Preparetion

Follow the Official Guide to generate a project from the template.

npx wrangler generate unix_time_formatter https://github.com/cloudflare/workers-sdk/templates/experimental/worker-rust
cd unix_time_formatter

Running in the Development Environment

wrangler dev

When a request is received, it returns "Hello, World!".template

curl localhost:8787
Hello, World!

Modifying the Code

We will use Chrono to return the unixtime in the format yyyy/mm/dd (day of the week) HH:MM:SS. Modified Code

Testing the Functionality

(If you save while running wrangler dev, hot reloading will run and it will be updated automatically.)

curl -X GET http://127.0.0.1:8787/$(date +%s)
2023/7/21(Fri) 08:51:05

Deploy

wrangler publish

# Published worker-rust (0.29 sec)
# URL will be generated

Testing in Production

After running wrangler publish, a URL will be issued.

curl -X GET [Insert the issued URL here]/$(date +%s)
2023/7/21(Fri) 08:52:33