Files
README.md

Project Structure #

  • ./cmd: entrypoint for Golang HTTP server
  • ./internals: business logic
  • ./web: Single Page App with Vue.js
  • ./deploy: deployment instructions

Description #

I tried to make a full-stack web app for exploring the whole development lifecycle (architecture, UI/UX, database, backend, frontend, deployment). Chose "chat" app for learning "real-time" technologies such as Websockets and Server-sent events.

Features #

[x] Contacts [x] Add/Remove/Search [x] Live update of online status [x] Group chat [x] Create/Remove/Invite/Search [x] User account [x] Signin [x] Register [x] Logout [x] Change email/fullname [x] Change profile picture [x] Messaging [x] Send files as message [x] Search message from private/group chat [x] Live update of messages received [x] Files are downloadable [x] Preview images [] Schedule send [x] Chat [x] Live update of recent messages [] Call [] Audio call [] Video call

Architecture #

  • static server & web api: Golang with standard HTTP library
  • frontend: HTML, CSS, Vue.js for Single page application
  • database: Postgresql for simple relational database

Rationale #

Golang #

  • Easy to deploy something compiled to single binary
  • Widely used for network services and web development
  • Simple mental model for concurrency
  • Fewer language features means easy to learn but can be verbose
  • Explicit error handling. Treating error as values
  • Static typing

Vue.js #

  • Preferred mental model for implementing single-page application compared to React.js
  • Use of single-file components for separation of concerns
  • Well documented APIs

Chat app #

  • To experience starting a project from zero as well as refactoring and fixing bugs as project grows
  • Learn how to integrate Websockets and Server-sent events to satisfy live update features.

Postgresql #

  • Widely used RDBMS with open-source license
  • Large community support
  • Prefer to use "relational" than "non-sql" database

Contribution #

Dependencies #

  • Go language compiler v1.23^
  • Postgresql v17
  • Javascript runtime node-v22
  • Git

CI/CD Pipeline #

Deployment #

Option 1: Run directly on virtual machine (EC2, Lightsail, DigitalOcean)

Fully responsible on setting up environment on VM from operating system up to application

Option 2: Run on a lightweight virtual machine like Firecracker VM (Fly.io)

Wrapping the app on a docker image then unwrapped when deployed. Only responsible for building docker image and not for configuring infastructure like load balancer, reverse proxy, and TLS certificate

Important

Make sure to run package manager update. sudo apt update && sudo apt upgrade if Debian based OS.

Go language compiler #

For building backend

curl -LO https://go.dev/dl/go1.23.11.linux-amd64.tar.gz
sudo rm -rf /usr/local/go || sudo tar -C /usr/local -xzf go1.23.11.linux-amd64.tar.gz
sudo echo 'export PATH=\$PATH:/usr/local/go/bin' >> /etc/profile
rm go1.23.11.linux-amd64.tar.gz

Important

You may need to re-login to server or source /etc/profile for changes to take effect.
Recommend re-login to read /etc/profile and other login scripts.

Postgresql 17 #

sudo apt install -y postgresql-common
echo | sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt install -y postgresql-17
# if current user is root
sudo -u postgres psql -c "create role root with superuser login"

Node JS runtime #

For building frontend

curl -LO https://nodejs.org/dist/v22.17.0/node-v22.17.0-linux-x64.tar.xz
sudo tar -C /opt -xJf node-v22.17.0-linux-x64.tar.xz 
sudo echo 'export PATH=\$PATH:/opt/node-v22.17.0-linux-x64/bin' >> /etc/profile
rm node-v22.17.0-linux-x64.tar.xz

Go Task #

For build scripts

go install github.com/go-task/task/v3/cmd/task@latest
sudo echo 'export PATH=\$PATH:$HOME/go/bin' >> $HOME/.profile

Cerbot #

Free SSL/TLS certificates with auto-renewal

  1. Install snapd
sudo apt install snapd
sudo snap install core
  1. Install cerbot from snap
sudo snap install --classic cerbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
  1. (OPTIONAL) Install cloudflare DNS plugin
sudo snap install certbot-dns-cloudflare
sudo snap set certbot trust-plugin-with-root=ok
  1. Setup config for letsencrypt, see config
touch /etc/letsencrypt/cli.ini

Copy contents below to cli.ini file

email = sample@mail.com
preferred-challenges = dns
agree-tos = yes
eff-email = no

Methods to run certbot:

  1. login as root, then run cerbot
  2. change ownership of the following directories, so it can be writable:
    • /var/lib/letsencrypt
    • /var/log/letsencrypt
    • /etc/letsencrypt
  3. specify config-dir = /home/user/letsencrypt/config, work-dir = /home/user/letsencrypt/work logs-dir = /home/user/letsencrypt/logs in cli.ini then run certbot

Nginx server #

Used for redirecting HTTP request to HTTPS.

sudo apt install nginx

Put this somewhere in /etc/nginx/nginx.conf or /etc/nginx/sites-enabled/default

# http {
    server {
        listen 80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
    }
# }

Important

It is recommended to remove headers that show server is running on nginx.
Install nginx-extras using sudo apt install nginx-extras.
Add more_clear_headers Server; to a server {} block inside nginx configuration.

Reload config of Nginx

sudo systemctl reload nginx

Additional dependencies for development only #

SQLC, sql compiler github repository

go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest

Install Git #

sudo apt install git

Clone repository #

git clone https://github.com/tambay-coder/GoVue.git

or using ssh

git clone git@github.com:tambay-coder/GoVue.git

Important

Use SSH Forwarding agent when using ssh to clone repository.

Setup Database #

Note

Make sure your current account is added in postgres.
If needed, set locale to 'en_SG.UTF-8'. Run sudo localectl set-locale LANG=en_SG.UTF-8, then restart postgresql sudo systemctl restart postgresql.

While in project root directory. Run sql script in schema_all.sql

sudo -u postgres psql -c "create database chat_app" postgres
sudo -u postgres psql -f internals/model/sqlc/schema_all.sql chat_app

Ask for devs to fill DB with data.

Configure .env file #

Place this in the project root directory.
You can use unix socket for DB_HOST= if postgresql is in the same machine as the web server.
If DB_PORT is blank, will use :443 by default. CA_CERT and CA_PRIV_KEY for TLS server certificate and private key

DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=
DB_NAME=
DB_ARGS=
SERVER_ADDRESS=
CA_CERT= /path/to/server/certificate ex: fullchain.pem
CA_PRIV_KEY= /path/to/server/privatekey ex: privkey.pem

Important

Change file permissions of .env file to 600 or rwx------.
sudo chmod 600 .env

Build frontend and backend #

task build

Just frontend

task build-frontend

Just backend

task build-backend

Note

See taskfile.yaml file for underlying commands.

How to get TLS certificates #

Go to certificates directory.

.env file #

Place this in certificates directory.
DNS_TOKEN and DNS_SECRET is optional depending on the authentication of domain name by certbot.
If using cloudflare, just DOMAIN_NAMES is needed.

DNS_TOKEN=
DNS_SECRET=
DOMAIN_NAMES=sample.com,www.sample.com

DOMAINS= can be single value (DOMAINS=sample.com) or list of values (DOMAINS=sample.com,www.sample.com).
You can get the values of DNS_TOKEN and DNS_SECRET from API manager of your nameserver.

Important

See documentation for instructions on how to install DNS plugin for cloudflare.

auth.sh and cleanup.sh files #

Warning

Deprecated files: auth.sh, cleanup.sh Only use this file if "spaceship.com" is the nameserver. Use cloudflare DNS plugin to manage authentication in certbot.

These scripts are used for automatic renewal of certificates, see instructions. The HTTP requests are based on DNS provider's API(spaceship.com). Change the HTTP request based on DNS provider's API.

Important

  • Regex matching of domains and subdomains are assuming single top level domains .com, .cc., .ph and not .co.uk.
  • In auth.sh file, sleep 5 is for waiting for the DNS records to propagate to other nameservers.

deploy.sh file #

Script for reloading TLS certificates without killing the server. This is done automatically by certbot.

certbot.sh file #

This file is used for initial creation of TLS certificates. This is made with cloudflare DNS plugin in mind. If different nameserver is used change accordingly.
Use --dry-run and --run-deploy-hooks for testing, see documentation.

(if DNS = cloudflare) cloudflare.ini file #

Get API token from cloudflare, then create cloudflare.ini file.
Below are template for ini file that contains API token.

dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567

How to deploy #

  1. Run config file in the project root directory.

  2. Run certbot.sh script after configuration (domain names, DNS API credentials) .

    .certificates/certbot.sh
    
  3. Copy certificate and private key path to the root .env file.

  1. Create symlink or copy of go-web.service to /etc/systemd/system.
    sudo ln -s /absolute/path/of/go-web.service /etc/systemd/system/
    
  2. Reload systemctl.
    sudo systemctl daemon-reload
    
  3. Run Go web server.
    sudo systemctl enable --now go-web
    

How to update/patch server #

  1. Fetch updates from remote repo then merge remote to local. Use -r option to rebase current change on top of origin's HEAD.
git pull
  1. Build both frontend and backend files. Use task build-frontend if only frontend assets are changed and task build-backend if only backend files are changed.
task build
  1. Web server need to be restarted for changes to be in effect.
sudo systemctl restart go-web.service