Skip to content

Backend Package

@habit-tracker/backend

Express.js REST API server for the Habit Tracker application. Manages habits, logs, and settings with SQLite persistence.

Key Files

FilePurpose
src/server.tsExpress app setup with middleware and routes
src/routes/habits.tsCRUD operations for habits and completion/skip actions
src/routes/settings.tsGlobal settings (blocked websites)
src/routes/status.tsDaemon status endpoint
src/db/schema.tsDrizzle ORM schema definitions
src/db/index.tsDatabase connection and initialization
src/middleware/validators.tsRequest validation middleware

API Endpoints

Habits

MethodEndpointDescription
GET/api/habitsList all habits
POST/api/habitsCreate a new habit
GET/api/habits/:idGet habit details
PUT/api/habits/:idUpdate habit settings
DELETE/api/habits/:idDelete a habit
POST/api/habits/:id/completeMark habit complete for today
POST/api/habits/:id/skipSkip habit with reason
GET/api/habits/:id/logsGet habit completion history
PATCH/api/habits/:id/logs/date/:dateUpdate log data value for specific date
GET/api/habits/:id/calendarGet calendar view data for habit
GET/api/habits/:id/graphGet graph data for data-tracking habits

Settings

MethodEndpointDescription
GET/api/settingsGet global settings
PUT/api/settingsUpdate settings
POST/api/settings/blocked-websitesAdd website to blocked list
DELETE/api/settings/blocked-websitesRemove website from blocked list

Status

MethodEndpointDescription
GET/api/statusGet daemon status
POST/api/daemon/syncTrigger daemon sync
POST/api/daemon/resetEmergency reset (unblock all)
GET/healthHealth check

Development

bash
# Start development server (port 3001, sandbox DB)
npm run dev -w @habit-tracker/backend

# Start production server (port 3000, persistent DB)
npm run start:prod -w @habit-tracker/backend

# Run tests
npm test -w @habit-tracker/backend

# Database migrations
npm run db:migrate -w @habit-tracker/backend
npm run db:generate -w @habit-tracker/backend

Architecture

The backend follows a standard Express pattern:

  1. Routes handle HTTP requests and responses
  2. Validators ensure request data is valid
  3. Drizzle ORM manages SQLite database operations
  4. Daemon Client notifies the daemon when blocking state changes

When a habit is completed or a deadline passes, the backend signals the daemon via IPC to update /etc/hosts.

Environment Variables

VariableDevelopmentProduction
PORT30013000
DB_PATH./data/dev/habit-tracker.db~/.habit-tracker/data/habit-tracker.db
NODE_ENVdevelopmentproduction

Built with VitePress