Aller au contenu

Partie 1: Installation et Configuration

circle Partie 1/4 - Temps estimé: 15 minutes

arrow_back Retour à l'index


Objectif

Créer une API REST complète pour un blog avec:

  • Articles (Posts) avec auteur, titre, contenu, tags
  • Commentaires sur les articles
  • Authentification JWT (déjà incluse dans create-go-starter)
  • Tests complets
  • Déploiement Docker

À la fin de ce tutorial, vous aurez une API Blog production-ready avec toutes les bonnes pratiques.

Prérequis

Logiciels requis

  • Go 1.25+ - Télécharger
  • PostgreSQL ou Docker - Pour la base de données
  • curl ou Postman - Pour tester l'API
  • Éditeur de code (VS Code, GoLand, etc.)

Connaissances recommandées

  • Bases de Go (structs, interfaces, error handling)
  • Concepts REST API
  • Familiarité avec SQL/PostgreSQL (basique)

Pas besoin d'être expert! Ce tutorial explique chaque étape en détail.


Étape 1: Installation du CLI

Installation globale (recommandée)

La méthode la plus simple pour installer create-go-starter:

go install github.com/tky0065/go-starter-kit/cmd/create-go-starter@latest

Cette commande télécharge, compile et installe le CLI globalement.

Vérification

create-go-starter --help

Vous devriez voir l'aide s'afficher.

Note: Si la commande n'est pas trouvée, ajoutez $GOPATH/bin à votre PATH:

export PATH=$PATH:$(go env GOPATH)/bin

Étape 2: Génération du projet

Créer le projet

create-go-starter blog-api

Cette commande génère ~45 fichiers avec toute l'architecture nécessaire.

Structure générée

cd blog-api
tree -L 3

Résultat:

blog-api/
├── cmd/
│   └── main.go                       # Point d'entrée avec fx DI
├── internal/
│   ├── models/
│   │   └── user.go                   # Entités: User, RefreshToken, AuthResponse
│   ├── domain/
│   │   ├── user/                     # Domaine User (pré-généré)
│   │   │   ├── service.go
│   │   │   └── module.go
│   │   └── errors.go
│   ├── adapters/
│   │   ├── handlers/
│   │   │   ├── auth_handler.go
│   │   │   └── user_handler.go
│   │   ├── middleware/
│   │   │   ├── auth_middleware.go
│   │   │   └── error_handler.go
│   │   └── repository/
│   │       └── user_repository.go
│   ├── infrastructure/
│   │   ├── database/
│   │   └── server/
│   └── interfaces/                   # Ports (interfaces)
│       └── user_repository.go
├── pkg/
│   ├── auth/                         # JWT utilities
│   ├── config/                       # Configuration
│   └── logger/                       # Zerolog logger
├── docs/
│   ├── README.md
│   └── quick-start.md
├── .env                              # Configuration (auto-copié)
├── .env.example
├── Dockerfile
├── Makefile
├── go.mod
└── README.md

check_circle Checkpoint 1: Le projet est généré avec succès.


Étape 3: Configuration initiale

3.1 Installer les dépendances

cd blog-api
go mod tidy

Cette commande télécharge toutes les dépendances (Fiber, GORM, fx, etc.).

3.2 Configurer PostgreSQL

Vous avez 2 options:

Option A: Docker (recommandé)

docker run -d \
  --name blog-postgres \
  -e POSTGRES_DB=blog_api \
  -e POSTGRES_PASSWORD=postgres \
  -p 5432:5432 \
  postgres:16-alpine

Option B: PostgreSQL local

Si PostgreSQL est installé localement:

createdb blog_api

3.3 Configurer les variables d'environnement

Générer un secret JWT sécurisé:

JWT_SECRET=$(openssl rand -base64 32)
echo "JWT_SECRET généré: $JWT_SECRET"

Éditer le fichier .env:

nano .env

Contenu du .env:

# Application
APP_NAME=blog-api
APP_ENV=development
APP_PORT=8080

# Database
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=blog_api
DB_SSLMODE=disable

# JWT
JWT_SECRET=<coller_le_secret_généré_ici>
JWT_EXPIRY=15m
REFRESH_TOKEN_EXPIRY=168h

Important: Remplacez <coller_le_secret_généré_ici> par le JWT_SECRET généré.


Étape 4: Tester le projet de base

4.1 Lancer l'application

make run

Vous devriez voir:

2024/01/10 10:00:00 INF Starting blog-api server on :8080

4.2 Tester le health check

Dans un autre terminal:

curl http://localhost:8080/health

Réponse attendue:

{"status":"ok"}

4.3 Tester l'authentification par défaut

Créer un utilisateur

curl -X POST http://localhost:8080/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "admin@blog.com",
    "password": "admin123"
  }'

Réponse:

{
  "access_token": "eyJhbGci...",
  "refresh_token": "eyJhbGci...",
  "user": {
    "id": 1,
    "email": "admin@blog.com",
    "created_at": "2024-01-10T10:05:00Z"
  }
}

Se connecter

curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "admin@blog.com",
    "password": "admin123"
  }'

Même réponse avec access_token et refresh_token.

Tester une route protégée

# Remplacez <ACCESS_TOKEN> par le token reçu
curl -X GET http://localhost:8080/api/v1/users \
  -H "Authorization: Bearer <ACCESS_TOKEN>"

Réponse:

[
  {
    "id": 1,
    "email": "admin@blog.com",
    "created_at": "2024-01-10T10:05:00Z"
  }
]

check_circle Checkpoint 2: Le projet de base fonctionne parfaitement avec User et Auth.


Résumé de la Partie 1

check Installation du CLI create-go-starter check Génération d'un projet complet check Configuration PostgreSQL et JWT check Test de l'authentification

Vous avez maintenant un projet fonctionnel avec authentification JWT et gestion des utilisateurs.


arrow_back Retour à l'index | Partie 2: Créer votre premier domaine arrow_forward