An API server for a SpaceAPI endpoint.
SpaceAPI-Endpoint provides a REST API that follows the SpaceAPI v15 specification to expose real-time information about the hackerspace status.
Copy spaceapi.json.example to spaceapi.json.
The SpaceAPI configuration is stored in spaceapi.json. Key sections:
- Basic Info: Space name, logo, URL
- Location: Address, coordinates, timezone
- Contact: Email, IRC, social media
- State: Current open/closed status
- Sensors: People count, temperature, etc.
- Feeds: Blog RSS, calendar feeds
For full documentation check the SpaceAPI Schema Documentation .
-
Copy the environment template:
cp .env.example .env
-
Generate a secure API key:
openssl rand -hex 32
-
Set your API key in
.env:# Edit .env file SPACEAPI_AUTH_KEY=your_generated_key_here -
For Docker Compose: The
.envfile is automatically loaded by Docker Compose. -
For manual scripts:
export SPACEAPI_AUTH_KEY=your_generated_key_here ./scripts/update-space-status.sh open
The easiest way to deploy is using the pre-built Docker image from GitHub Container Registry:
- Copy docker-compose-prod.yml to your host.
- Create your
.envandspaceapi.jsonfiles as described in the Configuration section. docker-compose up -d
For other deployment options, check the Deployment Guide.
The complete API is documented using the OpenAPI Specification (OAS) 3.0 in openapi.yaml.
# View interactive API documentation
make openapi-uiThen visit http://localhost:8081 to browse and test the API.
You can also view and interact with the API documentation using:
- Swagger UI: Upload
openapi.yamlto Swagger Editor - Redoc: Use Redocly for beautiful API docs
- VS Code: Use the OpenAPI (Swagger) Editor extension
For detailed information about the OpenAPI specification, client generation, and more, see the OpenAPI Documentation.
Returns the complete SpaceAPI JSON response.
Example:
curl http://localhost:8089/api/spaceUpdates the space state (open/closed status). Requires API key authentication.
Headers:
X-API-Key: your_api_key_here
Content-Type: application/json
Payload:
{
"open": true,
"message": "Space is open for members",
"trigger_person": "John Doe"
}Example:
curl -X POST \
-H "X-API-Key: your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"open": true, "message": "Space is open"}' \
http://localhost:8089/api/space/stateUpdates the people count in the space. Requires API key authentication.
Headers:
X-API-Key: your_api_key_here
Content-Type: application/json
Payload:
{
"value": 5,
"location": "Main Space"
}Example:
curl -X POST \
-H "X-API-Key: your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"value": 5, "location": "Main Space"}' \
http://localhost:8089/api/space/peopleAdds an event to the space timeline. Requires API key authentication.
Headers:
X-API-Key: your_api_key_here
Content-Type: application/json
Payload:
{
"name": "John Doe",
"type": "check-in",
"extra": "Working on Arduino project"
}Example:
curl -X POST \
-H "X-API-Key: your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "type": "check-in"}' \
http://localhost:8089/api/space/eventAll POST endpoints require authentication via API key.
Check the Configuration section below.
Failed authentication attempts are rate limited to mitigate brute force attacks:
- Limit: 5 failed attempts within 15 minutes
- Block Duration: 1 hour
- Response: HTTP 429 Too Many Requests with
Retry-Afterheader - Scope: Per IP address
| Status | Description | Response |
|---|---|---|
| 401 | Missing or invalid API key | "API key required" or "Invalid API key" |
| 403 | API key format invalid | "Access forbidden" |
| 429 | Rate limited | "Too many failed authentication attempts. Please try again later." |
# Open the space
./scripts/update-space-status.sh open "Open for members" "John Doe"
# Close the space
./scripts/update-space-status.sh closed "Closed for maintenance" "Jane Smith"# Set people count
./scripts/update-people-count.sh 5 "Main Space"
# Set to zero
./scripts/update-people-count.sh 0# Build spaceapi server
make build
# Or build directly
go build -o bin/spaceapi ./cmd/spaceapi# Run the SpaceAPI server
make run
# or
go run ./cmd/spaceapiThe project includes a comprehensive test suite covering all critical components.
# Run all tests (recommended)
make test
# Run with verbose output
make test-verbose
# Run with coverage
make test-coverage
# Generate HTML coverage report
make test-coverage-htmlOr run directly with Go:
# Run all tests
go test ./internal/... ./cmd/...
# Run tests with verbose output
go test -v ./internal/... ./cmd/...
# Run tests with coverage report
go test -cover ./internal/... ./cmd/...
# Run tests for specific packages
go test ./internal/handlers ./internal/middleware
# Run tests with detailed coverage
go test -coverprofile=coverage.out ./internal/handlers ./internal/middleware
go tool cover -html=coverage.outThe CI pipeline includes comprehensive E2E tests that validate the Docker container against the OpenAPI specification using Schemathesis.
Test Coverage:
- β OpenAPI conformance testing (all endpoints)
- β Authentication and authorization
- β Request/response schema validation
- β Data persistence verification
- β Error handling
For detailed information about E2E testing, including how to run tests locally, see E2E Testing Documentation.
- API Authentication: POST endpoints now require API key authentication via
SPACEAPI_AUTH_KEYenvironment variable. - Rate Limiting: Failed authentication attempts are rate limited (5 attempts in 15 minutes = 1 hour block).
- HTTPS Required: Production deployments must use HTTPS to protect API keys in transit.
- Key Management: API keys should be rotated regularly and stored securely.
// Fetch space status
fetch('http://localhost:8089/api/space')
.then(response => response.json())
.then(data => {
const statusElement = document.getElementById('space-status');
if (data.state && data.state.open) {
statusElement.textContent = 'Space is OPEN';
statusElement.className = 'status-open';
} else {
statusElement.textContent = 'Space is CLOSED';
statusElement.className = 'status-closed';
}
});- Health Checks: Both services have health check endpoints
- Logs: Check Docker logs for issues
- API not responding: Check if the spaceapi service is running
- CORS errors: Ensure the API URL is correct
- JSON parsing errors: Validate your JSON payloads
# Check service status
docker-compose ps
# View API logs
docker-compose logs spaceapi
# Test API endpoint
curl http://localhost:8089/health
# Test SpaceAPI endpoint
curl http://localhost:8089/api/spacespaceapi-endpoint/
βββ cmd/
β βββ spaceapi/ # SpaceAPI server
βββ internal/
β βββ handlers/ # HTTP handlers
β βββ middleware/ # Auth, CORS middleware
β βββ models/ # Data models
β βββ services/ # Business logic
β βββ testutil/ # Test helpers
βββ doc/ # Documentation
β βββ OPENAPI.md # OpenAPI documentation
β βββ E2E_TESTING.md # End-to-end testing guide
β βββ DEPLOYMENT_GUIDE.md
β βββ ...
βββ scripts/ # Management scripts
βββ bin/ # Built binaries
βββ openapi.yaml # OpenAPI 3.0 specification
βββ spaceapi.json # Configuration
βββ Makefile # Build automation
