Skip to content

senonide/gopirest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GopiRest API

Go Version Docker

A modern REST API built with Go and Gin that connects to MongoDB, following Go best practices and clean architecture principles.

πŸ“‹ API Endpoints

Posts

  • POST /api/v1/posts - Create a new post
  • GET /api/v1/posts - Get all posts (with pagination)
  • GET /api/v1/posts?published=true - Get only published posts
  • GET /api/v1/posts/:id - Get a specific post
  • PUT /api/v1/posts/:id - Update a post
  • DELETE /api/v1/posts/:id - Delete a post

Authors

  • GET /api/v1/authors/:author/posts - Get posts by author

System

  • GET /health - Health check endpoint

πŸ—οΈ Project Structure

gopirest/
β”œβ”€β”€ cmd/
β”‚   └── server/
β”‚       └── main.go              # Application entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ config/                  # Configuration management
β”‚   β”‚   β”œβ”€β”€ config.go           # Config struct and loading logic
β”‚   β”‚   β”œβ”€β”€ default.json        # Default embedded configuration
β”‚   β”‚   └── local.json          # Local development configuration
β”‚   β”œβ”€β”€ database/               # Database connection management
β”‚   β”‚   β”œβ”€β”€ mongodb.go          # MongoDB connection and utilities
β”‚   β”‚   └── mongodb_test.go     # Database tests
β”‚   β”œβ”€β”€ handler/                # HTTP handlers (controllers)
β”‚   β”‚   └── post.go             # Post-related HTTP handlers
β”‚   β”œβ”€β”€ model/                  # Data models and DTOs
β”‚   β”‚   └── post.go             # Post model and request/response types
β”‚   β”œβ”€β”€ repository/             # Data access layer
β”‚   β”‚   └── post.go             # Post repository interface and implementation
β”‚   └── service/                # Business logic layer
β”‚       └── post.go             # Post service implementation
β”œβ”€β”€ examples/                   # Usage examples
β”‚   └── config_example.go       # Configuration system example
β”œβ”€β”€ scripts/                    # Utility scripts
β”‚   └── test_mongodb.sh         # MongoDB integration test script
β”œβ”€β”€ docker-compose.yml          # Local development setup
β”œβ”€β”€ Dockerfile                  # Production container
β”œβ”€β”€ Makefile                   # Development commands
└── README.md                  # This file

πŸ› οΈ Quick Start

Prerequisites

  • Go 1.24.3 or later
  • Docker and Docker Compose
  • Make (optional, for using Makefile commands)

Local Development Setup

  1. Clone the repository:

    git clone https://github.com/senonide/gopirest.git
    cd gopirest
  2. Start MongoDB and services:

    make dev-start
    # Or manually: docker compose up -d
  3. Run the application:

    make run
    # Or manually: go run cmd/server/main.go

The API will be available at http://localhost:8080

Using Docker Compose (Full Stack)

# Start all services (API + MongoDB + Mongo Express)
docker compose up -d

# View logs
docker compose logs -f api

# Stop all services
docker compose down

βš™οΈ Configuration

The application uses an embedded configuration system that supports multiple environments:

Embedded Configuration

  • Default: Production-ready configuration embedded in the binary
  • Local: Development configuration with debug mode enabled
  • External: Override with external config.json file

Environment Variables

  • GO_ENV: Set to local or development for development configuration

Configuration Structure

{
    "server": {
        "port": 8080,
        "host": "localhost",
        "mode": "release"
    },
    "database": {
        "name": "gopirest",
        "user": "gopirest_user",
        "password": "gopirest_password",
        "host": "localhost:27017",
        "timeout": 10
    },
    "logging": {
        "level": "info",
        "format": "json"
    }
}

🐳 Database Access

When using Docker Compose:

  • MongoDB: mongodb://gopirest_user:gopirest_password@localhost:27017/gopirest
  • Admin Access: mongodb://admin:password123@localhost:27017/
  • Web UI (Mongo Express): http://localhost:8081

πŸ“¦ Building and Deployment

Development Build

make build
# Creates: build/gopirest

Docker Build

make docker-build
# Creates: gopirest:latest

Production Deployment

# Build optimized binary
go build -ldflags="-s -w" -o gopirest ./cmd/server/main.go

# Run with production config
./gopirest

πŸ§ͺ Testing

Run All Tests

make test
# Or: go test ./...

Integration Tests

# Test MongoDB setup and API functionality
./scripts/test_mongodb.sh

Manual API Testing

# Create a post
curl -X POST http://localhost:8080/api/v1/posts \
  -H "Content-Type: application/json" \
  -d '{"title":"Hello World","content":"My first post","author":"John Doe","published":true}'

# Get all posts
curl http://localhost:8080/api/v1/posts

# Get a specific post (replace ID)
curl http://localhost:8080/api/v1/posts/507f1f77bcf86cd799439011

# Health check
curl http://localhost:8080/health

πŸ” Development Commands

The project includes a comprehensive Makefile:

make help          # Show all available commands
make dev-setup     # Setup development environment
make dev-start     # Start MongoDB with Docker Compose
make dev-stop      # Stop development services
make run           # Run the application
make build         # Build production binary
make test          # Run tests
make lint          # Run linter
make clean         # Clean build artifacts

πŸ“Š API Examples

Create Post

curl -X POST http://localhost:8080/api/v1/posts \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Getting Started with GopiRest",
    "content": "This is a comprehensive guide to using the GopiRest API...",
    "author": "Jane Developer",
    "tags": ["go", "api", "mongodb"],
    "published": true
  }'

Get Posts with Pagination

curl "http://localhost:8080/api/v1/posts?page=1&limit=10&published=true"

Update Post

curl -X PUT http://localhost:8080/api/v1/posts/507f1f77bcf86cd799439011 \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Updated Title",
    "published": true
  }'

πŸ“ License

See the LICENSE file for details.

Debug Mode

Set environment variable for debug logging:

GO_ENV=local go run cmd/server/main.go

About

A rest api with the crud methods for posts written in Go

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages