Skip to content

Gin starting point with sqlc, htmx, auth and bits. Just playing around.

Notifications You must be signed in to change notification settings

AccentDesign/gin-boilerplate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gin Boilerplate

  • Manage the database schema and apply migrations in db/migrations.
  • Manage the queries in db/sqlc.
  • Generated query directory db/dbx.
  • Manage the templates in ui.

Tooling

for migrations:

go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest

for sqlc:

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

for template rendering:

go install github.com/a-h/templ/cmd/templ@latest

for air reloading:

go install github.com/air-verse/air@latest

Config

For configuration see the config.toml passed in as the --config flag to app.

Tip

Generate new session auth keys ask ChatGPT

"Could you generate random hex keys of 32 bytes and 16 bytes for me?"

SQL Helpers

make sure to use the correct db dsn in sqlc.yml and that the db is fully migrated.

sqlc generate

Migrations

new, requires golang-migrate cli to be installed:

migrate create -ext sql -dir db/migrations \
-seq <do_something>

up:

go run . migrate --config config.dev.toml up

down:

go run . migrate --config config.dev.toml down

Templates

Generate template code with templ.guide

templ generate

Usage

go run . help

run the server:

go run . server --config config.dev.toml

create a user:

go run . createuser --config config.dev.toml \
--email admin@example.com \
--password password \
--firstname Admin \
--lastname User

Docker

This can be used to build the app as a binary and run it in a container.

build:

docker build --build-arg EXPOSE_PORT=80 -t gin-boilerplate:latest .

run:

docker run \
--rm \
--name gin-boilerplate \
--publish "80:80" \
--env "DATABASE_HOST=host.docker.internal" \
--env "SERVER_MODE=release" \
--env "SERVER_PORT=80" \
--log-driver=fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt tag=docker.gin.boilerplate \
gin-boilerplate:latest \
server --config config.toml

Postgres Container

create the container:

docker run \
--detach \
--name "gin-postgres" \
--mount type=tmpfs,destination=/var/lib/postgresql/data \
--publish "5432:5432" \
--env POSTGRES_USER=postgres \
--env POSTGRES_PASSWORD=password \
--env POSTGRES_DB=gin-boilerplate \
postgres:latest

Load Testing

Just a basic test with wrk, no real load testing was done. Logging was disabled for the test.

logged in with real user and session cookie, postgres db on localhost:

wrk -c 400 -t 10 -d 30s -H "Cookie: session=..." http://localhost
Running 30s test @ http://localhost
    10 threads and 400 connections
    Thread Stats   Avg      Stdev     Max   +/- Stdev
      Latency    20.20ms    2.00ms  71.96ms   88.29%
      Req/Sec     1.99k   144.93     2.24k    85.63%
    593341 requests in 30.02s, 796.16MB read
    Socket errors: connect 0, read 402, write 0, timeout 0
Requests/sec:  19763.59
Transfer/sec:     26.52MB

not logged in:

wrk -c 400 -t 10 -d 30s http://localhost/auth/login
Running 30s test @ http://localhost/auth/login
    10 threads and 400 connections
    Thread Stats   Avg      Stdev     Max   +/- Stdev
      Latency     5.40ms    3.32ms  86.58ms   84.56%
      Req/Sec     7.68k   645.82    16.49k    89.63%
    2294661 requests in 30.04s, 4.52GB read
    Socket errors: connect 0, read 406, write 0, timeout 0
Requests/sec:  76392.72
Transfer/sec:    154.23MB

About

Gin starting point with sqlc, htmx, auth and bits. Just playing around.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 87.4%
  • templ 7.2%
  • PLpgSQL 2.5%
  • CSS 1.3%
  • Other 1.6%