In this quick post, I will show you how to build a Serverless function in Go to get the latest 9Gag Memes using OpenFaaS.
This tutorial assume that you have:
- faas-cli installed – The easiest way to install the faas-cli is through cURL:
curl -sSL https://cli.openfaas.com | sudo sh
- Swarm or Kubernetes environment configured – See Docs.
Create a handler.go file with the following content:
The code is self-explanatory, it uses 9Gag Web Crawler to parse the website and fetch memes by their tag.
I wrote a simple Dockerfile using the Multi-stage builds technique to reduce the image size down:
FROM golang:1.9.1 AS builder
Note: If pushing to a remote registry change the name from mlabouardy to your own Hub account.
Issue the following command:
faas-cli build -f ./stack.yml
faas-cli push -f ./stack.yml
Once deployed, you can invoke the function via:
curl http://localhost:8080/function/memes-9gag -d "GoT"
echo "GoT" | faas-cli invoke memes-9gag
Note: all code used in this demo, is available on my GitHub 😍
Drop your comments, feedback, or suggestions below — or connect with me directly on Twitter @mlabouardy.