| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A minimal Golang HTTP API built with the standard library, featuring API key authentication and designed for serverless deployment on AWS Lambda.
/ ├── cmd/ │ ├── api/ │ │ └── main.go # Local server entry point │ └── lambda/ │ └── main.go # AWS Lambda entry point ├── internal/ │ ├── handlers/ │ │ ├── health.go # Health check handler │ │ └── handlers_test.go # Handler tests │ ├── middleware/ │ │ ├── auth.go # Authentication middleware │ │ └── auth_test.go # Middleware tests │ └── server/ │ ├── server.go # Server setup and configuration │ └── server_test.go # Server tests ├── serverless.yml # Serverless Framework configuration ├── Taskfile.yml # Task runner configuration ├── .air.toml # Hot reload configuration ├── Dockerfile # Docker configuration ├── .gitignore # Git ignore file ├── go.mod # Go module file └── README.md # This file
git clone https://github.com/nicobistolfi/go-lambda-api.git
cd go-lambda-apigo mod download
# or using Task
task modcp .env.example .env
# Edit .env and set your API_KEY# macOS
brew install go-task/tap/go-task
# Linux
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d
# Windows (using Scoop)
scoop install taskView all available tasks:
task --list
# or simply
taskCommon operations:
# Run the server locally
task run
# Run tests
task test
# Run tests with coverage
task test-coverage
# Build the binary
task build
# Format code
task fmt
# Start development server with hot reload
task devThe application uses the following environment variables:
| Variable | Description | Default | Required |
|---|---|---|---|
| API_KEY | API key for authentication | - | Yes |
| PORT | Port to run the server on | 8080 | No |
# Run with default dev API key
task run
# Run with custom API key
API_KEY="your-secret-api-key" task run
# Run on custom port
PORT=3000 task runexport API_KEY="your-secret-api-key"
export PORT="8080" # Optional, defaults to 8080go run cmd/api/main.go# Build and run in Docker
API_KEY="your-secret-api-key" task dockerThe server will start on http://localhost:8080 (or the port specified).
Health check (no authentication required):
curl http://localhost:8080/health
# or using Task
curl http://localhost:8080/health | jq .Expected response:
{"status":"ok"}With authentication (for future authenticated endpoints):
curl -H "X-API-Key: your-secret-api-key" http://localhost:8080/some-endpoint
# or using Task with custom API key
API_KEY="your-secret-api-key" task run# Run all tests
task test
# Run tests with coverage
task test-coverage
# Run linter
task lint
# Clean build artifacts
task cleanRun all tests with coverage:
go test -v -cover ./...Run tests for a specific package:
go test -v ./internal/handlers
go test -v ./internal/middleware
go test -v ./internal/serverGenerate coverage report:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.htmlnpm install -g serverlessnpm installaws configureMake sure you have a .env file with your API_KEY set, or pass it explicitly:
# Deploy using .env file
task deploy
# Or deploy with explicit API_KEY
API_KEY="your-api-key" task deploy
# Deploy to specific stage
STAGE=production task deploy
# View logs
task logsexport API_KEY="your-production-api-key"serverless deploy --stage production --region us-west-1serverless deploy --stage dev
serverless deploy --stage staging
serverless deploy --stage productionView function logs:
serverless logs -f api --tail
# or using Task
task logsRemove the deployed service:
serverless remove --stage production
# or using Task
STAGE=production serverless removeHealth check endpoint that returns the service status.
Authentication: Not required
Response:
All endpoints (except /health) require API key authentication via the X-API-Key header.
Example:
curl -H "X-API-Key: your-api-key" https://your-api-url.com/endpointError Responses:
Install development dependencies:
go install github.com/cosmtrek/air@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latestThis installs:
Start development server with hot reload:
task devRun code checks:
# Format code
task fmt
# Run linter (if installed)
task lint
# Run default task (format, test, build)
task defaultClean build artifacts:
task cleanExample:
// In internal/server/server.go
mux.HandleFunc("/api/users", middleware.AuthMiddleware(handlers.UsersHandler))Server fails to start
Authentication failures
Deployment issues
This project is licensed under the MIT License - see the LICENSE file for details.
| Back | FazBrowse Home | New Git URL |