Skip to content

corbinlazarone/snippetbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snippet Box

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.

Prerequisites

  • Go (1.21 or higher)
  • PostgreSQL (15 or higher)
  • Docker and Docker Compose
  • A terminal or command-line interface

Run Locally

  1. Install PostgreSQL: Download from postgresql.org/download (version 15+ recommended).

  2. Set Up Environment: Copy .env.example to .env in the project root and add your PostgreSQL password:

   DB_PASSWORD=your_secure_password
  1. Clone the Repository:
   git clone <repository-url>
   cd snippetbox
  1. Start the Database: Run the following to create the snippetbox database with tables and sample data:
   docker compose up -d
  1. Run the Application: Start the server, replacing YOUR_PASSWORD with your PostgreSQL password:
   go run ./cmd/web -db=postgresql://postgres:YOUR_PASSWORD@localhost:5432/snippetbox

The app runs on http://localhost:4000.

  1. Optional: Change Port: Use the -port flag to change the default port:
   go run ./cmd/web -port=:3000 -db=postgresql://postgres:YOUR_PASSWORD@localhost:5432/snippetbox
  1. 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/snippetbox

Accessing the Application

Open http://localhost:4000 (or your custom port) in your browser.

Stopping the Application

  • Stop the server: Press Ctrl+C in the terminal.
  • Stop the database: Run docker compose down.

Troubleshooting

  • Database connection error: Verify PostgreSQL is running, the password matches .env, and the snippetbox database 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.

Security Note

Avoid using the postgres user in production. Use a dedicated user with limited permissions, as shown in step 7.

Demo Video

snippet-box-preview.mp4

About

A web app for users to create, store and share snippets of text.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors