Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    How to setup Gocommerce on a new Digital Ocean droplet

    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.

    Published on January 23rd, 2018 in Go, Linux, Technical, Ubuntu

    Did you enjoy this article?

    Continue reading about How to setup Gocommerce on a new Digital Ocean droplet

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    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 ❤️

    Created by Swizec with ❤️