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.
As the Image shows, this is the Idea.
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.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.
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.
Lets list down the choices I have
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 š„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 I
mage D
ynamic A
pi 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.
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Sidago!")
}
go run .
Perfect!
Setting up netlify is super simple.
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
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.]
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
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.
Email Address
Powered by Buttondown
Divyanshu Negi is a VP of Engineering at Zaapi Pte.
X