Skip to content

API v1 Package

Implements the v1 REST API handlers for GoDoxy, exposing endpoints for managing routes, Docker containers, certificates, metrics, and system configuration.

Overview

The internal/api/v1 package implements the HTTP handlers that power GoDoxy's REST API. It uses the Gin web framework and provides endpoints for route management, container operations, certificate handling, system metrics, and configuration.

Primary Consumers

  • WebUI: The homepage dashboard and admin interface consume these endpoints

Non-goals

  • Authentication and authorization logic (delegated to internal/auth)
  • Route proxying and request handling (handled by internal/route)
  • Docker container lifecycle management (delegated to internal/docker)
  • Certificate issuance and storage (handled by internal/autocert)

Stability

This package is stable. Public API endpoints follow semantic versioning for request/response contracts. Internal implementation may change between minor versions.

Public API

Exported Types

Types are defined in goutils/apitypes:

TypePurpose
apitypes.ErrorResponseStandard error response format
apitypes.SuccessResponseStandard success response format

Handler Subpackages

PackagePurpose
routeRoute listing, details, and playground testing
dockerDocker container management and monitoring
certCertificate information and renewal
metricsSystem metrics and uptime information
homepageHomepage items and category management
fileConfiguration file read/write operations
authAuthentication and session management
agentRemote agent creation and management
proxmoxProxmox API management and monitoring

Architecture

Handler Organization

Package structure mirrors the API endpoint paths (e.g., auth/login.go handles /auth/login).

Request Flow

Configuration Surface

API listening address is configured with GODOXY_API_ADDR environment variable.

Dependency and Integration Map

Internal Dependencies

PackagePurpose
internal/route/routesRoute storage and iteration
internal/dockerDocker client management
internal/configConfiguration access
internal/metricsSystem metrics collection
internal/homepageHomepage item generation
internal/agentpoolRemote agent management
internal/authAuthentication services
internal/proxmoxProxmox API management and monitoring

External Dependencies

PackagePurpose
github.com/gin-gonic/ginHTTP routing and middleware
github.com/gorilla/websocketWebSocket support
github.com/moby/moby/clientDocker API client

Observability

Logs

Handlers log at INFO level for requests and ERROR level for failures. Logs include:

  • Request path and method
  • Response status code
  • Error details (when applicable)

Metrics

No dedicated metrics exposed by handlers. Request metrics collected by middleware.

Security Considerations

  • All endpoints (except /api/v1/version) require authentication
  • Input validation using Gin binding tags
  • Path traversal prevention in file operations
  • WebSocket connections use same auth middleware as HTTP

Failure Modes and Recovery

FailureBehavior
Docker host unreachableReturns partial results with errors logged
Certificate provider not configuredReturns 404
Invalid request bodyReturns 400 with error details
Authentication failureReturns 302 redirect to login
Agent not foundReturns 404

Usage Examples

Listing All Routes via WebSocket

go
import (
    "github.com/gorilla/websocket"
)

func watchRoutes(provider string) error {
    url := "ws://localhost:8888/api/v1/route/list"
    if provider != "" {
        url += "?provider=" + provider
    }

    conn, _, err := websocket.DefaultDialer.Dial(url, nil)
    if err != nil {
        return err
    }
    defer conn.Close()

    for {
        _, message, err := conn.ReadMessage()
        if err != nil {
            return err
        }
        // message contains JSON array of routes
        processRoutes(message)
    }
}

Getting Container Status

go
import (
    "encoding/json"
    "net/http"
)

type Container struct {
    Server string `json:"server"`
    Name   string `json:"name"`
    ID     string `json:"id"`
    Image  string `json:"image"`
}

func listContainers() ([]Container, error) {
    resp, err := http.Get("http://localhost:8888/api/v1/docker/containers")
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    var containers []Container
    if err := json.NewDecoder(resp.Body).Decode(&containers); err != nil {
        return nil, err
    }
    return containers, nil
}

Health Check

bash
curl http://localhost:8888/health

)

Released under the MIT License.