Snippetbox is a web application built in Go that allows users to paste, share, and view text snippets — similar to services like Pastebin or GitHub Gists. It is designed with simplicity, security, and scalability in mind, leveraging Go's performance and MySQL for data storage.
- User Authentication: Only registered users can create snippets.
- Session Management: Secure cookie-based session management.
- Snippet Management: Create, read, and manage snippets with customizable expiration dates (1 day, 1 week, or 1 year).
- MySQL Integration: All snippets and user data are stored in a MySQL database.
- Secure by Design: The application is HTTPS-ready with TLS support and follows best security practices.
- Dynamic HTML Templating: HTML templates with Go's html/template package for safe rendering of dynamic content.
- Middleware for Logging and Recovery: Includes middleware for structured logging and panic recovery to handle errors gracefully.
You can check out a live demo of Snippetbox hosted on an AWS EC2 instance: Live Demo
- Go 1.11+ (with module support)
- MySQL database
- Git
-
Clone the repository:
git clone https://github.com/curator69/snippetbox.git
-
Navigate to the project directory:
cd snippetbox
-
Install Go modules:
go mod tidy
-
Set up MySQL and create a database for Snippetbox:
CREATE DATABASE snippetbox;
-
Create a .env file in the root directory with the following content:
DB_DSN="username:password@/snippetbox"
Replace username and password with your MySQL credentials.
-
Start the application:
go run cmd/web/*
-
Access the application by visiting https://localhost:4000 in your browser.
/cmd/web # Contains the main entry point and handlers
/pkg # Application-specific packages (models, database interaction)
/ui # User interface assets (HTML templates, CSS, JS)
/ui/html # HTML templates for various pages
/ui/static # Static assets like CSS, JS, images
- Handlers: Found under /cmd/web/handlers.go, they control the flow of requests and responses.
- Templates: Located in /ui/html/, the Go templates are responsible for rendering dynamic HTML pages.
- Database Models: Defined in /pkg/models, these handle the interaction with the MySQL database.
- Sessions: Managed using the SCS package to provide secure session management.
Snippetbox is currently deployed on an AWS EC2 instance and can be accessed here: https://13.233.101.20:4000
- Set up an AWS EC2 instance with Ubuntu or Amazon Linux.
- Install Go and MySQL on the server.
- Clone the repository and set up environment variables with your MySQL DSN.
- Use a process manager like pm2 or systemd to run the Go application as a service.
- Use NGINX or Apache to reverse proxy and serve the application over HTTPS.
Contributions are welcome! Feel free to submit pull requests or open issues for bug fixes, new features, or enhancements.
This project is open-source and available under the MIT License. See the LICENSE file for more details.
- Alex Edwards for the Let's Go book, which inspired the structure of this project.