Skip to content

fable-compiler/cfworker-hello-world

Repository files navigation

FSharp/Fable 'Hello World' on Cloudflare Workers

This Repo shows how to run 'Hello World' in FSharp on Cloudflare Workers via the Fable JavaScript transpiler. Workers are a simple inexpensive way to execute functions on Cloudflare edge network. They can be used for anything from utilities to full-on WebAPI's. For a more detailed description of Workers in FSharp see:

Setting Up Your Environment

Prerequisits

  • A Cloudflare account, either paid or free. Needed to provide the hosting environment to which your worker will be deployed.
  • Wrangler, the Cloudflare Workers CLI. This works with the webpack.config.js file to build and deploy your worker.
  • .NET SDK, used to generate an F# abstract syntax tree from which the JavaScript is generated.
  • Node.js, used to support the tooling to convert the AST to JavaScript.
  • An editor with F# support. VisualStudio Code with Ionide is recomended.

Install and Check Prerequisits

Perform the following as some simple checks to ensure the pre-requisits are in place. At time of writing the following were working:

  • Check you are able to edit F# files.
  • Log into Cloudflare, you should be able to view the workers pannel.
  • wrangler --version -> v1.10.3
  • dotnet --version -> .NET Core 3.1 or .Net 5.0
  • node -v -> v12.18

Configure Wrangler

To authenticate wrangler commands it is recomended you configure wrangler with your APIKey using wrangler config.

Generating and Testing a Worker

Generate a New Project

To create a new project based on this template execute:

wrangler generate projectname https://github.com/fable-compiler/cfworker-hello-world

Build and Deploy to Dev

  1. Run dotnet tool restore
  2. Run dotnet fable watch src --outDir tmp --run wrangler dev This will run Fable and wrangler dev both in watch mode. Fable compiles F# to JavaScript. Wrangler then pushes the new javascript to your accounts Cloudflare Dev environment and starts a stub running locally for testing. Cloudflare has a blog explaining how this works.

Test the Dev Worker:

MBPro:~ $ curl localhost:8787
Hello from Fable at: Mon Oct 19 2020 19:30:39 GMT+0000 (Coordinated Universal Time)

Publish to Your Cloudflare Account

To publish your worker to your Cloudflare account first configure a route and zone id in your ./wrangler.toml file. Then execute wrangler publish this will deploy the worker javascript file as specified in the TOML file.

 

Note to Contributors: PRs are welcome. Test changes by:

  • Creating a new project from the template: wrangler generate my-proj file://"path_to_local_repo"
  • Building and deploying the worker from root of generated my-proj: dotnet fable watch src --outDir tmp --run wrangler dev
  • Testing the worker: $ curl localhost:8787

Hello from Fable at: Sun Nov 08 2020 17:41:19 GMT+0000 (Coordinated Universal Time)