Skip to content

Commit

Permalink
Cleanup v1 (#8)
Browse files Browse the repository at this point in the history
* renaming everything to go-rest-api

* serve docs

* adding favicons and docs

* adding favicons and docs

* improving docs

* fixing logger

* improving docs

* adding unit tests

* adding unit tests

* adding more documentation and cache to the token middleware

---------

Co-authored-by: Nico Bistolfi <[email protected]>
  • Loading branch information
nicobistolfi and nicobistolfi authored Sep 3, 2024
1 parent 7b25abc commit 73a8322
Show file tree
Hide file tree
Showing 57 changed files with 1,738 additions and 555 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TOKEN CONFIGURATION
TOKEN_URL=http://localhost:8080/api/v1/token
TOKEN_CACHE_EXPIRY=5m

# JWT Configuration
JWT_SECRET=your_jwt_secret_key
Expand Down
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ docker/run:
help:
@grep -h -E '^[a-zA-Z_/-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

## test-unit: Run unit tests
## test-unit: Run unit tests in cmd, internal, and pkg folders
test-unit:
go test ./tests/unit
@echo " > Running unit tests..."
go test ./internal/... ./pkg/...

## test-integration: Run integration tests
test-integration:
Expand All @@ -89,4 +90,13 @@ test-contract:
## test-all: Run all tests
test-all: test-unit test-integration test-performance test-security test-e2e

.PHONY: build run clean test test/coverage dep lint docker/build docker/run help test-unit test-integration test-performance test-security test-e2e test-all
## docs: Run the documentation server locally
docs:
@echo " > Starting documentation server..."
@cd docs && npm i && npm start -- --port 3001

example-auth:
@echo " > Running example auth..."
@cd examples/auth && npm i && npm run dev

.PHONY: build run clean test test/coverage dep lint docker/build docker/run help test-unit test-integration test-performance test-security test-e2e test-all docs example-auth
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Go API Boilerplate
# Go REST API Boilerplate

This repository provides a structured Go project for building scalable APIs. It emphasizes clean architecture, separation of concerns, and ease of testing and deployment.

## Project Structure

```
go-boilerplate/
go-rest-api/
├── cmd/
│ └── api/
│ └── main.go
Expand Down
16 changes: 6 additions & 10 deletions deployments/serverlesss/handlers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"log"

"go-boilerplate/internal/api"
"go-boilerplate/internal/config"
"go-rest-api/internal/api"
"go-rest-api/internal/config"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/gin-gonic/gin"
"go.uber.org/zap"

logger "go-rest-api/pkg"
)

var ginLambda *ginadapter.GinLambda
Expand All @@ -26,18 +27,13 @@ func init() {
log.Fatalf("Failed to load configuration: %v", err)
}

// Initialize logger
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("Failed to initialize logger: %v", err)
}
defer logger.Sync()
logger.Init()

// Create a new Gin router
r := gin.New()

// Setup router with middleware and routes
api.SetupRouter(r, cfg, logger)
api.SetupRouter(r, cfg, logger.Log)

ginLambda = ginadapter.New(r)
}
Expand Down
2 changes: 1 addition & 1 deletion deployments/serverlesss/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
service: go-boilerplate
service: go-rest-api
frameworkVersion: ^3.0.0

plugins:
Expand Down
1 change: 1 addition & 0 deletions docs/docs/auth/_category_.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
label: Authentication
position: 4
24 changes: 24 additions & 0 deletions docs/docs/auth/auth-middleware-md.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Auth Middleware

The Auth Middleware, defined in `auth.go`, is responsible for extracting authentication tokens from incoming requests. It supports multiple authentication methods to provide flexibility in how clients can authenticate.

## Key Features

- Checks multiple sources for authentication tokens
- Supports different authentication methods
- Sets extracted token information in the request context

## Token Extraction Process

1. Check the `Authorization` header
2. If not found, check the `X-API-Key` header
3. If still not found, check the `access_token` query parameter

## Usage in Routes

```go
protected := r.Group("/api/v1")
protected.Use(middleware.AuthMiddleware())
```

This middleware sets the stage for token validation, which is handled by the Token Middleware.
35 changes: 35 additions & 0 deletions docs/docs/auth/environment-variables-md.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Environment Variables

The authentication system uses environment variables for configuration. These variables allow for flexible deployment across different environments.

## Key Environment Variables

1. `TOKEN_URL`
- Purpose: Specifies the URL of the external authentication service used for token validation.
- Required: Yes
- Example: `https://auth.example.com/validate`

2. `TOKEN_CACHE_EXPIRY`
- Purpose: Sets the expiration time for cached tokens.
- Usage: Parsed in the `init()` function of `token.go`
- Required: No (defaults to 5 minutes if not set)
- Example: `15m` for 15 minutes, `1h` for 1 hour

## Setting Environment Variables

### In Development

Use a `.env` file in the project root:

```
TOKEN_URL=https://auth.example.com/validate
TOKEN_CACHE_EXPIRY=10m
```

### In Production

Set environment variables according to your deployment platform:

- Docker: Use the `-e` flag or `environment` section in docker-compose.yml
- Kubernetes: Use `ConfigMap` and `Secret` resources
- Cloud Platforms: Use the platform-specific method for setting environment variables
Loading

0 comments on commit 73a8322

Please sign in to comment.