π€ SleepTrack API
SleepTrack API is a RESTful API built with Go, designed to help users track and analyze their sleep patterns. This API is part of a health tech initiative aimed at improving personal wellness through data-driven insights.
π Features
User Registration & Authentication: Secure user signup and login with JWT-based authentication.
Log Sleep Data: Easily log sleep start and end times, along with sleep quality ratings.
View Sleep Logs: Retrieve and manage past sleep logs.
Sleep Trends Analysis: Analyze sleep patterns over time to help users understand and improve their sleep quality.
π οΈ Tech Stack
Go: The powerful and efficient programming language for building scalable backends.
Gin: A lightweight and fast HTTP framework for building web applications.
Sqlc: Type-safe code generator from SQL
PostgreSQL: A robust and reliable relational database for storing user and sleep data.
JWT: For secure user authentication and authorization.
π¦ Project Structure
βββ cmd/ # Entry point of the application
βββ internal/ # Core application logic
β βββ api/ # HTTP handlers and routing
β βββ config/ # Loads configuration and contains strong type representation of env variables
β βββ database/ # Database access and queries
β βββ gapi/ # GRPC api definition
β βββ logging/ # Initialize Zap logger
β βββ pagination/ # Pagination request models
β βββ pb # Implementation of GRPC service
β βββ proto/ # Definition of GRPC service
β βββ token/ # JWT authentication creation and validation
βββ util/ # Utility classes/functions used across modules
βββ README.md # Project documentation
π Getting Started
Prerequisites
- Go (version 1.19+)
- Docker
Development tools (optional):
- Protobuf compiler (brew install protobuf)
- Cobra CLI generator (go install github.com/spf13/cobra/cobra)
- Protoc-gen-go (go install github.com/golang/protobuf/protoc-gen-go)
- Protoc-gen-go-grpc (go install google.golang.org/grpc/cmd/protoc-gen-go-grpc)
- Swag-go (go install github.com/swaggo/swag/cmd/swag)
Installation
- Clone the repository:
git clone https://github.com/sk0gen/sleep-tracking-api.git
cd sleep-tracking-api
- Install dependencies:
go mod tidy
- Create a
.env
file with environment variables:
mv .env.example .env
- Set up the PostgreSQL database:
make docker-up
- Update database schema
make migration_up
- Start the server:
make run
Usage
The SleepTrack API can be accessed and tested through Swagger UI. Once the server is running, you can explore and interact with the API endpoints at (If the example configuration is used)
http://localhost:8080/swagger/index.html
This Swagger interface provides a user-friendly way to view all available endpoints, their parameters, and even test the API directly from your browser.
To-Do List for Sleep Tracker API Project
Backend Development
- GRPC - Implemented simple GRPC server with LoginUser and GetUserSleepLogs methods
- Zap logger
- Graceful shutdown
- API integration tests - Half done. No full coverage in Sleep-logs api
- Swagger
- Tracing/Observability/Metrics
- Export sleep data to file?
- Extract sleep data - Cursor query for GRPC
- Sleep analysis - Patterns/Sleep time per week calculation etc...