Time Tracker Backend
This is the backend service for the Time Tracker application, built with Go, Gin, and GORM.
Database Setup
The application uses PostgreSQL as its database. The database connection is configured using GORM, a popular Go ORM library.
Configuration
Database configuration is handled through the models.DatabaseConfig
struct in internal/models/db.go
. The application uses sensible defaults that can be overridden with environment variables:
DB_HOST
: Database host (default: "localhost")DB_PORT
: Database port (default: 5432)DB_USER
: Database user (default: "timetracker")DB_PASSWORD
: Database password (default: "password")DB_NAME
: Database name (default: "timetracker")DB_SSLMODE
: SSL mode (default: "disable")
Running with Docker
The easiest way to run the database is using Docker Compose:
# Start the database
docker-compose up -d db
# Check if the database is running
docker-compose ps
Database Migrations
The application automatically migrates the database schema on startup using GORM's AutoMigrate feature. This creates all necessary tables based on the model definitions.
Initial Data Seeding
The application seeds the database with initial data if it's empty. This includes:
- A default company
- An admin user with email "admin@example.com" and password "Admin@123456"
Running the Application
Using Make Commands
The project includes a Makefile with common commands:
# Start the database
make db-start
# Test the database connection
make db-test
# Run database migrations
make migrate
# Seed the database with initial data
make seed
# Test the database models
make model-test
# Run the application
make run
# Build the application
make build
# Show all available commands
make help
Manual Commands
If you prefer not to use Make, you can run the commands directly:
# Start the database
cd /path/to/timetracker
docker-compose up -d db
# Test the database connection
cd backend
go run cmd/dbtest/main.go
# Run database migrations
cd backend
go run cmd/migrate/main.go
# Seed the database with initial data
cd backend
go run cmd/seed/main.go
# Run the backend application
cd backend
go run cmd/api/main.go
The API will be available at http://localhost:8080/api and the Swagger documentation at http://localhost:8080/swagger/index.html.
Environment Variables
You can configure the database connection using environment variables:
# Example: Connect to a different database
DB_HOST=my-postgres-server DB_PORT=5432 DB_USER=myuser DB_PASSWORD=mypassword DB_NAME=mydb go run cmd/api/main.go
Database Models
The application uses the following models:
User
: Represents a user in the systemCompany
: Represents a companyCustomer
: Represents a customerProject
: Represents a projectActivity
: Represents an activityTimeEntry
: Represents a time entry
Each model has corresponding CRUD operations and relationships defined in the internal/models
directory.
GORM Best Practices
The application follows these GORM best practices:
-
Connection Pooling: Configured with sensible defaults for maximum idle connections, maximum open connections, and connection lifetime.
-
Migrations: Uses GORM's AutoMigrate to automatically create and update database tables.
-
Transactions: Uses transactions for operations that require multiple database changes to ensure data consistency.
-
Soft Deletes: Uses GORM's soft delete feature to mark records as deleted without actually removing them from the database.
-
Relationships: Properly defines relationships between models using GORM's relationship features.
-
Error Handling: Properly handles database errors and returns appropriate error messages.
-
Context Support: Uses context for database operations to support timeouts and cancellation.
-
Logging: Configures GORM's logger for appropriate logging based on the environment.
-
Graceful Shutdown: Properly closes database connections when the application shuts down.
-
Validation: Implements validation for model fields before saving to the database.