Skip to content

Latest commit

 

History

History
115 lines (90 loc) · 2.41 KB

File metadata and controls

115 lines (90 loc) · 2.41 KB

Backend Development with Golang

This section covers building the backend API server using Golang with both REST and GraphQL endpoints.

Technology Stack

  • Go 1.21: Modern Go version
  • Gin: HTTP web framework
  • GORM: ORM library for database operations
  • PostgreSQL: Relational database
  • GraphQL-Go: GraphQL implementation
  • UUID: Unique identifier generation

Project Structure

backend/
├── internal/
│   ├── api/
│   │   ├── rest/       # REST API handlers
│   │   ├── graphql/    # GraphQL schema and resolvers
│   │   └── routes.go   # Route setup
│   ├── config/         # Configuration management
│   ├── database/       # Database connection and setup
│   └── models/         # Data models
├── go.mod              # Go module definition
└── main.go             # Application entry point

Key Features

1. Dual API Support

  • RESTful API endpoints
  • GraphQL API with schema-first approach
  • Shared business logic

2. Database Integration

  • PostgreSQL with GORM
  • Auto-migration support
  • Connection pooling

3. Clean Architecture

  • Separation of concerns
  • Dependency injection
  • Environment-based configuration

API Endpoints

REST API

GET    /api/health      # Health check
GET    /api/users       # List all users
POST   /api/users       # Create new user
GET    /api/users/:id   # Get user by ID
PUT    /api/users/:id   # Update user
DELETE /api/users/:id   # Delete user

GraphQL API

POST   /graphql         # GraphQL endpoint
GET    /graphql         # GraphiQL playground

Development Commands

# Install dependencies
cd backend && go mod tidy

# Run development server
go run main.go

# Build binary
go build -o bin/server main.go

# Run tests
go test ./...

Environment Variables

DATABASE_URL=postgres://user:password@localhost:5432/tutorial_db?sslmode=disable
PORT=8080
ENVIRONMENT=development

GraphQL Schema Example

type User {
  id: ID!
  name: String!
  email: String!
  created_at: String!
  updated_at: String!
}

type Query {
  users: [User!]!
  user(id: ID!): User
  health: Health!
}

type Mutation {
  addUser(input: UserInput!): User!
  updateUser(id: ID!, input: UserInput!): User!
  deleteUser(id: ID!): Boolean!
}

Next Steps

Continue to Docker Configuration to learn about containerizing the application.