A small TODO app for practicing Go.
Feel free to follow along while I make progress. There will be plenty of mistakes.
Goals:
- Practice writing a real-world app with Go
- Practice using HTMX
You can run the app in a few different ways:
- Go CLI
- Docker
- Air – Live reload for Go apps
The quickest way to get the app up and running is to run it with Go and the
-localdb flag.
go run cmd/web/main.go -localdbRead more about all the different ways to run the app below.
By default, the app will try to connect to a Postgres database. The app can also be run with an in-memory database which is useful when developing locally and testing.
-localdb: This flag will tell the app to use a local, in-memory database
instead of Postgres.
| Variable | Default Value | Usage |
|---|---|---|
TODO_DB_NAME |
todo |
Postgres database name |
TODO_DB_PORT |
5432 |
Postgres database port |
TODO_DB_USER |
todouser |
Postgres database user |
TODO_DB_PW |
todopassword |
Postgres database password |
TODO_WEB_PORT |
8080 |
Web app port |
The most direct way to run the app is with Go; however, if you plan on using a Postgres database, you’ll need to make sure you have one up and running.
go run cmd/web/main.goWith the in-memory database:
go run cmd/web/main.go -localdbOn a different port:
TODO_WEB_PORT=8001 go run cmd/web/main.goTo avoid the hassle of setting up and running Postgres locally you can use Docker.
docker-compose upWith the in-memory database:
TODO_ARGS=-localdb docker-compose upTo use environment variables with Docker Compose create a .env file in the
project root.
Example .env file:
TODO_DB_NAME=cooltododb
TODO_DB_PORT=5433
TODO_DB_USER=raduser
TODO_DB_PW=supersecurepassword
TODO_WEB_PORT=8001Docker Compose will cache the Docker build of the app image specified in the Dockerfile. If you make some file changes, rebuild with Docker Compose to see the changes in action.
docker-compose build --no-cacheLocal development can be a lot easier if the server restarts when files are changed. This is what Air does; it’s Live reload for Go apps.
Install Air:
go install github.com/cosmtrek/air@latestRun the app with Air:
airWith the in-memory database:
air -- -localdb