-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
162 lines (137 loc) · 4.24 KB
/
main.go
File metadata and controls
162 lines (137 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"database/sql"
"log"
"net/http"
"os"
"os/signal"
"time"
"weather-notification/internal/domain/service"
middleware "weather-notification/internal/middlewares"
_ "weather-notification/docs"
"weather-notification/internal/infrastructure/adapter/api/handler"
"weather-notification/internal/infrastructure/adapter/cptec"
postgres "weather-notification/internal/infrastructure/adapter/persistence/postgres"
"weather-notification/internal/infrastructure/adapter/queue"
"weather-notification/internal/infrastructure/worker"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// @title API de Notificação de Previsão do Tempo
// @version 1.0
// @description Serviço de notificações de previsão do tempo
// @host localhost:8080
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @BasePath /api
func main() {
if err := godotenv.Load(); err != nil {
log.Printf("Arquivo .env não encontrado, usando variáveis do sistema")
}
//DATABASE CONFIG
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalf("Erro ao conectar ao banco: %v", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
log.Fatalf("Erro ao pingar banco: %v", err)
}
// REPOSITORIES
userRepo := postgres.NewUserRepository(db)
locationRepo := postgres.NewLocationRepository(db)
notificationRepo := postgres.NewNotificationRepository(db)
globalNotificationRepo := postgres.NewGlobalNotificationRepository(db)
// ADAPTERS
cptecClient := cptec.NewClient()
queueService, err := queue.NewRabbitMQService(os.Getenv("RABBITMQ_URL"))
if err != nil {
log.Fatalf("Erro ao conectar ao RabbitMQ: %v", err)
}
defer queueService.Close()
// SERVICES
weatherService := service.NewWeatherService(cptecClient, locationRepo)
notificationService := service.NewNotificationService(
notificationRepo,
userRepo,
weatherService,
queueService,
)
globalNotificationService := service.NewGlobalNotificationService(
globalNotificationRepo,
userRepo,
queueService,
weatherService,
notificationRepo,
)
userService := service.NewUserService(userRepo)
// WORKERS
notificationWorker := worker.NewNotificationWorker(
context.Background(),
notificationService,
weatherService,
queueService,
)
globalWorker := worker.NewGlobalNotificationWorker(context.Background(), globalNotificationService)
go func() {
if err := notificationWorker.Start(); err != nil {
log.Printf("Erro no worker: %v", err)
}
}()
go func() {
if err := globalWorker.Start(); err != nil {
log.Printf("Erro no worker de notificações globais: %v", err)
}
}()
// API
forecastHandler := handler.NewWeatherHandler(weatherService)
notificationHandler := handler.NewNotificationHandler(notificationService)
globalNotificationHandler := handler.NewGlobalNotificationHandler(globalNotificationService)
userHandler := handler.NewUserHandler(userService, weatherService)
webhookHandler := handler.NewWebhookHandler()
gin.SetMode(os.Getenv("GIN_MODE"))
router := gin.Default()
router.Use(cors.Default())
// SWAGGO
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
api := router.Group("/api", middleware.AuthMiddleware())
{
forecastHandler.SetupRoutes(api)
notificationHandler.SetupRoutes(api)
globalNotificationHandler.SetupRoutes(api)
userHandler.SetupRoutes(api)
webhookHandler.SetupRoutes(api)
}
router.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "OK",
})
})
srv := &http.Server{
Addr: ":" + os.Getenv("PORT"),
Handler: router,
}
go func() {
log.Printf("Servidor iniciado na porta %s", os.Getenv("PORT"))
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Erro ao iniciar servidor: %v", err)
}
}()
// SHUTDOWN
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Desligando servidor...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forçado a fechar:", err)
}
log.Println("Servidor encerrado")
}