A robust weather subscription service built with Go that allows users to subscribe to weather updates for their favorite cities. Users receive email notifications with current weather information at their chosen frequency (hourly or daily).
- Real-time Weather Data: Get current weather information for any city
- Email Subscriptions: Subscribe to weather updates via email
- Flexible Frequency: Choose between hourly or daily notifications
- Email Confirmation: Double opt-in subscription process
- Secure Unsubscribe: Easy one-click unsubscribe functionality
- Docker Support: Full containerization with Docker Compose
- PostgreSQL Database: Reliable data persistence
- Comprehensive Testing: Unit and integration tests included
- Backend: Go (Golang) with Echo framework
- Database: PostgreSQL with GORM ORM
- Email: SMTP integration (Gmail configured)
- Weather API: WeatherAPI.com integration
- Containerization: Docker & Docker Compose
- Migration: golang-migrate
- Validation: go-playground/validator
WeatherAPI/
├── cmd/
│ ├── server/
│ │ └── main.go # Application entry point
│ └── config/
│ └── config.yaml # Configuration file
├── internal/
│ ├── config/ # Configuration management
│ ├── db/ # Database connection and migrations
│ ├── email/ # Email service implementation
│ ├── http/
│ │ └── controllers/ # HTTP request handlers
│ ├── models/ # Data models
│ ├── repository/ # Data access layer
│ └── services/ # Business logic layer
├── migrations/ # Database migrations
├── tests/ # Test files
├── Dockerfile # Docker build configuration
├── docker-compose.yml # Docker Compose configuration
└── README.md # Project documentation
- Docker and Docker Compose installed
- Weather API key from WeatherAPI.com
- Gmail account with app password for SMTP
git clone https://github.com/H1vee/WeatherAPI.git
cd WeatherAPIUpdate cmd/config/config.yaml with your credentials:
server:
port: 8080
database:
url: "postgres://postgres:postgres@postgres:5432/weatherapi?sslmode=disable"
migrations_dir: "./migrations"
weather:
api_key: "your_weather_api_key_here"
email:
host: "smtp.gmail.com"
port: 587
username: "your_email@gmail.com"
password: "your_app_password"
from_email: "your_email@gmail.com"
website_url: "http://localhost:8080"# Start all services
docker compose up
# Or run in detached mode
docker compose up -d
# View logs
docker compose logs -fThe application will be available at http://localhost:8080
- GET
/api/weather?city={city_name}- Get current weather for a city
- POST
/api/subscribe- Subscribe to weather updates - GET
/api/confirm/{token}- Confirm email subscription - GET
/api/unsubscribe/{token}- Unsubscribe from updates
curl "http://localhost:8080/api/weather?city=London"Response:
{
"temperature": 15.5,
"humidity": 65,
"description": "Partly cloudy"
}curl -X POST "http://localhost:8080/api/subscribe" \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"city": "London",
"frequency": "daily"
}'Response:
{
"message": "Subscription successful. Confirmation email sent."
}-
Install Dependencies
go mod download
-
Setup Local Database
# Start only PostgreSQL docker compose up postgres -d -
Run the Application
go run cmd/server/main.go
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific test suite
go test ./tests/...# Apply migrations
migrate -path migrations -database "postgres://postgres:postgres@localhost:5432/weatherapi?sslmode=disable" up
# Rollback migrations
migrate -path migrations -database "postgres://postgres:postgres@localhost:5432/weatherapi?sslmode=disable" downmigrate create -ext sql -dir migrations -seq create_new_tableYou can override configuration values using environment variables:
DB_URL- Database connection stringWEATHER_API_KEY- Weather API keyEMAIL_USERNAME- SMTP usernameEMAIL_PASSWORD- SMTP password
- Enable 2-Factor Authentication on your Gmail account
- Generate an App Password:
- Go to Google Account settings
- Security → 2-Step Verification → App passwords
- Generate a password for "Mail"
- Use the generated password in your configuration
The application includes:
- Structured logging with request/response details
- Health check endpoints for containers
- Graceful shutdown handling
- Error recovery middleware
- Email confirmation required for subscriptions
- Secure token generation for subscriptions
- CORS middleware enabled
- SQL injection protection via GORM
- Input validation on all endpoints
# Build and start services
docker compose up --build
# Stop services
docker compose down
# View service logs
docker compose logs app
docker compose logs postgres
# Execute commands in running container
docker compose exec app sh
docker compose exec postgres psql -U postgres -d weatherapi
# Remove all containers and volumes
docker compose down -v-
Database Connection Failed
- Ensure PostgreSQL container is healthy
- Check database URL in configuration
- Verify port 5432 is not in use by another service
-
Email Not Sending
- Verify Gmail app password is correct
- Check SMTP configuration
- Ensure less secure app access is enabled (if not using app password)
-
Weather API Errors
- Verify your WeatherAPI.com API key
- Check API rate limits
- Ensure internet connectivity from container
To run with debug logging:
# Add to docker-compose.yml environment section
environment:
- LOG_LEVEL=debug- 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.
- WeatherAPI.com for weather data
- Echo Framework for HTTP handling
- GORM for database operations
- golang-migrate for database migrations