A server-side rendered web application built in Go, following Let's Go by Alex Edwards. Snippet Box allows users to create, store, and share snippets of text with a clean, user-friendly interface.
- Go (1.21 or higher)
- PostgreSQL (15 or higher)
- Docker and Docker Compose
- A terminal or command-line interface
-
Install PostgreSQL: Download from postgresql.org/download (version 15+ recommended).
-
Set Up Environment: Copy
.env.exampleto.envin the project root and add your PostgreSQL password:
DB_PASSWORD=your_secure_password
- Clone the Repository:
git clone <repository-url>
cd snippetbox- Start the Database: Run the following to create the
snippetboxdatabase with tables and sample data:
docker compose up -d- Run the Application: Start the server, replacing
YOUR_PASSWORDwith your PostgreSQL password:
go run ./cmd/web -db=postgresql://postgres:YOUR_PASSWORD@localhost:5432/snippetboxThe app runs on http://localhost:4000.
- Optional: Change Port: Use the
-portflag to change the default port:
go run ./cmd/web -port=:3000 -db=postgresql://postgres:YOUR_PASSWORD@localhost:5432/snippetbox- Optional: Secure Database User: Create a dedicated user for
snippetbox:
CREATE USER snippetbox_user WITH PASSWORD 'secure_password';
GRANT CONNECT ON DATABASE snippetbox TO snippetbox_user;
GRANT USAGE ON SCHEMA public TO snippetbox_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO snippetbox_user;Update the connection string:
go run ./cmd/web -db=postgresql://snippetbox_user:secure_password@localhost:5432/snippetboxOpen http://localhost:4000 (or your custom port) in your browser.
- Stop the server: Press
Ctrl+Cin the terminal. - Stop the database: Run
docker compose down.
- Database connection error: Verify PostgreSQL is running, the password matches
.env, and thesnippetboxdatabase exists. - Port conflict: Ensure port
4000(or your custom port) is free or use a different port. - Docker issues: Confirm Docker is running and check logs with
docker compose logs.
Avoid using the postgres user in production. Use a dedicated user with limited permissions, as shown in step 7.