tsk-4: Adding CORS support

This commit is contained in:
phoenix
2025-12-12 13:59:04 -05:00
parent fbaa95b2e9
commit fd8fdd93b9
+12 -1
View File
@@ -12,6 +12,7 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
"github.com/rs/cors"
"github.com/swaggo/http-swagger/v2" "github.com/swaggo/http-swagger/v2"
_ "git.kundeng.us/phoenix/textsender-api/docs" _ "git.kundeng.us/phoenix/textsender-api/docs"
@@ -115,10 +116,20 @@ func main() {
httpSwagger.URL(fmt.Sprintf("http://localhost:%s/swagger/doc.json", config.PORT)), httpSwagger.URL(fmt.Sprintf("http://localhost:%s/swagger/doc.json", config.PORT)),
)) ))
// Configure CORS
c := cors.New(cors.Options{
AllowedOrigins: []string{fmt.Sprintf("http://localhost:%s", config.PORT), "https://textsender.com"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link", "X-Total-Count"},
AllowCredentials: true,
MaxAge: 300, // 5 minutes
})
// Start server // Start server
server := &http.Server{ server := &http.Server{
Addr: ":" + cfg.ServerPort, Addr: ":" + cfg.ServerPort,
Handler: router, Handler: c.Handler(router),
ReadTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second, IdleTimeout: 60 * time.Second,