Skip to content

Commit

Permalink
Atualiza Swagger para usar HTTPS
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemacedo1 committed Aug 11, 2024
1 parent 3505844 commit 1912e76
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"net/http"
"os"

_ "github.com/FelipeAJdev/dev-cloud-challenge/docs" // Importa os documentos gerados pelo swagger
docs "github.com/FelipeAJdev/dev-cloud-challenge/docs" // Ajuste: Importa o pacote de documentação gerada
_ "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 @@ -26,8 +25,9 @@ import (
// @contact.name Felipe Macedo
// @contact.email felipealexandrej@gmail.com

// @host dev-cloud-challenge-b3f5485f2dcf.herokuapp.com
// @BasePath /
// @schemes http
// @schemes https
func main() {
log := initLogger()

Expand All @@ -39,13 +39,6 @@ func main() {
}
}

// Configura o host dinamicamente para o Swagger
host := os.Getenv("HOST")
if host == "" {
host = "localhost:8080" // Valor padrão para desenvolvimento
}
docs.SwaggerInfo.Host = host // Ajuste: Configura o host para o Swagger

database := pgstore.InitDB()
defer func() {
if err := database.Close(); err != nil {
Expand All @@ -70,6 +63,21 @@ func main() {

router := mux.NewRouter()

// Configura o CORS Middleware
router.Use(mux.CORSMethodMiddleware(router))
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") // Permite todas as origens, altere conforme necessário
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
})

// Redireciona a rota raiz para o Swagger
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/swagger/", http.StatusMovedPermanently)
Expand Down

0 comments on commit 1912e76

Please sign in to comment.