Skip to content

Commit

Permalink
feat: standardize error handling and response format
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemacedo1 committed Aug 10, 2024
1 parent 6bea653 commit f08dc06
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"net/http"
"os"

_ "github.com/FelipeAJdev/dev-cloud-challenge/docs" // Importa os documentos gerados pelo swagger
"github.com/FelipeAJdev/dev-cloud-challenge/internal/handlers"
"github.com/FelipeAJdev/dev-cloud-challenge/internal/repository"
"github.com/FelipeAJdev/dev-cloud-challenge/internal/services"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/gorilla/mux"
"github.com/joho/godotenv"
logrus "github.com/sirupsen/logrus"
httpSwagger "github.com/swaggo/http-swagger"
)

// @title API de Gestão de Alunos
Expand All @@ -25,38 +28,10 @@ import (
// @host localhost:8080
// @BasePath /
// @schemes http

func initLogger() *logrus.Logger {
log := logrus.New()
log.SetFormatter(&logrus.JSONFormatter{})
log.SetLevel(logrus.InfoLevel)
return log
}

func runMigrations(databaseURL string, log *logrus.Logger) {
m, err := migrate.New(
"file://./internal/store/pgstore/migrations",
databaseURL)
if err != nil {
log.WithFields(logrus.Fields{
"error": err,
"url": databaseURL,
}).Fatal("Falha ao criar a instância de migrate")
}

if err := m.Up(); err != nil && err != migrate.ErrNoChange {
log.WithFields(logrus.Fields{
"error": err,
}).Fatal("Falha ao aplicar migrations")
}

log.Info("Migrations aplicadas com sucesso!")
}

func main() {
log := initLogger()

if err := godotenv.Load(); err != nil {
if err := godotenv.Load("../.env"); err != nil {
log.WithFields(logrus.Fields{
"error": err,
}).Fatal("Erro ao carregar arquivo .env")
Expand All @@ -68,16 +43,21 @@ func main() {
log.WithFields(logrus.Fields{
"error": err,
}).Error("Erro ao fechar a conexão com o banco de dados")
} else {
log.Info("Conexão com o banco de dados fechada com sucesso")
}
}()

databaseURL := "postgres://postgres:123456789@localhost:5432/wsrs?sslmode=disable"
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
log.Fatal("DATABASE_URL is not set in the environment")
}

runMigrations(databaseURL, log)

alunoRepository := repository.NewAlunoRepository(database)
alunoService := services.NewAlunoService(alunoRepository)
alunoHandler := handlers.NewAlunoHandler(alunoService)
alunoHandler := handlers.NewAlunoHandler(alunoService, log)

router := mux.NewRouter()

Expand All @@ -87,10 +67,39 @@ func main() {
router.HandleFunc("/alunos/{id}", alunoHandler.UpdateAluno).Methods("PUT")
router.HandleFunc("/alunos/{id}", alunoHandler.DeleteAluno).Methods("DELETE")

router.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)

log.Info("Servidor rodando na porta 8080")
if err := http.ListenAndServe(":8080", router); err != nil {
log.WithFields(logrus.Fields{
"error": err,
}).Fatal("Erro ao iniciar o servidor HTTP")
}
}

func initLogger() *logrus.Logger {
log := logrus.New()
log.SetFormatter(&logrus.JSONFormatter{})
log.SetLevel(logrus.InfoLevel)
return log
}

func runMigrations(databaseURL string, log *logrus.Logger) {
m, err := migrate.New(
"file://../internal/store/pgstore/migrations",
databaseURL)
if err != nil {
log.WithFields(logrus.Fields{
"error": err,
"url": databaseURL,
}).Fatal("Falha ao criar a instância de migrate")
}

if err := m.Up(); err != nil && err != migrate.ErrNoChange {
log.WithFields(logrus.Fields{
"error": err,
}).Fatal("Falha ao aplicar migrations")
}

log.Info("Migrations aplicadas com sucesso!")
}

0 comments on commit f08dc06

Please sign in to comment.