# README
Snippetbox š
Snippetbox is a web application for pasting and sharing snippets of text, inspired by tools like Pastebin and GitHub Gists. Built step-by-step using the Go programming language, this project follows the tutorial in the book Let's Go by Alex Edwards.
Features āØ
- View and create text snippets.
- User authentication and session management.
- Secure server configuration with HTTPS.
- Dynamic HTML templating and inheritance.
- Middleware for enhanced functionality.
- A MySQL database for storing snippets.
Project Structure š
The project follows a modular structure for scalability and maintainability:
.
āāā cmd
ā āāā web # Main application entry point and handlers
ā āāā context.go # Context-related logic
ā āāā handlers.go # HTTP handlers
ā āāā helpers.go # Utility functions
ā āāā main.go # Application entry point
ā āāā middleware.go # Middleware for HTTP requests
ā āāā routes.go # HTTP route configurations
ā āāā templates.go # HTML template helpers
āāā internal
ā āāā models # Database models and related logic
ā ā āāā errors.go # Custom error definitions
ā ā āāā snippets.go # Snippet model and queries
ā ā āāā users.go # User model and queries
ā āāā validator # Input validation logic
ā āāā validator.go # Validators for user input
āāā tls # TLS certificates for HTTPS
ā āāā cert.pem # Public certificate
ā āāā key.pem # Private key
āāā ui
ā āāā html # HTML templates for the application
ā ā āāā pages # Individual pages
ā ā ā āāā create.tmpl.html
ā ā ā āāā home.tmpl.html
ā ā ā āāā login.tmpl.html
ā ā ā āāā signup.tmpl.html
ā ā ā āāā view.tmpl.html
ā ā āāā partials # Reusable components
ā ā āāā base.tmpl.html
ā ā āāā nav.tmpl.html
ā āāā static # Static assets (CSS, JS, images)
ā āāā css
ā āāā img
ā āāā js
āāā efs.go # Entry point for embedded file system
āāā .gitignore # Git ignore file
Getting Started š
Prerequisites
- Go version 1.23 or higher. Download here.
- MySQL database.
Installation
-
Clone the repository:
git clone https://github.com/mujeebcodes/snippetbox.git cd snippetbox
-
Install Go dependencies:
go mod tidy
-
Set up the MySQL database:
- Create a database named
snippetbox
. - Run the SQL schema provided in
internal/models/schema.sql
.
- Create a database named
-
Start the application:
go run ./cmd/web
-
Access the application at http://localhost:4000.
Configuration
Environment variables:
DSN
: Data Source Name for connecting to the MySQL database (e.g.,user:password@/snippetbox
).
Development š ļø
Running Tests
Unit and integration tests can be run using:
go test ./...
Directory for Templates
HTML templates are located in the ui/html
directory, and you can customize them to change the appearance of the application.
Contributions š¤
Contributions are welcome! Feel free to open an issue or submit a pull request.
Acknowledgments š
This project is built as a part of the tutorial in the book Let's Go by Alex Edwards.
License š
This project is open-source and available under the MIT License.