Skip to content

TheControlApp/server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

80 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ControlMe Go Backend

A modern, secure, and scalable rewrite of the ControlMe platform in Go, providing a clean modern API for applications.

πŸš€ Features

  • βœ… Modern API: RESTful API with JWT authentication
  • βœ… RFC 7807 Error Handling: Standardized, developer-friendly error responses
  • βœ… Real-time Communication: WebSocket support for instant messaging
  • βœ… Secure: Modern authentication, bcrypt password hashing, HTTPS support
  • βœ… Interactive Documentation: Swagger/OpenAPI documentation
  • βœ… Scalable: Docker-based deployment
  • βœ… Cross-platform: Runs on Linux, macOS, and Windows
  • βœ… Well-tested: Comprehensive test coverage

πŸ”₯ New: RFC 7807 Compliant Error Handling

The API now provides structured, consistent error responses following the RFC 7807 Problem Details standard:

{
  "type": "validation_error",
  "title": "Validation Failed",
  "status": 422,
  "detail": "One or more fields failed validation",
  "help": "Please check the field requirements in the API documentation",
  "errors": [
    {
      "field": "username", 
      "message": "Username must be at least 3 characters long",
      "code": "MIN_LENGTH"
    }
  ]
}

Benefits for client developers:

  • 🎯 Consistent structure across all error responses
  • πŸ› οΈ Machine-readable error types and codes
  • πŸ“ Detailed validation errors with field-specific information
  • πŸ”— Actionable guidance for resolving issues
  • πŸ“– Complete documentation with examples

πŸ“ Project Structure

controlme-go/
β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ server/              # Main application entry point
β”‚   └── tools/               # Development and maintenance tools
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ handlers/        # HTTP request handlers
β”‚   β”‚   β”œβ”€β”€ responses/       # RFC 7807 response types
β”‚   β”‚   └── routes/          # Route definitions
β”‚   β”œβ”€β”€ auth/                # Authentication logic
β”‚   β”œβ”€β”€ config/              # Configuration management
β”‚   β”œβ”€β”€ database/            # Database connection and setup
β”‚   β”œβ”€β”€ middleware/          # HTTP middleware
β”‚   β”œβ”€β”€ models/              # Data models (GORM)
β”‚   β”œβ”€β”€ services/            # Business logic layer
β”‚   └── websocket/           # WebSocket hub and handlers
β”œβ”€β”€ configs/                 # Configuration files
β”œβ”€β”€ scripts/                 # Development and deployment scripts
β”œβ”€β”€ docker/                  # Docker configuration
└── docs/                    # Documentation

πŸ› οΈ Quick Start

Prerequisites

  • Go 1.21+
  • Docker & Docker Compose
  • Make (optional, but recommended)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd controlme-go
  2. Set up the development environment

    make setup
  3. Start the development server

    make dev

The server will be available at http://localhost:3080

Manual Setup (without Make)

  1. Start Docker services

    ./scripts/docker.sh up
  2. Install dependencies

    go mod tidy
  3. Build and run

    go build -o bin/server cmd/server/main.go
    ./bin/server

πŸ”§ Development

Available Commands

make help          # Show all available commands
make setup         # Set up development environment
make dev           # Start development server with hot reload
make build         # Build the server binary
make test          # Run all tests
make lint          # Run code linter
make fmt           # Format code
make clean         # Clean build artifacts
make docker-up     # Start Docker services (includes Swagger UI)
make docker-down   # Stop Docker services
make seed          # Run database seed data
make swagger       # Generate Swagger documentation
make swagger-serve # Generate docs and start server

Docker Services

The Docker Compose setup includes:

  • PostgreSQL Database (port 5432)
  • Go Server with hot reload (port 3080)
  • Swagger UI for API documentation (port 3080)
  • Nginx reverse proxy for production (port 80/443)

Access Points

Configuration

Copy the example configuration and modify as needed:

cp configs/config.example.yaml configs/config.yaml

Key configuration options:

  • Database connection settings
  • Server port and host
  • JWT secret keys
  • CORS settings
  • Log levels

Testing

Run the full test suite:

make test

Test specific packages:

go test ./internal/auth/...
go test ./internal/api/handlers/...

🌐 API Documentation

Modern API (v1)

Base URL: http://localhost:3080/api/v1

All endpoints return RFC 7807 compliant error responses with structured error details and optional developer help. See docs/ERROR_RESPONSE_REFERENCE.md for complete error handling documentation.

Authentication

  • POST /auth/login - User authentication
  • POST /auth/register - User registration

Commands

  • GET /commands/pending - Get pending commands for user
  • POST /commands/complete - Mark command as completed

Users

  • GET /users - Get all users
  • GET /users/{id} - Get user by ID

System

  • GET /health - Server health check

WebSocket

  • WS /ws/client - Universal WebSocket connection (all clients)

🎯 Building Custom Clients

