2020-09-20 03:55:46 +00:00
|
|
|
# goserv
|
|
|
|
|
|
|
|
> Boilerplate for how I like to write a backend web service
|
2020-09-28 07:26:16 +00:00
|
|
|
|
2020-09-30 04:34:20 +00:00
|
|
|
## Build
|
|
|
|
|
2020-09-28 07:26:16 +00:00
|
|
|
```bash
|
2020-09-30 04:34:20 +00:00
|
|
|
export GOFLAGS="-mod=vendor"
|
2020-09-28 07:26:16 +00:00
|
|
|
go mod tidy
|
|
|
|
go mod vendor
|
|
|
|
go generate -mod=vendor ./...
|
|
|
|
go build -mod=vendor .
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
|
|
|
./goserv run --listen :3000 --serve-path ./overrides
|
|
|
|
```
|
2020-09-29 03:17:10 +00:00
|
|
|
|
2020-09-30 08:29:24 +00:00
|
|
|
## Eamples and Config Templates
|
|
|
|
|
|
|
|
The example files are located in `./examples`
|
|
|
|
|
2020-09-30 08:35:56 +00:00
|
|
|
- Caddyfile (web server config)
|
|
|
|
- .env (environment variables)
|
|
|
|
- build.sh
|
2020-09-30 08:29:24 +00:00
|
|
|
|
|
|
|
## 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
|
|
|
|
|
2020-09-29 03:17:10 +00:00
|
|
|
## License
|
|
|
|
|
|
|
|
Copyright 2020. All rights reserved.
|