From f1779268f483965f5251ebae81f399a1f8634501 Mon Sep 17 00:00:00 2001 From: FernandoJVideira <03.pleaser-minster@icloud.com> Date: Mon, 31 Mar 2025 19:02:03 +0100 Subject: [PATCH] fix: updated readme and added envrc example --- .envrc.example | 35 ++++++++ README.md | 195 ++++++++++++++++++++++++++++++++++++++++---- cmd/api/comments.go | 2 +- 3 files changed, 215 insertions(+), 17 deletions(-) create mode 100644 .envrc.example diff --git a/.envrc.example b/.envrc.example new file mode 100644 index 0000000..6240a1d --- /dev/null +++ b/.envrc.example @@ -0,0 +1,35 @@ +# Server Configuration +export ADDR=":8080" +export EXTERNAL_URL="localhost:8080" +export FRONTEND_URL="http://localhost:5173" +export ENV="development" +export ALLOWED_ORIGINS="https://*,http://*" # TODO: Change to the actual allowed origins on production + +# Database Configuration +export DB_ADDR="postgres://postgres:postgres@localhost:5432/lk_tmpl?sslmode=disable" +export DB_MAX_OPEN_CONNS="30" +export DB_MAX_IDLE_CONNS="30" +export DB_MAX_IDLE_TIME="15m" + +# Redis Configuration +export REDIS_ADDR="localhost:6379" +export REDIS_PASSWORD="" +export REDIS_DB="0" +export REDIS_ENABLED="false" + +# Authentication +export BASIC_AUTH_USERNAME="admin" +export BASIC_AUTH_PASSWORD="admin" +export AUTH_TOKEN_SECRET="your-secret-key-here" +export AUTH_TOKEN_EXP="24h" +export AUTH_TOKEN_ISS="lkapi" +export AUTH_TOKEN_AUD="lkapi" + +# Email Configuration +export FROM_EMAIL="your-email@example.com" +export SENDGRID_API_KEY="your-sendgrid-api-key" + +# Rate Limiter +export RATE_LIMITER_REQS_COUNT="20" +export RATE_LIMITER_TIME_FRAME="1m" +export RATE_LIMITER_ENABLED="true" \ No newline at end of file diff --git a/README.md b/README.md index 5aa5422..928be93 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LK API Template -A modern, production-ready API template built with Go and React, featuring comprehensive authentication, mailing capabilities, and automatic API documentation. +A modern, production-ready API template built with Go and Nuxt 3, featuring comprehensive authentication, mailing capabilities, and automatic API documentation. ## 🚀 Features @@ -28,11 +28,17 @@ A modern, production-ready API template built with Go and React, featuring compr ## 📋 Prerequisites -- Go 1.21 or higher -- Node.js 18 or higher -- PostgreSQL -- Redis +- Go 1.23.4 or higher +- Bun 1.0 or higher (for frontend development) +- Docker and Docker Compose - SendGrid API key (for email functionality) +- Platform-specific requirements: + - **Mac/Linux**: + - direnv (for environment management) + - make + - **Windows**: + - Git Bash, WSL2, or PowerShell + - make (optional, commands can be run manually) ## 🛠️ Installation @@ -43,49 +49,206 @@ git clone https://github.com/FernandoJVideira/LK_API_Temp.git cd LK_API_Temp ``` -2. Install backend dependencies: +2. Install Bun (if not already installed): + +**Mac/Linux**: ```bash -go mod download +curl -fsSL https://bun.sh/install | bash ``` -3. Install frontend dependencies: +**Windows**: ```bash -cd web -npm install +# Install WSL2 first if not already installed +# Then run the same command as Mac/Linux inside WSL2 ``` -4. Set up environment variables: +3. Set up environment variables: + +**Mac/Linux with direnv**: + +```bash +cp .envrc.example .envrc +# Edit .envrc with your configuration +direnv allow +``` + +**Windows or alternative setup**: ```bash cp .env.example .env # Edit .env with your configuration ``` +4. Install backend dependencies: + +```bash +go mod download +``` + +5. Install frontend dependencies: + +```bash +cd web +bun install +``` + ## 🚀 Development +### Infrastructure + +**Mac/Linux**: + +```bash +# Start services +make start + +# Stop services +make stop +``` + +**Windows (without make)**: + +```bash +# Start services +docker-compose up -d + +# Stop services +docker-compose down +``` + +### Database Management + +**Mac/Linux**: + +```bash +# Create new migration +make migration name=your_migration_name + +# Run migrations +make migrate-up + +# Rollback migrations +make migrate-down + +# Seed database +make seed +``` + +**Windows (without make)**: + +```bash +# Create new migration +go run ./cmd/migrate create your_migration_name + +# Run migrations +go run ./cmd/migrate up + +# Rollback migrations +go run ./cmd/migrate down + +# Seed database +go run ./cmd/migrate seed +``` + ### Backend -Run the backend server with hot reload: +**Mac/Linux**: ```bash +# Install Air (if not already installed) +go install github.com/cosmtrek/air@latest + +# Run with hot reload air ``` +**Windows**: + +```bash +# Install Air (if not already installed) +go install github.com/cosmtrek/air@latest + +# Run with hot reload (Git Bash or WSL) +air + +# Alternative without Air +go run ./cmd/api +``` + The API will be available at `http://localhost:8080` with the base path `/v1`. ### Frontend -Run the frontend development server: +All platforms: ```bash cd web -npm run dev +bun run dev ``` -The frontend will be available at `http://localhost:3000` (default Nuxt port). +The frontend will be available at `http://localhost:3000`. + +For other frontend commands: + +```bash +# Build for production +bun run build + +# Preview production build +bun run preview + +# Lint +bun run lint + +# Type check +bun run typecheck +``` + +Note: Bun provides significantly faster installation and execution times compared to npm or yarn. It also includes built-in TypeScript support and other optimizations for modern web development. + +## 🔧 Environment Configuration + +### Using direnv (Mac/Linux recommended) + +The `.envrc` file is used with direnv to automatically load environment variables when entering the project directory. Example configuration: + +```bash +# Server +export ADDR=:8080 +export EXTERNAL_URL=localhost:8080 +export FRONTEND_URL=http://localhost:3000 +export ENV=development + +# Database +export DB_ADDR=postgres://postgres:postgres@localhost:5432/lk_tmpl?sslmode=disable +export DB_MAX_OPEN_CONNS=30 +export DB_MAX_IDLE_CONNS=30 +export DB_MAX_IDLE_TIME=15m + +# Additional variables... +``` + +### Using .env file (Windows or alternative) + +The same variables should be set in your `.env` file without the `export` keyword: + +```env +ADDR=:8080 +EXTERNAL_URL=localhost:8080 +FRONTEND_URL=http://localhost:3000 +ENV=development +# Additional variables... +``` + +## 🔍 Monitoring + +- Redis Commander UI: `http://localhost:8081` + - Mac/Linux: Access directly + - Windows: If using WSL2, use the WSL2 IP address instead of localhost +- Metrics and debugging endpoints available through expvar ## 📚 API Documentation -The API documentation is automatically generated using Swagger. Once the server is running, you can access the Swagger UI at: `http://localhost:8080/v1/swagger/index.html#/` \ No newline at end of file +The API documentation is automatically generated using Swagger. Once the server is running, you can access the Swagger UI at: `http://localhost:8080/v1/swagger/index.html#/` diff --git a/cmd/api/comments.go b/cmd/api/comments.go index bed2e97..4ddc2e6 100644 --- a/cmd/api/comments.go +++ b/cmd/api/comments.go @@ -11,7 +11,7 @@ type CreateCommentPayload struct { Content string `json:"content" validate:"required, max=1000"` } -// CreateComment godo +// CreateComment godoc // // @Summary Creates a new Comment // @Description Creates a new Comment