2023-09-17 16:29:46 -07:00
|
|
|
package main
|
|
|
|
|
|
2023-09-20 22:09:15 -07:00
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"connectrpc.com/grpcreflect"
|
|
|
|
|
servicepb "github.com/chathaway-codes/home-sensors/v2/gen/genconnect"
|
|
|
|
|
"github.com/chathaway-codes/home-sensors/v2/pkg/signaler"
|
2023-10-01 22:37:12 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2023-09-28 20:35:50 -07:00
|
|
|
"github.com/rs/cors"
|
2023-09-20 22:09:15 -07:00
|
|
|
)
|
2023-09-17 16:29:46 -07:00
|
|
|
|
|
|
|
|
func main() {
|
2023-09-20 22:09:15 -07:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
reflector := grpcreflect.NewStaticReflector(
|
|
|
|
|
servicepb.SignalerServiceName,
|
|
|
|
|
)
|
|
|
|
|
mux.Handle(grpcreflect.NewHandlerV1(reflector))
|
|
|
|
|
mux.Handle(grpcreflect.NewHandlerV1Alpha(reflector))
|
|
|
|
|
mux.Handle(servicepb.NewSignalerServiceHandler(signaler.New()))
|
|
|
|
|
|
2023-09-28 20:35:50 -07:00
|
|
|
corsHandler := cors.New(cors.Options{
|
|
|
|
|
AllowedMethods: []string{
|
|
|
|
|
http.MethodGet,
|
|
|
|
|
http.MethodPost,
|
|
|
|
|
},
|
2023-10-03 16:17:34 -07:00
|
|
|
AllowedOrigins: []string{"*"},
|
2023-09-28 20:35:50 -07:00
|
|
|
AllowedHeaders: []string{
|
|
|
|
|
"Accept-Encoding",
|
|
|
|
|
"Authorization",
|
|
|
|
|
"Content-Encoding",
|
|
|
|
|
"Content-Type",
|
|
|
|
|
"Connect-Protocol-Version",
|
|
|
|
|
"Connect-Timeout-Ms",
|
|
|
|
|
"Connect-Accept-Encoding", // Unused in web browsers, but added for future-proofing
|
|
|
|
|
"Connect-Content-Encoding", // Unused in web browsers, but added for future-proofing
|
|
|
|
|
"Grpc-Timeout", // Used for gRPC-web
|
|
|
|
|
"X-Grpc-Web", // Used for gRPC-web
|
|
|
|
|
"X-User-Agent", // Used for gRPC-web
|
|
|
|
|
},
|
|
|
|
|
ExposedHeaders: []string{
|
|
|
|
|
"Content-Encoding", // Unused in web browsers, but added for future-proofing
|
|
|
|
|
"Connect-Content-Encoding", // Unused in web browsers, but added for future-proofing
|
|
|
|
|
"Grpc-Status", // Required for gRPC-web
|
|
|
|
|
"Grpc-Message", // Required for gRPC-web
|
|
|
|
|
},
|
|
|
|
|
})
|
2023-10-01 22:37:12 -07:00
|
|
|
mux.Handle("/metrics", promhttp.Handler())
|
2023-09-28 20:35:50 -07:00
|
|
|
handler := corsHandler.Handler(mux)
|
|
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
|
Addr: "0.0.0.0:8080",
|
2023-10-03 16:17:34 -07:00
|
|
|
Handler: handler,
|
|
|
|
|
//Handler: h2c.NewHandler(handler, &http2.Server{}),
|
|
|
|
|
|
2023-09-28 20:35:50 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-03 16:17:34 -07:00
|
|
|
if err := server.ListenAndServeTLS("server.crt", "server.key"); err != nil {
|
2023-09-20 22:09:15 -07:00
|
|
|
log.Fatalf("Failed to listen for HTTP traffic: %v", err)
|
|
|
|
|
}
|
2023-09-17 16:29:46 -07:00
|
|
|
}
|