Boilerplate for how I like to write a backend web service.
Go to file
AJ ONeal e2cf7bc33b add deploy docs 2020-09-30 02:29:24 -06:00
assets add postgres db init, and stub out some API routes 2020-09-30 01:56:23 -06:00
examples add deploy docs 2020-09-30 02:29:24 -06:00
internal/db add postgres db init, and stub out some API routes 2020-09-30 01:56:23 -06:00
public add health check page 2020-09-30 02:14:33 -06:00
tools static server with basic options 2020-09-28 01:26:16 -06:00
vendor vendor deps 2020-09-30 02:01:41 -06:00
.gitignore static server with basic options 2020-09-28 01:26:16 -06:00
.prettierignore initial commit 2020-09-19 21:55:46 -06:00
.prettierrc initial commit 2020-09-19 21:55:46 -06:00
AUTHORS update LICENSE and AUTHORS 2020-09-28 21:17:10 -06:00
LICENSE initial commit 2020-09-19 21:55:46 -06:00
README.md add deploy docs 2020-09-30 02:29:24 -06:00
go.mod add postgres db init, and stub out some API routes 2020-09-30 01:56:23 -06:00
go.sum add postgres db init, and stub out some API routes 2020-09-30 01:56:23 -06:00
main.go add deploy docs 2020-09-30 02:29:24 -06:00

README.md

goserv

Boilerplate for how I like to write a backend web service

Build

export GOFLAGS="-mod=vendor"
go mod tidy
go mod vendor
go generate -mod=vendor ./...
go build -mod=vendor .
./goserv run --listen :3000 --serve-path ./overrides

Eamples and Config Templates

The example files are located in ./examples

  • Caddyfile (web server config)
  • .env (environment variables)
  • build.sh

Dependencies

This setup can be run on a VPS, such as Digital Ocean, OVH, or Scaleway for $5/month with minimal dependencies:

  • VPS
  • Caddy
  • PostgreSQL
  • Serviceman

Mac, Linux:

curl -fsS https://webinstall.dev | bash
export PATH="$HOME:/.local/bin:$PATH"
webi caddy serviceman postgres

VPS Setup

You should have a domain pointing to a VPS and create a user account named app (because that's a common convention). This script will create an app user, copying the authorized_keys from the root account.

my_vps='example.com'
ssh root@"$my_vps" 'curl -sS https://webinstall.dev/ssh-adduser | bash'

You can then login as a normal user.

ssh app@"$my_vps"

It is now safe to disable the root account.

Caddy (Automatic HTTPS Server)

curl -fsS https://webinstall.dev/caddy | bash

You can start Caddy as a system service under the app user like this:

sudo setcap 'cap_net_bind_service=+ep' "$(readlink $(command -v caddy))"

sudo env PATH="$PATH" \
    serviceman add --name caddy --username app \
    caddy run --config ./Caddyfile

See the Cheat Sheet at https://webinstall.dev/caddy and https://webinstall.dev/serviceman

PostgreSQL (Database)

curl -fsS https://webinstall.dev/postgres | bash

You can start Postgres as a system service under the app user like this:

sudo env PATH="$PATH" \
    serviceman add --name postgres --username app -- \
    postgres -D /home/app/.local/share/postgres/var -p 5432

Username and password are set to 'postgres' by default:

psql 'postgres://postgres:postgres@localhost:5432/postgres'

See the Cheat Sheets at https://webinstall.dev/postgres and https://webinstall.dev/serviceman

License

Copyright 2020. All rights reserved.