A learning project to understand web development concepts in Go. This project was created to explore and implement various features commonly found in modern web applications.
This project covers several key concepts in Go web development:
- Building RESTful APIs
- Implementing authentication with JWT
- Working with databases (SQLite)
- Writing middleware
- Handling forms and JSON data
- Writing unit tests
- Structuring a Go application
- RESTful API endpoints
- JWT-based authentication
- SQLite database integration
- Middleware support (logging, authentication)
- Form handling
- Unit tests
- Clean architecture
- Go 1.22 or higher
- SQLite3
- Clone the repository:
git clone <your-repo-url>
cd go-web-server
- Install dependencies:
go mod tidy
go run main.go
The server will start on http://localhost:8080
GET /
- Home pageGET /about
- About pageGET /contact
- Contact formPOST /contact
- Submit contact formPOST /login
- Login to get JWT token
GET /api/messages
- Get all messagesPOST /api/messages
- Create a new message
To access protected endpoints, you need to:
- Get a token by sending a POST request to
/login
:
curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{"username": "user", "password": "pass"}'
- Use the token in subsequent requests:
curl http://localhost:8080/api/messages \
-H "Authorization: Bearer <your-token>"
Run all tests:
go test ./...
.
├── main.go # Main application file
├── main_test.go # Main package tests
├── auth/
│ ├── auth.go # Authentication package
│ └── auth_test.go # Authentication tests
└── db/
└── db.go # Database operations
- The JWT secret key is hardcoded for demonstration. In production, use environment variables.
- The login handler accepts any non-empty username/password. In production, implement proper authentication.
- SQLite is used for simplicity. For production, consider using a more robust database.
This project was inspired by and built while learning from:
- Go's official documentation
- Various web development best practices
- RESTful API design principles
- Authentication and security concepts
This is a learning project and is not intended for production use without proper security review and enhancements.
Feel free to submit issues, fork the repository, and create pull requests for any improvements. As this is a learning project, constructive feedback is highly appreciated!