ThinkInk Backend Server - A Go-based backend service for ThinkInk with EEG signal processing capabilities, user management, payment integration, and machine learning services.
- User Management: Registration, authentication, profile management
- EEG Signal Processing: File upload, processing, and report generation
- Payment Integration: Stripe Checkout for subscriptions and one-time payments
- Machine Learning Integration: gRPC services for EEG signal translation and token validation
- RESTful API: Comprehensive REST API with Swagger documentation
- Database Support: PostgreSQL with GORM ORM
- Security: JWT-based authentication with token blacklisting
- Docker Support: Containerized deployment
Before running the application, ensure you have:
- Go 1.23.1 or later - Installation Guide
- PostgreSQL database - Installation Guide
- Protocol Buffers compiler (protoc) - Installation Guide
- Make - Build automation tool (usually pre-installed on Linux/macOS, Windows installation)
- Git - Version control system - Installation Guide
- Docker (optional, for containerized deployment) - Installation Guide
For development and code generation, you'll also need:
- Swag (for Swagger documentation generation) - Installed automatically via
go installduring setup - protoc-gen-go and protoc-gen-go-grpc (for Protocol Buffer Go code generation) - Installed automatically during setup
- Clone the repository
git clone https://github.com/ThinkInkTeam/thinkink-core-backend.git
cd thinkink-core-backend- Install dependencies
go mod download- Generate Protocol Buffer files
make gen-proto- Generate Swagger documentation
make gen-docs- Start PostgreSQL database
make db-start- Run the server
make run-serverOr run everything together:
make run-allThe API is documented using Swagger. When the server is running, you can access the Swagger UI at:
http://localhost:8080/swagger/index.html
This provides an interactive documentation where you can:
- Browse all available API endpoints
- See request/response models
- Try out API calls directly from the browser
Create a .env file in the root directory with the following variables:
# Database configuration
DB_HOST="localhost"
DB_USER="postgres"
DB_PASSWORD="your_db_password"
DB_NAME="postgres"
DB_PORT="5432"
DB_SSL_MODE="disable" # Use "require" in production
DB_ENABLE_LOGS="false" # Set to "true" for SQL query logging# Server ports
PORT="8080" # REST API server port
GRPC_PORT="50051" # gRPC server port
# JWT secret for authentication tokens
JWT_SECRET="your_jwt_secret_key_here"
# Application environment
APP_ENV="development" # Use "production" for production# Stripe API keys (use your actual keys)
STRIPE_SECRET_KEY="sk_test_your_key_here"
STRIPE_WEBHOOK_SECRET="whsec_your_webhook_secret_here"Note: For development, default test keys are used if these environment variables are not set.
The project includes a Makefile with useful commands:
# Generate protobuf files
make gen-proto
# Generate Swagger documentation
make gen-docs
# Database management
make db-start # Start PostgreSQL container
make db-stop # Stop PostgreSQL container
# Run application
make run-server # Run the server only
make run-all # Stop DB, start fresh DB, then run serverThinkInk Core Backend is a dual-server application that runs both REST API and gRPC services concurrently:
- User authentication and management
- File upload and processing
- Payment handling with Stripe
- Report management
- Swagger documentation at
/swagger/index.html
- Translation Service: Converts EEG signals to text using ML models
- Token Validation Service: Validates JWT tokens for ML service access
- User: User accounts with Stripe integration
- Report: EEG analysis reports with matching scales
- SingleFile: Temporary file storage before processing
- Token: JWT token blacklist management
- Translation Client: Communicates with ML services for EEG processing
- Token Validator: Validates user tokens and subscription status
proto/translation/translation.proto: EEG translation service definitionsproto/validation/validation.proto: Token validation service definitions
Processes EEG data and converts it to readable text.
Endpoint: localhost:50051
Methods:
Translate(TranslateRequest) returns (TranslateResponse)
Request Format:
message TranslateRequest {
string token = 1; // JWT authentication token
repeated EegRow eeg = 2; // 2D array: list of float32 lists
repeated float msk = 3; // 1D array: float32 mask
}Validates JWT tokens for ML service authentication.
Endpoint: localhost:50051
Methods:
ValidateMLToken(ValidateTokenRequest) returns (ValidateTokenResponse)
Request Format:
message ValidateTokenRequest {
string token = 1; // JWT token to validate
}Response Format:
message ValidateTokenResponse {
bool is_valid = 1; // Whether the token is valid
}POST /signup- User registrationPOST /signin- User loginPOST /logout- User logout (requires auth)POST /refresh-token- Refresh JWT token (requires auth)GET /check-auth- Validate current token (requires auth)POST /forgot-password- Request password resetPOST /reset-password- Reset password with tokenPOST /validate-ml-token- Validate token for ML services
GET /user/{id}- Get user profile (requires auth)PUT /user/{id}/update- Update user profile (requires auth)
POST /upload- Upload EEG signal files (requires auth)
GET /reports- Get all user reports (requires auth)GET /reports/sorted- Get reports sorted by matching scale (requires auth)POST /match- Update report matching scale (requires auth)
ThinkInk includes payment processing capabilities powered by Stripe Checkout, making it simple to accept payments with a secure, hosted payment page.
Set the following environment variables in your .env file:
# Your Stripe API key
STRIPE_SECRET_KEY="sk_test_your_key_here"
# Your webhook signing secret for verifying Stripe events
STRIPE_WEBHOOK_SECRET="whsec_your_webhook_secret_here"For development, default test keys are used if these environment variables are not set.
- Checkout Sessions: Create hosted checkout pages for both subscriptions and one-time payments
- Subscription Management: View and cancel subscription plans
- Automatic Updates: Process Stripe webhook events to keep subscription data updated
POST /payment/checkout/subscription- Create a Stripe Checkout session for subscriptionPOST /payment/checkout/one-time- Create a Stripe Checkout session for one-time payment
GET /payment/subscription- Get the active subscription detailsPOST /payment/subscription/cancel- Cancel a subscription
POST /stripe/webhook- Stripe event webhook (public endpoint)
Stripe data is directly integrated into the User model to simplify the implementation:
For testing, use Stripe's test keys and test cards:
- Test card success:
4242 4242 4242 4242 - Test card decline:
4000 0000 0000 0002
For more test cards, see Stripe's documentation.
The application includes a multi-stage Dockerfile for containerized deployment.
docker build -t thinkink-backend .# Run the container
docker run -p 8080:8080 -p 50051:50051 \
-e DB_HOST=your_db_host \
-e DB_USER=your_db_user \
-e DB_PASSWORD=your_db_password \
-e STRIPE_SECRET_KEY=your_stripe_key \
thinkink-backend- Multi-stage build: Optimized for production deployment
- Alpine-based runtime: Small image size
- Protocol buffer generation: Automatic protobuf file generation during build
- Swagger documentation: Generated during build process
├── api/ # REST API router and server setup
├── cmd/ # Application entry point
├── database/ # Database connection and management
├── docs/ # Swagger documentation (auto-generated)
├── handlers/ # HTTP request handlers
├── middleware/ # HTTP middleware (auth, CORS, etc.)
├── models/ # Database models and business logic
├── proto/ # Protocol Buffer definitions
├── proto-gen/ # Generated Protocol Buffer code
├── services/ # Business services and gRPC clients
├── uploads/ # File upload directory
├── utils/ # Utility functions
├── dockerfile # Docker configuration
├── go.mod # Go module dependencies
├── makefile # Build and development commands
└── README.md # This file
- Make changes to your code
- Regenerate protobuf files if you modify
.protofiles:make gen-proto
- Update Swagger docs if you modify API endpoints:
make gen-docs
- Restart the server:
make run-server
- Add handler function in appropriate file under
handlers/ - Add route in
api/server.go - Add Swagger annotations for documentation
- Run
make gen-docsto update documentation
- Define service in
.protofile underproto/ - Run
make gen-prototo generate Go code - Implement service in
services/directory - Register service in
cmd/main.go
- JWT Authentication: Secure token-based authentication
- Token Blacklisting: Revoked tokens are tracked in database
- CORS Support: Configurable cross-origin resource sharing
- Input Validation: Request validation using Gin binding
- Environment Variables: Sensitive data stored in environment variables
- Gin: HTTP web framework
- GORM: ORM for database operations
- JWT-Go: JSON Web Token implementation
- Stripe Go: Stripe payment processing
- gRPC: High-performance RPC framework
- Protocol Buffers: Serialization framework
- Swaggo: Swagger documentation generation
- Air (optional): Live reload for development
- Docker: Containerization platform
- 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
For support and questions:
- Create an issue in the GitHub repository
- Check the Swagger documentation at
http://localhost:8080/swagger/index.html - Review the protocol buffer definitions in the
proto/directory