by Francis Cote

A guide to Golang e-commerce

Get your e-commerce site up and running with this Go-powered CMS tutorial

Photo by Kenny Luo on Unsplash

“We haven’t played with Go in a while.”

Our lead developer, reflecting on our content roadmap.

“I haven’t worked with Go at all,” I think to myself.

Today, I’ll give it a go (yeah, I know 🤦‍♂️) by exploring the Golang ecosystem and the e-commerce possibilities it offers.

I’ll then show you how to craft your own Go-powered online shop with the help of Ponzu CMS.

Tutorial steps:

  1. Setting up Ponzu CMS.
  2. Creating the store frontend with a Go static site generator.
  3. Deploying the website to Netlify.
  4. Enabling e-commerce capabilities with Snipcart.

Let’s first take a step back and immerse ourselves in the Golang universe.

Is Golang any good for e-commerce?

What’s Go?

I think it’s still legitimate to call Go an up-and-coming programming language, since the developer community defines it as one of the languages it most wishes to learn. It also ranks as the fifth most loved one, higher than JavaScript and C# amongst others.

This post isn’t about learning the language. If you feel like you need a proper introduction to Golang’s strengths & weaknesses, read this excellent post.

Take Hugo for instance, one of the most popular static site generators out there. Widely recognized as the fastest of its kind, it’ll generate a neat blog or e-commerce frontend in no time.

Go e-commerce backend solutions

Some of these tools are specifically made to generate online shops and e-commerce backends:

  • QOR — A Golang SDK for e-commerce and CMS development. It’s comprised of modules that abstract common features for such systems. Build by Go developers, for Go developers.
  • Ottemo — A mobile-first platform, marketed as “next generation” e-commerce for small and medium companies.
  • Upnext — This one is different as it acts as a payment gateway, leveraging Golang to create faster e-commerce checkouts.

Looking at these e-commerce solutions, you’ll quickly get that they’re mostly massive platforms which might not give you lots of freedom as developers to craft custom shopping UX.

So, are there alternatives here? Two come to mind:

  1. Writing your own custom Go e-commerce API — time-consuming option, but if you have the time and skills to do it right, hats off to you!
  2. Using a third-party solution to integrate with the Go-powered CMS/app of your choice — precisely what I’ll do in the following demo!
If none of these solutions seem to fit your needs, maybe Golang isn’t the right language/framework for your e-commerce backend. Have you thought about trying Node.js, Python/Django or PHP/Laravel?

Technical tutorial: Golang e-commerce with Ponzu CMS

Introduction to Ponzu: a Golang CMS

I’ll use Ponzu CMS to manage the content of my e-commerce app here. In a nutshell, it is an efficient Go-powered open-source HTTP server framework and CMS.

In the words of its creator Steve Manuel:

“It’s easy to use, JSON friendly, and with a fast and concurrent API server. I wanted to pick the parts of other platforms that I liked: take a great CMS backend from WordPress, a developer-first CLI to generate code for me from Rails, and a blazing fast JSON API from Netty.”

It looks like a neat minimalist framework; an excellent choice for small use cases with crazy fast installation and setup, automatic HTTPS configuration, and top-notch performances.

As a headless CMS it allows me to take a JAMstack approach.

The stack will be Go all the way down to the store website: I’ll throw Hugo in the mix as my static site generator.

So, let’s get on with it!

A bit of context for my demo

Sea levels are rising. A rise of a few feet only could get some people’s feet wet. Better be prepared! So let’s start a simple store selling some goods to help people deal with these floods. We’ll have limited stocks, so it will be crucial to keep track of our inventory.

Prerequisites

  • Basic knowledge of Go
  • Having Go installed and available in your PATH
  • A Snipcart account (forever free in Test mode)

So let’s get started.

1. Setting up Ponzu CMS and the products API

Having an instance of Ponzu running amounts to executing a few terminal commands:

Once you’ve done the above, head over to https://localhost:10443/admin.

There’ll be a one-time configuration step before you can get your hands dirty:

You’ll end up in the admin interface. Looks a bit empty, right?

That’s because there still isn’t any content type available. Ponzu provides a command line generator for this.

Let’s create a product content type. Stop the app (CTRL+C) and run the following:

When executing the command above, Ponzu creates a product.go file under the content directory of your app. This is the base of this content type definition. You can extend it to make it fit your needs (we’ll get back to that).

