Creating an API to generate Images using Go programming language and hosting on serverless

Divyanshu Negiā€¢

ā€¢

šŸš€šŸš€šŸš€ 5 min read

Part 1

I want to generate meta images for this blog, So when I share these blogs on twitter, Linkedin, Facebook, slack, etc, I want to see the image as well, right now it only shows the title and description.

One way to do that is I can set a single image for all the blogs, but thats no fun.

Another way is, When I upload these blogs I also add an Image with it to my CMS, which can be extracted using the API on my blog links meta tags. Again this adds a friction for me to add Images and create images.

My Solution is, I will create an API endpoint like

https://mywebsite.com/api/meta-image/title_of_blog-w_100_h_100.jpg

This endpoint would create an Image for me or return an existing Image in the server with the specific name (if exists), Also different social media work with different kind of sizes so I want to make my API support a dynamic Image size. An easy solution could be, If I just create 1 size Image (using my function) and upload the Image to cloudinary, which can later convert that Image into required sizes, but again no fun in that, I might want to customise the Images based on its size requirements in my function only.

Image from Level Up Coding via Google Image search

As the Image shows, this is the Idea.

  1. I want the function to be fully customisable, I could have used something like https://github.com/micheleriva/gauguin, but that still is not what helps me to customise at code level unless I want to contribute on it.
  2. I want the code to be small and execute fast on a serverless deployment.
  3. The reason for choosing Go is due to the fact that its the fastest language on serverless as its compiled compared to JS and Python which are interpreted at runtime.

The Design

I quickly went to FIGMA and started creating a mock of what I want as an Image for my blogs.

here is something I came up with :

I want my API request to create an Image like this, with the title of the blog post as a dynamic data.

The Setup

To create the Go function, first I need to find where can I host this function ?

The host I want to use could easily be simulated on my local environment, easy deployment, auto CICD a plus.

Where to Host ?

Lets list down the choices I have

  1. FIrebase Cloud Function : My go to choice for JS, TS but unfortunately they do not support Go at this time. šŸŸ„
  2. Lambda : AWS Lambda is a good choice, but I personally do not like the AWS route, too complicated to setup and not good for small tasks šŸŸ„
  3. Cloudflare workers : I did a digging around this, found few docs which said it supports Go and the 0ms boot really enticed me to work with it, but the docs were mostly inconclusive to support Go and some were quite complicated as hacky solutions using JS with Go into WASM šŸŸ„
  4. Netlify Functions : Interesting, technically Netlify functions run on AWS Lambda, but it provides an abstraction to it and easy deployment, We can run it locally in our machine without setting up an AWS lambda Simulator, We need to set an adapter to convert our IO from Go to AWS Lambda and we already have an adapter named as Gateway (https://github.com/carlmjohnson/gateway) āœ…

Setup Go on Local Machine

  • Setup Go on local machine, the easiest way in a mac is brew
brew install go

It would take a minute, and once its done, time to check the version of Go using this command

go version

Perfect! šŸ‘

  • I created a new directory where I will be working on this Go project as ./Weekend_Projects/sidago, the package name is SIDAGO short for S ocial Image Dynamic Api with Go

  • Installed Go extension on VSCode, must!

  • Created a new go project as :

go mod init sidago.com/sidago 

it will create a go.mod file which is like package.json if you have worked with node projects.

  • test if go working fine locally by creating a simple go file as main.go
package main

import "fmt"

func main() {

fmt.Println("Hello, Sidago!")

}
  • run the code
go run .

Perfect!

Writing the API for Netlify Function

Setting up netlify is super simple.

  • Create a project with this structure

Here functions is created using netlify CLI , If you havent installed, Please install and authenticate with your netlify account.

  • public contains a simple index.html file, this will be hosted on netlify as a basic html file with only the name of my API.
  • .gitignore this file contains only
# Local Netlify folder
.netlify
  • build.sh is the file which we tell netlify to configure as our build command, if we want to pre-process any script we can write here. current content looks like this :
set -euxo pipefail
go env
  • Now just run a command as
netlify functions:create

This will ask you to name the function and select the language you want to make the function, I selected GoLang and named the function as Sidago. This created a function for me, now if I want to test this function locally I can write

netlify functions:serve

This will compile the function and will make it ready to be served on my localhost

One good thing about server command is it will make the hot-reloading available, so any change I make in my go file the server will immediately reload with new changes.

As this article is getting too long, I am going to pause it here, I will write part 2 of this series where I created the Image manipulation with go lang.

Also test my API.

for now to release my function I can write :

netlify deploy --prod

the flag --prod will make this release directly to our production branch, also to auto deploy you can commit the changes and push to your linked github repo as well.]

Permission Error when building function

I also got one issue while in the build command execution, the ./build.sh was throwing a permission error on netlify dashboard while executing, to fix that, I updated the build command in netlify as

chmod a+x ./build.sh && ./build.sh

So here prociding the required permissions before executing the build command, hoepefully you wont get this issue.

Thanks for reading along, Will se you in the next part of this series to create a Dynamic Image API using Go.

šŸ™

X

Did this post help you ?

I'd appreciate your feedback so I can make my blog posts more helpful. Did this post help you learn something or fix an issue you were having?

Yes

No

X

If you'd like to support this blog by buying me a coffee I'd really appreciate it!

X

Subscribe to my newsletter

Join 107+ other developers and get free, weekly updates and code insights directly to your inbox.

  • No Spam
  • Unsubscribe whenever
  • Email Address

    Powered by Buttondown

    Picture of Divyanshu Negi

    Divyanshu Negi is a VP of Engineering at Zaapi Pte.

    X