Skip to main content

Nim App on Section

Learn how to run a "Hello World" Nim app at the edge for low latency and high availability. You can use our repo as a template, or perform the steps yourself using the Kubernetes dashboard or kubectl commands.

What You'll Build

Visit https://fragrant-smoke-2968.section.app to see what you'll be building.

Option 1 - Copy Our GitHub Repo

workflow status

Make a new repo from our template: in your browser visit https://github.com/section/nim-template and select Use this template (don't clone, don't fork, but use the template). Choose yourself as an owner, give it a name of your choice, and make it be Public (not Private).

  1. In your new GitHub repo, under Settings > Secrets > Actions, use New repository secret to add these two:
  2. Make any change to ./helloworld.nim and watch your changes go live.

Option 2 - Step by Step

Following are step-by-step instructions to deploy a Nim "Hello World" application to the edge on Section. We'll Dockerize it, and deploy it on Section.

Prerequisites

  • You need Docker installed so that you can build a docker image.

Create the Nim App

Create a new directory for your app.

mkdir my-nim-app
cd my-nim-app

Create helloworld.nim with the following code.

helloworld.nim
import std/asynchttpserver
import std/asyncdispatch

proc main {.async.} =
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
echo (req.reqMethod, req.url, req.headers)
let headers = {"Content-type": "text/plain; charset=utf-8"}
await req.respond(Http200, "Hello World from Nim on Section!", headers.newHttpHeaders())

server.listen(Port(8080))
let port = server.getPort
echo "Web server is now running on port " & $port.uint16
while true:
if server.shouldAcceptRequest():
await server.acceptRequest(cb)
else:
# too many concurrent connections, `maxFDs` exceeded
# wait 500ms for FDs to be closed
await sleepAsync(500)

waitFor main()

Dockerize It

Let's build the container image that we'll deploy to Section. First make a Dockerfile in your directory with the following content.

Dockerfile
FROM nimlang/nim
COPY . ./my-nim-app
WORKDIR /my-nim-app
EXPOSE 8080
CMD [ "nim", "c", "--run", "./helloworld.nim" ]

Build and tag it.

docker build . -t ghcr.io/YOUR_GITHUB_USERNAME/my-nim-app:prod

Launch it locally to test it.

docker run -p 8080:8080 ghcr.io/YOUR_GITHUB_USERNAME/my-nim-app:prod
curl http://localhost:8080

Push It

Push it to GitHub Packages. This makes it available to Section.

docker push ghcr.io/YOUR_GITHUB_USERNAME/my-nim-app:prod

Be sure to make it public. To see your packages and make this change, visit https://github.com/YOUR_GITHUB_USERNAME?tab=packages

Deploy It

Next, use the Create Project command in the Section Console in order to deploy your new container. Use the image name ghcr.io/YOUR_GITHUB_USERNAME/my-nim-app:prod with port 8080.

See the pods running on Section's network with either the Kubernetes dashboard or kubectl get pods -o wide. The -o wide switch shows where your app is running according to the default AEE location optimization strategy. Your app will be optimally deployed according to traffic. In lieu of significant traffic, your deployment will be made to default locations.

Try kubectl logs POD to see the log message reporting that the server is listening on port 8080 (Web server is now running on port 8080)

Finally, follow the instructions that configure DNS and TLS.

See What You've Built

See the "Hello World!" app you've built by visiting the https://YOUR.DOMAIN.COM, substituting YOUR.DOMAIN.COM according to your DNS and HTTPS configuration.