Skip to content

viperh/auth-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth Service

Go authentication service with JWT authentication and authorization.

Features

  • Authentication: Register and login with email/password
  • JWT tokens: HS256-signed tokens with 7-day expiry
  • Protected routes: Profile management (get/update/delete) behind JWT middleware
  • Password hashing: bcrypt with default cost
  • PostgreSQL: via GORM ORM with migration support (up/down)
  • Provider interface: database layer can be swapped by implementing the Provider interface
  • CORS middleware: handles preflight requests and sets appropriate headers
  • Rate limiting: in-memory per-IP rate limiter (100 req/min default)
  • Graceful shutdown: handles SIGINT/SIGTERM with 5s timeout
  • Statistics: in-memory request tracking with thread-safe access
  • Standardized responses: consistent APIResponse format across all endpoints
  • Docker support: multi-stage Dockerfile and docker-compose with PostgreSQL

API Endpoints

Method Path Auth Description
POST /api/v1/register No Register a new user
POST /api/v1/login No Login and get JWT
GET /api/v1/health No Service health/stats
GET /api/v1/me Yes Get current profile
PUT /api/v1/me Yes Update profile
DELETE /api/v1/me Yes Delete account

Getting Started

Prerequisites

  • Go 1.24+
  • PostgreSQL 16+

Environment Variables

Copy .env.example to .env and configure:

cp .env.example .env
Variable Description Default
DB_HOST Database host localhost
DB_PORT Database port 5432
DB_NAME Database name postgres
DB_USER Database user postgres
DB_PASS Database password postgres
DB_SSL SSL mode disable
SECRET JWT signing secret yourSecretKey
PORT Server port 3000

Run with Docker

docker-compose up --build

Run Locally

# Run migrations
go run scripts/migrate.go -action up

# Start the server
go run cmd/main.go

Run Tests

go test ./... -v

Project Structure

├── cmd/
│   └── main.go                  # Application entrypoint
├── scripts/
│   └── migrate.go               # Database migration CLI
├── internal/
│   ├── app/
│   │   └── app.go               # App bootstrap and graceful shutdown
│   ├── config/
│   │   └── config.go            # Environment-based configuration
│   ├── models/
│   │   └── user.go              # GORM data models
│   ├── provider/
│   │   ├── provider.go          # Provider interface
│   │   └── postgres.go          # PostgreSQL implementation
│   ├── statistics/
│   │   ├── statistics.go        # Thread-safe statistics tracking
│   │   └── statistics_test.go
│   └── api/
│       ├── types/
│       │   └── types.go         # Request/response DTOs
│       ├── controllers/
│       │   ├── controller.go    # Route handlers
│       │   └── controller_test.go
│       ├── middlewares/
│       │   ├── middleware.go    # JWT, CORS, rate limit
│       │   └── middleware_test.go
│       └── routes/
│           └── routes.go        # Route definitions
├── Dockerfile
├── docker-compose.yml
├── .env.example
└── .gitignore

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors