Multiple Endpoints in Go on Netlify Functions
Share my case to fill the lack of the official documentation
I picked Go for my new programming language for 2020. I touched a bit before but didn’t have enough time to write a solid thing with Go until this time. After reading through the official documents, I decided to replace my subtle tool which I had written by CoffeeScript with Go.
Netlify Functions | Netlify
Meet the modern backend. Infinitely scalable. Genuinely simple. Powered by AWS Lambda. Simplified by Netlify. Write…
The tool is Hubot Script to have endpoints to accept HTTP requests on Heroku to run some tasks like notifying to Slack team for my family.
The major use case is “Notifying summarized events from the alert for our credit-cards usage”. Receiving email notifications on Mailgun, and Mailgun makes an HTTP request into the Hubot host. Then a Hubot Script parses email and notifies to Slack.
Chose the FaaS service Netlify Functions (Actually, Netlify is wrapping AWS Lambda btw) to run those tasks. NF supports JS and Go, we can trigger functions by HTTP req, and functions could be beside of source codes for a website on Netlify. My recent projects are mostly running on Netlify, so I intended to get rid of the exception which runs on Heroku. (Feeling hard for remembering the operations to handle the app on Heroku for just maintaining the scripts 🙃)
AWS's serverless Lambda functions open a world of possibilities for running on-demand, server-side code without having…
However, I guess Netlify really doesn’t recommend Go lang for using on Netlify Functions since their document and sample codes are not well-documented and their dashboard shows as “.js” for each function 👀
If there are multiple binaries on “functions” directory, Netlify deploys each like JS functions. Only the thing Netlify miss is their sample doesn’t suppose their user may want to have multiple endpoints.
As a newbie for Go lang, I concluded that the doc didn’t guide me into what I build. Learned a bit about file structures of typical Go project, how to build multiple binaries and configured some Netlify settings. This repo is the outcome of my experiments. You can deploy a boilerplate (and create a repo on your GitHub account) by just hitting “Deploy to netlify” button 😉
Deployable template of multiple Go lambda functions for Netlify Functions.
The boilerplate gives 2 endpoints which return a message:
Each endpoint runs a compiled binary of
/cmd/[function name]/main.go. If you’d like to have common codes for multiple functions, place
/internal/pkg/[name]/[name].go and import that.
See README for more details.