A web application for creating and sharing code snippets built with Go and MySQL.
- Create Snippets: Users can create code snippets with titles, content, and expiration dates
- View Snippets: Browse and view individual snippets
- User Authentication: User registration, login, and logout functionality
- Session Management: Secure session handling with MySQL storage
- Flash Messages: User feedback via flash messages
- HTTPS Support: TLS encryption for secure connections
- Template Caching: Efficient template rendering with caching
- Middleware: Custom middleware for logging, panic recovery, and security headers
- Form Validation: Client and server-side form validation
- CSRF Protection: Cross-site request forgery protection
- Backend: Go 1.22
- Database: MySQL
- Frontend: HTML templates with CSS and JavaScript
- Session Store: MySQL-backed sessions
- Router: httprouter
- Middleware: Alice for middleware chaining
- Security: nosurf for CSRF protection, SCS for session management
snippetbox/
├── cmd/web/ # Web application entry point
│ ├── handlers.go # HTTP handlers
│ ├── helpers.go # Helper functions
│ ├── main.go # Application entry point
│ ├── middleware.go # Custom middleware
│ ├── routes.go # Route definitions
│ └── templates.go # Template management
├── internal/
│ ├── models/ # Data models and database logic
│ │ ├── mocks/ # Mock implementations for testing
│ │ ├── testdata/ # Test data and SQL scripts
│ │ ├── errors.go # Custom error types
│ │ ├── snippets.go # Snippet model
│ │ └── users.go # User model
│ └── validator/ # Form validation logic
├── ui/
│ ├── html/ # HTML templates
│ │ ├── base.html # Base template
│ │ ├── pages/ # Page templates
│ │ └── partials/ # Partial templates
│ ├── static/ # Static assets (CSS, JS, images)
│ └── efs.go # Embedded filesystem
├── tls/ # TLS certificates
└── go.mod # Go module definition
- Go 1.22 or higher
- MySQL 8.0 or higher
- Git
-
Clone the repository
git clone https://github.com/ASHUTOSH-SWAIN-GIT/snippetbox.git cd snippetbox -
Install dependencies
go mod tidy
-
Set up MySQL database
CREATE DATABASE snippetbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'web'@'localhost' IDENTIFIED BY 'pass'; GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web'@'localhost';
-
Create database tables
mysql -u web -p snippetbox < internal/models/testdata/setup.sql
Development (HTTP)
go run ./cmd/webProduction (HTTPS)
go run ./cmd/web -addr=":443" -dsn="web:your_password@/snippetbox?parseTime=true"The application will be available at:
- HTTP: http://localhost:4000
- HTTPS: https://localhost:4000
-addr: Network address (default: ":4000")-dsn: MySQL data source name (default: "web:pass@/snippetbox?parseTime=true")
GET /- Home page with recent snippetsGET /snippet/view/:id- View a specific snippetGET /snippet/create- Create snippet formPOST /snippet/create- Submit new snippetGET /user/signup- User registration formPOST /user/signup- Submit registrationGET /user/login- User login formPOST /user/login- Submit loginPOST /user/logout- User logoutGET /ping- Health check endpoint
Run all tests
go test ./...Run tests with verbose output
go test -v ./...Run specific package tests
go test -v ./cmd/web/
go test -v ./internal/models/The application uses MySQL with the following default settings:
- Host: localhost
- Database: snippetbox
- Username: web
- Password: pass
- Session lifetime: 12 hours
- Session store: MySQL
- Cookie settings: HttpOnly, SameSite=Lax
- CSRF protection on all state-changing requests
- Secure headers (X-Frame-Options, X-XSS-Protection, etc.)
- HTTPS redirect in production
- Session-based authentication
- Password hashing with bcrypt
- Create new handlers in
cmd/web/handlers.go - Add routes in
cmd/web/routes.go - Create templates in
ui/html/pages/ - Add models in
internal/models/ - Write tests for new functionality
To add new database changes:
- Create migration SQL files
- Update the setup.sql for new installations
- Document changes in the README
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built following the "Let's Go" book by Alex Edwards
- Uses the excellent httprouter for routing
- Session management powered by SCS
- CSRF protection via nosurf
- GitHub: @ASHUTOSH-SWAIN-GIT
- Project Link: https://github.com/ASHUTOSH-SWAIN-GIT/snippetbox