CORS
All checks were successful
Docker Deploy / build-and-push (push) Successful in 1m12s

This commit is contained in:
Atridad Lahiji 2025-03-24 01:15:32 -06:00
parent 0b85919395
commit 4547bf9f6e
Signed by: atridad
SSH key fingerprint: SHA256:LGomp8Opq0jz+7kbwNcdfTcuaLRb5Nh0k5AchDDb438
2 changed files with 18 additions and 0 deletions

View file

@ -30,6 +30,14 @@ func SSE(c echo.Context) error {
c.Response().Header().Set(echo.HeaderConnection, "keep-alive") c.Response().Header().Set(echo.HeaderConnection, "keep-alive")
c.Response().Header().Set(echo.HeaderCacheControl, "no-cache") c.Response().Header().Set(echo.HeaderCacheControl, "no-cache")
// Get origin from request
origin := c.Request().Header.Get(echo.HeaderOrigin)
// Only allow specific origins
if origin == "https://atri.dad" || origin == "http://localhost:3000" {
c.Response().Header().Set(echo.HeaderAccessControlAllowOrigin, origin)
c.Response().Header().Set(echo.HeaderAccessControlAllowCredentials, "true")
}
// Create a channel to receive messages from the lib.SSEServer // Create a channel to receive messages from the lib.SSEServer
clientChan := make(chan string) clientChan := make(chan string)

10
main.go
View file

@ -43,6 +43,16 @@ func main() {
})) }))
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50))) e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50)))
// Add CORS middleware
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"https://atri.dad", "http://localhost:3000"},
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
ExposeHeaders: []string{echo.HeaderContentType},
AllowCredentials: true,
MaxAge: 86400,
}))
// Static server // Static server
fs := http.FS(PublicFS) fs := http.FS(PublicFS)
e.GET("/public/*", echo.WrapHandler(http.FileServer(fs))) e.GET("/public/*", echo.WrapHandler(http.FileServer(fs)))