ControlApp is designed to support rich 3rd party clients! Whether you're building a desktop app, mobile client, web interface, or IoT integration, we provide comprehensive resources:

πŸ“– Client Development Resources

πŸ› οΈ Reference Implementations

  • JavaScript/Web - Full-featured browser client with all standard commands
  • Python - Async client library perfect for automation and scripting
  • Go - High-performance client for system integration
  • Examples - Platform-specific implementations (React Native, Flutter, Desktop)

⚑ Quick Start for Client Developers

// Minimal client in just a few lines
const client = new WebSocket('ws://localhost:3080/ws/client?token=your-jwt');
client.onmessage = (event) => {
    const message = JSON.parse(event.data);
    if (message.type === 'command') {
        executeCommand(message.payload);
    }
};

πŸ“š Complete Documentation

For comprehensive documentation including API references, implementation guides, and examples:

πŸ—οΈ Architecture

Technology Stack

  • Language: Go 1.21+
  • Web Framework: Gin
  • Database: PostgreSQL with GORM ORM
  • Authentication: JWT with bcrypt password hashing
  • Real-time: WebSocket with message hub
  • Deployment: Docker Compose

Key Components

  1. Modern API Layer: RESTful API with proper HTTP methods and status codes
  2. Authentication Service: JWT-based authentication with bcrypt password hashing
  3. WebSocket Hub: Real-time message broadcasting and client management
  4. Command Service: Business logic for command creation, assignment, and completion
  5. User Service: User management, authentication, and profile handling

🚒 Deployment

Docker Deployment

  1. Production deployment

    docker-compose -f docker-compose.prod.yml up -d
  2. Environment variables

    export DB_HOST=your-db-host
    export DB_PASSWORD=your-secure-password
    export JWT_SECRET=your-jwt-secret

Manual Deployment

  1. Build for production

    CGO_ENABLED=0 GOOS=linux go build -o controlme-server cmd/server/main.go
  2. Run with environment configuration

    export ENVIRONMENT=production
    ./controlme-server

πŸ§ͺ Testing

Unit Tests

go test ./internal/...

Integration Tests

go test -tags=integration ./...

Load Testing

# TODO: Add load testing instructions

�️ Development Tools

The cmd/tools/ directory contains helpful development and testing utilities:

Integration Test Tool

Comprehensive API validation tool that tests the server against its documentation:

# Run the integration test
go run cmd/tools/integration-test/main.go

What it tests:

  • βœ… REST API endpoints (/auth/register, /auth/login)
  • βœ… JWT token generation and validation
  • βœ… WebSocket connections and authentication
  • βœ… RFC 7807 error response format
  • βœ… All documented authentication methods

Use cases:

  • πŸ” Validate API changes don't break existing functionality
  • πŸ“š Ensure documentation stays accurate with implementation
  • πŸš€ CI/CD pipeline integration for automated testing
  • πŸ› Debug authentication and WebSocket issues

The tool creates test users (test1, test2) and runs a full integration test suite, providing detailed output and appropriate exit codes for automated environments.

Other Tools

  • test-client/ - Interactive WebSocket client for manual testing
  • test-websocket-auth/ - WebSocket authentication testing utility

See individual tool README files for detailed usage instructions.

οΏ½πŸ“Š Monitoring & Logging

Health Check

curl http://localhost:3080/health

Metrics

  • Application metrics available at /metrics (when enabled)
  • Docker container metrics via docker stats

Logging

  • Structured JSON logging via logrus
  • Log levels: debug, info, warn, error
  • Configurable log output (stdout, file)

πŸ”’ Security

  • Password Security: bcrypt hashing with salt
  • JWT Authentication: Secure token-based authentication
  • HTTPS Support: TLS/SSL configuration available
  • CORS: Configurable cross-origin resource sharing
  • Rate Limiting: Built-in request rate limiting
  • Input Validation: Comprehensive input sanitization

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests (make test)
  5. Run linter (make lint)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Code Style

  • Follow standard Go conventions
  • Use gofmt for formatting
  • Write comprehensive tests
  • Document public APIs
  • Follow semantic commit messages

πŸ“ License

This project is for educational and research purposes only.

πŸ†˜ Support

  • Documentation: Check this README and inline code documentation
  • Issues: Open an issue on GitHub

πŸ—ΊοΈ Roadmap

βœ… Phase 1: Modern Authentication (Complete)

  • JWT-based authentication
  • Bcrypt password hashing
  • RESTful API design
  • WebSocket communication

πŸ”„ Phase 2: Enhanced Features (In Progress)

  • Enhanced security features
  • Improved error handling
  • Comprehensive testing
  • Performance optimization

πŸ“‹ Phase 3: Advanced Features (Planned)

  • Microservices architecture
  • Advanced monitoring
  • Load balancing
  • Multi-tenant support
  • API versioning strategy

Last Updated: July 2025
Version: 1.0.0
Go Version: 1.21+

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors