GoCommerce is Netlify’s headless e-commerce for JAMstack sites. Whatever that means.

It acts as your database when you have a single page app (possibly in React) that sells stuff. Integrates through <script> tags with JSON in them.

I hear Netlify is working on a fully hosted solution. It’s not ready yet.

Digital Ocean droplets are a glorified VPS, virtual private server. I’m told it’s actually an IaaS (infrastructure as a service) and competes with Amazon’s AWS…

Sure felt a lot like ssh-ing into a VPS and mucking about. πŸ€·β€β™€οΈ

Recently, I had to make these two work together. I looked for a guide, but none could be found. So here’s a collection of my notes with some explanation.

The whole process takes about 10 minutes, if you know what you’re doing. Writing this will help me know what I’m doing next time πŸ™‚

Get a droplet, then set it up

I was given an existing droplet. This assumes you have a Digital Ocean Droplet running Ubuntu. Just the initial “Here’s your VPS” setup that comes out of the box.

πŸ‘‰ First step of this tutorial

Next we have to set up a user because running things as root is bad. I won’t go into why it’s bad right now because it’s just something I remember from my Linux-using days many years ago. It’s bad. Don’t do it.

πŸ‘‰ Follow this tutorial

The instructions boil down to:

  1. Login as root
  2. Create new user (I used gocommerce)
  3. Give it sudo privileges
  4. Add your personal public ssh key to the server’s authorized keys
  5. Login as new user

Login, install SQLite

Once you have a new user, it’s time to login and set up Gocommerce. This involves installing golang, sqlite3, and a few knickknacks.

Login to your server. My user was called gocommerce.

$ ssh gocommerce@<YOUR_IP>

Then install sqlite3. SQLite is a simple database that’s easy to setup. If you want to use this for real production, I suggest opting for Postgres.

πŸ‘‰ Following this Quora answer

$ sudo apt-get update
$ sudo apt-get install sqlite3 sqlite3-dev

apt is Ubuntu’s package management system. Much like npm for JavaScript. update updates the local listing of software packages, install installs them. I used to know why this was apt-get and not just apt.

I also used to know why you need to install sqlite3-dev, but I have since forgotten. Doesn’t matter.

You now have SQLite and can create local database. Single file, SQL interface. Wonderful.

Install Go

Installing Golang is a little harder. You can’t use the default package because Gocommerce needs at least Go version 1.7.

As I painfully discovered, the official version in Ubuntu packages is 1.6, and that’s not good enough.

πŸ‘‰ Followed this guide to install

πŸ‘‰ And this guide to configure

Here’s what it boils down to.

You install Go with these apt-get incantations.

$ sudo add-apt-repository ppa:gophers/archive
$ sudo apt-get update
$ sudo apt-get install golang-1.9-go

I installed the incorrect version of Go at first, so I also had to overwrite its executable with a link to the new one. You might not have to do this, and it’s probably a bad way of doing it, but πŸ‘‡

$ cd /usr/bin
$ sudo rm go
$ sudo ln -s /usr/lib/go-1.9/bin/go

When you run go version it should say 1.9.

Then we get to create $GOPATH and set it up in our .bashrc.

$ vim ~/.bashrc

Add these lines at the end of that file:

export GOPATH="$HOME/go"
export GOBIN="$GOPATH/bin"
export PATH="$GOBIN:$PATH"
export PATH="$PATH:/usr/local/go/bin"

This creates your $GOPATH and $GOBIN variables then adds them to $PATH. Go needs these to be able to execute stuff and load itself up.

Next you have to create that directory β†’

$ mkdir -p ~/go/src

This puts a directory called go in your home dir and a src dir inside that. ~/go/src is where all your Go code is going to live.

Build, configure, and run Gocommerce

Now that you have Go running, it’s time to set up Gocommerce.

First, create the directory it’s going to live in.

$ mkdir ~/go/src/github.com/netlify

Next go there and clone the repo from Github. Ubuntu comes with git preinstalled, so there’s no need to worry about that.

$ cd ~/go/src/github.com/netlify
$ make deps
$ make build_linux

make deps installs Go dependencies that Gocommerce uses and make build_linux builds Gocommerce itself. You have to build it for Linux because we’re running Ubuntu.

On your dev machine, if it’s a Mac, you’d use make build to compile for a Mac. You can see what Netlify considers the default πŸ™‚

Configure

To configure Gocommerce, you edit a .env file in its dir, ~/go/src/github.com/netlify/gocommerce/.env

You can see details in Gocommerce README. Something like this πŸ‘‡

GOCOMMERCE_SITE_URL=http://jamcommerce.netlify.com
GOCOMMERCE_JWT_SECRET="thisissupersecret"
GOCOMMERCE_DB_DRIVER=sqlite3
DATABASE_URL=gotrue.db
GOCOMMERCE_DB_AUTOMIGRATE=true
GOCOMMERCE_API_HOST=<your ip>
PORT=9111
GOCOMMERCE_MAILER_HOST=smtp.sendgrid.net
GOCOMMERCE_MAILER_PORT=587
GOCOMMERCE_MAILER_USER=swizec+jamcommerce@swizec.com
GOCOMMERCE_MAILER_PASS=<mailer pass>
GOCOMMERCE_MAILER_SUBJECTS_ORDER_CONFIRMATION="Thank you for your order!"
GOCOMMERCE_MAILER_SUBJECTS_ORDER_RECEIVED="A new order has been placed"
GOCOMMERCE_PAYMENT_STRIPE_ENABLED=true
GOCOMMERCE_PAYMENT_STRIPE_SECRET_KEY=<your stripe key>

The tricky one to find was that GOCOMMERCE_DB_AUTOMIGRATE. That keeps your database schema up to date.

You’ll need Stripe (or Paypal) and some sort of SMTP service. I’m using sendgrid here.

Run

You can run Gocommerce using ./gocommerce. This runs the service directly, and you can check that it’s running at http://<your ip>:9111.

But it’s going to stop running once your ssh session times out. Not good.

We have to turn gocommerce into a Daemon, a script that runs in the background.

πŸ‘‰ Follow this guide for creating a custom systemd service

systemd is the system that Ubuntu uses to manage automatically starting scripts.

To do this, you create /etc/systemd/system/gocommerce.service. I used vim, so sudo vim /etc/systemd/system/gocommerce.service.

[Unit]
Description=Gocommerce Service
After=network.target

[Service]
Type=simple
User=gocommerce
WorkingDirectory=/home/gocommerce/go/src/github.com/netlify/gocommerce
ExecStart=/home/gocommerce/go/src/github.com/netlify/gocommerce/gocommerce
Restart=on-abort

[Install]
WantedBy=multi-user.targer 

This assumes the user you created earlier is called gocommerce and that you haven’t moved the gocommerce executable.

You can now start the service.

$ systemctl start gocommerce

You should see a 404 error page on http://<your ip>:9111. That means gocommerce is running and serving request.

πŸ‘Œ

You can now close your ssh connection and gocommerce should keep running in the background forever.

Congratz.

Learned something new? πŸ’Œ

Join 8,400+ people becoming better Frontend Engineers!

Here's the deal: leave your email and I'll send you an Interactive ES6 Cheatsheet πŸ“– right away.Β  After that you'll get an email once a week with my writings aboutΒ React, JavaScript,Β  andΒ life as an engineer.


You should follow me on twitter, here.