112 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| # goserv
 | |
| 
 | |
| > Boilerplate for how I like to write a backend web service
 | |
| 
 | |
| ## Build
 | |
| 
 | |
| ```bash
 | |
| export GOFLAGS="-mod=vendor"
 | |
| go mod tidy
 | |
| go mod vendor
 | |
| go generate -mod=vendor ./...
 | |
| go build -mod=vendor .
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| ./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**:
 | |
| 
 | |
| ```bash
 | |
| curl -fsS https://webinstall.dev | bash
 | |
| export PATH="$HOME:/.local/bin:$PATH"
 | |
| ```
 | |
| 
 | |
| ```bash
 | |
| 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.
 | |
| 
 | |
| ```bash
 | |
| my_vps='example.com'
 | |
| ssh root@"$my_vps" 'curl -sS https://webinstall.dev/ssh-adduser | bash'
 | |
| ```
 | |
| 
 | |
| You can then login as a normal user.
 | |
| 
 | |
| ```bash
 | |
| ssh app@"$my_vps"
 | |
| ```
 | |
| 
 | |
| It is now safe to disable the root account.
 | |
| 
 | |
| ### Caddy (Automatic HTTPS Server)
 | |
| 
 | |
| ```bash
 | |
| curl -fsS https://webinstall.dev/caddy | bash
 | |
| ```
 | |
| 
 | |
| You can start Caddy as a system service under the app user like this:
 | |
| 
 | |
| ```bash
 | |
| 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)
 | |
| 
 | |
| ```bash
 | |
| curl -fsS https://webinstall.dev/postgres | bash
 | |
| ```
 | |
| 
 | |
| You can start Postgres as a system service under the app user like this:
 | |
| 
 | |
| ```bash
 | |
| 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:
 | |
| 
 | |
| ```bash
 | |
| 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.
 |