2299803050355f861a6dcc5abf25ec85deb0d05d
Counter - Habit Tracking Application
A full-stack web application for tracking habits and activities with counters. Users can create counters, increment/decrement them, and view statistics. The app supports both authenticated users (with database persistence) and anonymous users (with local storage).
Features
- User Authentication: Register and login with JWT tokens
- Anonymous Usage: Work without registration using browser local storage
- Counter Management: Create, edit, delete, and manage counters
- Increment/Decrement: Track positive and negative changes
- Date-based Tracking: Counters store entries grouped by dates
- Statistics: View today, week, month, and total values
- Search & Filter: Find counters by name or description
- Responsive Design: Modern UI that works on all devices
Tech Stack
- Backend: Go with Gin framework
- Frontend: React with TypeScript
- Database: PostgreSQL
- Authentication: JWT tokens
- Deployment: Docker with multi-stage builds
Quick Start
Prerequisites
- Docker and Docker Compose
- Go 1.21+ (for local development)
- Node.js 18+ (for local development)
Using Docker Compose (Recommended)
- Clone the repository:
git clone <repository-url>
cd counter
- Start the application:
docker-compose up --build
- Open your browser and navigate to
http://localhost:8080
The application will automatically:
- Start PostgreSQL database
- Build and run the Go backend
- Build and serve the React frontend
- Create necessary database tables
Local Development
Backend Setup
- Install dependencies:
go mod download
- Start PostgreSQL (using Docker):
docker run --name counter-postgres -e POSTGRES_DB=counter_db -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:15-alpine
- Set environment variables:
export DATABASE_URL="postgres://postgres:password@localhost:5432/counter_db?sslmode=disable"
export JWT_SECRET="your-secret-key"
- Run the backend:
go run .
Frontend Setup
- Navigate to frontend directory:
cd frontend
- Install dependencies:
npm install
- Start the development server:
npm start
- Open
http://localhost:3000in your browser
API Endpoints
Authentication
POST /api/v1/auth/register- Register a new userPOST /api/v1/auth/login- Login userGET /api/v1/auth/me- Get current user (protected)
Counters (Protected)
GET /api/v1/counters- Get all countersPOST /api/v1/counters- Create a new counterGET /api/v1/counters/:id- Get specific counterPUT /api/v1/counters/:id- Update counterDELETE /api/v1/counters/:id- Delete counterPOST /api/v1/counters/:id/increment- Increment/decrement counterGET /api/v1/counters/:id/entries- Get counter entriesGET /api/v1/counters/:id/stats- Get counter statistics
Database Schema
Users Table
id(SERIAL PRIMARY KEY)username(VARCHAR(50) UNIQUE)email(VARCHAR(255) UNIQUE)password(VARCHAR(255))created_at(TIMESTAMP)updated_at(TIMESTAMP)
Counters Table
id(SERIAL PRIMARY KEY)user_id(INTEGER REFERENCES users(id))name(VARCHAR(100))description(TEXT)created_at(TIMESTAMP)updated_at(TIMESTAMP)
Counter Entries Table
id(SERIAL PRIMARY KEY)counter_id(INTEGER REFERENCES counters(id))value(INTEGER)date(DATE)created_at(TIMESTAMP)
Anonymous User Support
Anonymous users can use the application without registration:
- Counters are stored in browser's localStorage
- Data persists across browser sessions
- No server-side storage for anonymous users
- Seamless transition to authenticated user (data remains in localStorage)
Environment Variables
Create a .env file based on env.example:
# Database Configuration
DATABASE_URL=postgres://postgres:password@localhost:5432/counter_db?sslmode=disable
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key-change-in-production
# Server Configuration
PORT=8080
GIN_MODE=release
Deployment
Production Deployment
- Set environment variables for production
- Use a production PostgreSQL database
- Set a strong JWT secret
- Build and deploy using Docker:
docker build -t counter-app .
docker run -p 8080:8080 \
-e DATABASE_URL="your-production-db-url" \
-e JWT_SECRET="your-production-secret" \
counter-app
Docker Compose Production
Update docker-compose.yml with production settings:
- Use external PostgreSQL database
- Set production environment variables
- Configure proper networking and volumes
Development
Project Structure
counter/
├── main.go # Main application entry point
├── models.go # Data models and DTOs
├── database.go # Database connection and setup
├── auth.go # Authentication handlers and middleware
├── counters.go # Counter management handlers
├── docker-compose.yml # Local development setup
├── Dockerfile # Multi-stage build for production
├── go.mod # Go dependencies
├── go.sum # Go dependencies checksum
└── frontend/ # React frontend
├── src/
│ ├── components/ # React components
│ ├── hooks/ # Custom React hooks
│ ├── services/ # API and localStorage services
│ ├── types/ # TypeScript type definitions
│ └── App.tsx # Main React component
├── package.json # Node.js dependencies
└── public/ # Static assets
Adding New Features
- Backend: Add new handlers in appropriate files
- Frontend: Create components in
src/components/ - API Integration: Update services in
src/services/ - Types: Add TypeScript interfaces in
src/types/
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License.
Description
Languages
TypeScript
62.1%
Go
33.1%
CSS
1.7%
Dockerfile
1.2%
Shell
0.8%
Other
1.1%