-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
50 lines (41 loc) · 1.51 KB
/
main.go
File metadata and controls
50 lines (41 loc) · 1.51 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
package main
import (
"log" // <- Adicione esta linha
"github.com/avanti-dvp/ms-saudacoes-aleatorias/database"
"github.com/avanti-dvp/ms-saudacoes-aleatorias/handlers"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
// Inicializa a conexão com o banco de dados
database.ConnectDatabase()
// Cria um router Gin com as configurações padrão
router := gin.Default()
// A configuração abaixo permite todas as origens (AllowAllOrigins).
// É uma configuração liberal, ideal para APIs públicas.
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // Permite todas as origens
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
// Uma forma ainda mais simples para permitir TODAS as origens é:
// router.Use(cors.Default()) // <- Alternativa simples que já permite '*'
// Define as rotas da API
api := router.Group("/api")
{
// Rota para cadastrar um novo cumprimento
// Ex: POST /api/saudacoes
api.POST("/saudacoes", handlers.CreateGreeting)
// Rota para obter um cumprimento aleatório
// Ex: GET /api/saudacoes/aleatorio
api.GET("/saudacoes/aleatorio", handlers.GetRandomGreeting)
}
// Inicia o servidor na porta 8080
// Você pode acessar em http://localhost:8080
if err := router.Run(":8080"); err != nil {
log.Fatalf("failed to run server: %v", err)
}
// novamnete
}