Skip to content

Commit

Permalink
gateway: serve OpenAPI Schema at /openapi.json
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Zelinskie <jimmy@zelinskie.com>
  • Loading branch information
jzelinskie committed Oct 25, 2021
1 parent f119e2a commit c36faef
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package gateway

import (
"context"
"io"
"net/http"

"github.com/authzed/authzed-go/proto"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/grpcutil"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand Down Expand Up @@ -44,14 +46,20 @@ func NewHttpServer(ctx context.Context, cfg Config) (*http.Server, error) {
opts = append(opts, grpcutil.WithCustomCerts(cfg.UpstreamTlsCertPath, grpcutil.SkipVerifyCA))
}

mux := runtime.NewServeMux(runtime.WithMetadata(auth.PresharedKeyAnnotator))
if err := v1.RegisterSchemaServiceHandlerFromEndpoint(ctx, mux, cfg.UpstreamAddr, opts); err != nil {
gwMux := runtime.NewServeMux(runtime.WithMetadata(auth.PresharedKeyAnnotator))
if err := v1.RegisterSchemaServiceHandlerFromEndpoint(ctx, gwMux, cfg.UpstreamAddr, opts); err != nil {
return nil, err
}
if err := v1.RegisterPermissionsServiceHandlerFromEndpoint(ctx, mux, cfg.UpstreamAddr, opts); err != nil {
if err := v1.RegisterPermissionsServiceHandlerFromEndpoint(ctx, gwMux, cfg.UpstreamAddr, opts); err != nil {
return nil, err
}

mux := http.NewServeMux()
mux.Handle("/openapi.json", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, proto.OpenAPISchema)
}))
mux.Handle("/", gwMux)

return &http.Server{
Addr: cfg.Addr,
Handler: promhttp.InstrumentHandlerDuration(histogram, otelhttp.NewHandler(mux, "gateway")),
Expand Down

0 comments on commit c36faef

Please sign in to comment.