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:
- Login as root
- Create new user (I used
gocommerce
) - Give it
sudo
privileges - Add your personal public ssh key to the server's authorized keys
- 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.comGOCOMMERCE_JWT_SECRET="thisissupersecret"GOCOMMERCE_DB_DRIVER=sqlite3DATABASE_URL=gotrue.dbGOCOMMERCE_DB_AUTOMIGRATE=trueGOCOMMERCE_API_HOST=<your ip>PORT=9111GOCOMMERCE_MAILER_HOST=smtp.sendgrid.netGOCOMMERCE_MAILER_PORT=587GOCOMMERCE_MAILER_USER=swizec+jamcommerce@swizec.comGOCOMMERCE_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=trueGOCOMMERCE_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 ServiceAfter=network.target[Service]Type=simpleUser=gocommerceWorkingDirectory=/home/gocommerce/go/src/github.com/netlify/gocommerceExecStart=/home/gocommerce/go/src/github.com/netlify/gocommerce/gocommerceRestart=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?
Want to become a high value JavaScript expert?
Here's how it works π
Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet πright away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.
Start with an interactive cheatsheet π
Then get thoughtful letters π on mindsets, tactics, and technical skills for your career.
"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. π"
Have a burning question that you think I can answer?Β I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.
Ready to Stop copy pasting D3 examples and create data visualizations of your own? Β Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization
Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.
Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first π° on the side with ServerlessReact.Dev
Want to brush up on your modern JavaScript syntax?Β Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you areΒ β€οΈ