Add CORS Support to allow frontend requests
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m28s
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m28s
This commit is contained in:
13
main.go
13
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
|
||||
"connectrpc.com/connect"
|
||||
connectcors "connectrpc.com/cors"
|
||||
"connectrpc.com/grpcreflect"
|
||||
"connectrpc.com/validate"
|
||||
"git.kocoder.xyz/vt/shortener/internal/config"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"git.kocoder.xyz/vt/shortener/internal/service"
|
||||
"git.kocoder.xyz/vt/shortener/internal/telemetry"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/rs/cors"
|
||||
"github.com/uptrace/opentelemetry-go-extra/otelsql"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
@@ -66,8 +68,17 @@ func main() {
|
||||
|
||||
mux.Handle("/", srv)
|
||||
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: conf.ALLOWED_ORIGINS,
|
||||
AllowedMethods: connectcors.AllowedMethods(),
|
||||
AllowedHeaders: connectcors.AllowedHeaders(),
|
||||
ExposedHeaders: connectcors.ExposedHeaders(),
|
||||
MaxAge: 7200,
|
||||
})
|
||||
corsMux := c.Handler(mux)
|
||||
|
||||
// Wrap the server with OpenTelemetry HTTP handler for tracing & metrics
|
||||
instrumentedHandler := otelhttp.NewHandler(mux, "shortener-server2")
|
||||
instrumentedHandler := otelhttp.NewHandler(corsMux, "shortener-server2")
|
||||
|
||||
p := new(http.Protocols)
|
||||
p.SetHTTP1(true)
|
||||
|
||||
Reference in New Issue
Block a user