Right now, just leave it as-is, restart the app, and go back to the admin dashboard.

You’ll notice that the Product content type is now available in the navigation bar. By clicking it, you access the list of product entries:

Create your first product:

Bam!

Once created, your new product will instantly be available through Ponzu’s API endpoint:

That last step wraps our basic headless CMS setup. Now, the crunchy part!

2. Creating the store website with a Golang SSG

The store will be a Hugo site hosted on Netlify.

You’ll have to install Hugo. Now let’s create your site.

You can do so using the Beautiful Hugo theme and tweaking it a bit.

I won’t go to great lengths explaining the customization since this isn’t our primary focus. A more in-depth walkthrough is covered in this earlier piece.

Now, you could add a product archetype and run a command like hugo new product/my-awesome-product.md to create content, but you remember this shiny Ponzu product catalog?

You want all of your products to be added automatically, right? Let’s do it this way.

2.1 Generating products for Hugo

What you’ll need to do is code a simple program that will read the products from your Ponzu API and generate Hugo Markdown content files for them.

Since there’s already Ponzu and Hugo in the mix, it just makes sense to use Go for that, doesn’t it?

Just create a simple Go package inside your Hugo site:

Here’s the code:

The above is pretty simple; it reads through all products and creates a product content file for each one by mapping all relevant fields.

You can test this script by running go build ./ponzuImport.go

3. Deploying to Netlify

You’ll want to generate products on each build, so create the following build.sh script at the root of your website:

Then, configure Netlify to run this script when building your site. Just add this netlify.toml file at the root of your website:

You’ll have to set the PONZU_HOST_URL environment variable in your Netlify build configuration. It has to point to your live instance of Ponzu. Whether it’s a ngrok proxy of your local app (when still in development) or a cloud app, it’ll have to be set.

4. Adding e-commerce capabilities to the Go website

Let’s add required Snipcart markup under your layout’s <head> element:

Then, render buy buttons like so in your template:

Now, how about we handle this Stock field seen earlier?

4.1 Enabling inventory management

Snipcart offers inventory management in your Merchant Dashboard.

You don’t want to wait for your products to be purchased before you see them in the dashboard, so let’s crawl them in advance:

Your products are now available in Snipcart! Let’s set stocks for them:

Now, you have to adapt the import script to fetch your products stocks. To do so, you’ll have to use Snipcart’s API.

Open ponzuImport.go and add these two structs:

Next, add the following at the end of the mapPonzuProduct function:

Note that your Snipcart private API key will have to be set in the SNIPCART_PRIVATE_API_KEY environment variable in your Netlify build configuration.

Neat! From now on, running your build will feed the Stock field as well.

There is one last piece missing to our grand scheme: triggering your Netlify build when data changes.

4.2 Generating build triggers

First, create a Netlify build hook for your website. Any POST request to this URL will trigger a new deployment.

Configuring webhooks in Snipcart is easy. Paste the URL in the Webhooks configuration page of your Merchant Dashboard:

You also want to call this webhook when a change occurs in your Ponzu data. Ponzu CMS offers a handful of extension points using interfaces.

Let’s extend your product content type definition by defining save hooks. Head back to your Ponzu app and add the following to content/product.go:

Aaaaaand you’re all set! You now have fresh Golang JAMstack e-commerce setup with inventory management. How cool is that?

Live demo & GitHub repo

See the live demo here.
See GitHub repo here.

Closing thoughts

Ponzu is great. The creator put a lot of thought into making the developer journey as simple as possible. Seamless HTTPS certificate setup through Let’s Encrypt is fantastic. The build process produces a native executable that you can run anywhere.

For the production environment, I faced an issue with Heroku’s buildpack for Go — wasn’t playing well with Ponzu’s structure. I recommend running your Ponzu app in Docker.

With some hurdles here and there, I spent about two days building this demo, including the learning curve. A more seasoned Go developer should get through this way faster.

To push this tutorial further? Ponzu CMS supports HTTP/2.0, which comes with a cool feature named Server Push. When building an SPA backed by Ponzu, you can reduce the amount of remote calls you make to the backend by using Server Push to bundle multiple payloads of data that the client is likely to ask in the future!

Got any questions regarding this Snipcart + Golang tutorial? Hit the comments for any comments or questions. And if you enjoyed this post, take a second to 👏 or share on Twitter!

Originally published on the Snipcart blog and in our newsletter.