Skip to content

Commit 2fb72b3

Browse files
committed
grpc
1 parent cb4f984 commit 2fb72b3

1 file changed

Lines changed: 2 additions & 69 deletions

File tree

cmd/server/main.go

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
openvmv1Connect "github.com/openvm-http/openvm-api/gen/openvm/v1/v1connect"
88
"github.com/openvm-http/openvm-api/internal/interceptor"
99
openvmServer "github.com/openvm-http/openvm-api/internal/service/openvm"
10-
"github.com/rs/cors"
1110
"log"
1211
"net/http"
1312
"os"
@@ -22,45 +21,6 @@ import (
2221
var gitTag string
2322
var dateTime string
2423

25-
func disableCORS() *cors.Cors {
26-
// To let web developers play with the demo service from browsers, we need a
27-
// very permissive CORS setup.
28-
return cors.New(cors.Options{
29-
AllowedMethods: []string{
30-
http.MethodHead,
31-
http.MethodGet,
32-
http.MethodPost,
33-
http.MethodPut,
34-
http.MethodPatch,
35-
http.MethodDelete,
36-
},
37-
AllowOriginFunc: func(_ /* origin */ string) bool {
38-
// Allow all origins, which effectively disables CORS.
39-
return true
40-
},
41-
AllowedHeaders: []string{"*"},
42-
ExposedHeaders: []string{
43-
// Content-Type is in the default safelist.
44-
"Accept",
45-
"Accept-Encoding",
46-
"Accept-Post",
47-
"Connect-Accept-Encoding",
48-
"Connect-Content-Encoding",
49-
"Content-Encoding",
50-
"Grpc-Accept-Encoding",
51-
"Grpc-Encoding",
52-
"Grpc-Message",
53-
"Grpc-Status",
54-
"Grpc-Status-Details-Bin",
55-
},
56-
// Let browsers cache CORS information for longer, which reduces the number
57-
// of preflight requests. Any changes to ExposedHeaders won't take effect
58-
// until the cached data expires. FF caps this value at 24h, and modern
59-
// Chrome caps it at 2h.
60-
MaxAge: int(2 * time.Hour / time.Second),
61-
})
62-
}
63-
6424
func main() {
6525
log.Printf("OpenVM-API %s %s", gitTag, dateTime)
6626
if token := os.Getenv("ACCESS_TOKEN"); token != "" {
@@ -73,33 +33,11 @@ func main() {
7333
addr = addrEnv
7434
}
7535

76-
interceptors := connect.WithInterceptors(interceptor.NewAuthInterceptor())
7736
api := http.NewServeMux()
7837
api.Handle(openvmv1Connect.NewApiServiceHandler(
7938
&openvmServer.ApiServer{},
80-
interceptors,
39+
connect.WithInterceptors(interceptor.NewAuthInterceptor()),
8140
))
82-
mux := http.NewServeMux()
83-
mux.Handle("/api/", http.StripPrefix("/api", api))
84-
var httpServerMux http.Handler
85-
if disableCors := os.Getenv("DISABLE_CORS"); disableCors == "YES_I_KNOWN_NOT_SAFE" {
86-
log.Printf("Security Warning: DISABLE_CORS set!\n")
87-
httpServerMux = disableCORS().Handler(mux)
88-
} else {
89-
httpServerMux = mux
90-
}
91-
92-
srv := &http.Server{
93-
Addr: addr,
94-
Handler: h2c.NewHandler(httpServerMux, &http2.Server{}),
95-
}
96-
log.Printf("HTTP server listening on %s\n", addr)
97-
go func() {
98-
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
99-
log.Fatalf("HTTP listen and serve: %v\n", err)
100-
}
101-
}()
102-
// grpc
10341
apiSrv := &http.Server{
10442
Addr: addr,
10543
Handler: h2c.NewHandler(api, &http2.Server{}),
@@ -116,12 +54,7 @@ func main() {
11654
<-signals
11755
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
11856
defer cancel()
119-
if err := srv.Shutdown(ctx); err != nil {
120-
log.Fatalf("HTTP shutdown: %v\n", err)
121-
}
122-
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Second)
123-
defer cancel2()
124-
if err := apiSrv.Shutdown(ctx2); err != nil {
57+
if err := apiSrv.Shutdown(ctx); err != nil {
12558
log.Fatalf("apiSrv shutdown: %v\n", err)
12659
}
12760
}

0 commit comments

Comments
 (0)