Snippet is a secure and robust web application for creating, storing, and sharing text snippets. Built with Go, it features user authentication, database persistence, and HTTPS support.
Snippet allows users to create and manage short snippets of text with customizable expiration times. This application demonstrates best practices for building web applications in Go, including:
- Clean architecture with separation of concerns
- User authentication and session management
- Secure form handling and validation
- Database integration with MySQL
- HTTPS support with TLS
- Unit and integration testing
- Create, view, and manage text snippets
- User registration and authentication
- Secure sessions with cookie management
- Customizable expiration times for snippets
- HTTPS support for secure connections
- Responsive web interface
- Go 1.19 or later
- MySQL 5.7 or later
- Make (for using the Makefile)
-
Clone the repository:
git clone https://github.com/Babatunde50/snippet.git cd snippet
-
Set up the database:
# Log into MySQL and create a database mysql -u root -p
CREATE DATABASE snippetbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'web'@'localhost' IDENTIFIED BY 'pass'; GRANT ALL PRIVILEGES ON snippetbox.* TO 'web'@'localhost';
-
Create the necessary tables (schema provided in
database.sql
). -
Generate TLS certificates for HTTPS:
mkdir -p tls cd tls # For development only - generate self-signed certificates go run /usr/local/go/src/crypto/tls/generate_cert.go --rsa-bits=2048 --host=localhost # Alternatively, for production, use proper certificates
-
Install dependencies:
make deps
The application can be configured using command-line flags:
-addr
: HTTP network address (default::4000
)-static-dir
: Path to static assets (default:./ui/static
)-dsn
: MySQL data source name (default:web:pass@/snippetbox?parseTime=true
)
Build the application:
make build
Run the application:
make run
For development:
make run-dev
Run all tests:
make test
Run tests with coverage:
make test-cover