Skip to content

Commit

Permalink
fix(grpc): add basic auth in swagger header (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Jun 29, 2024
1 parent 23613ac commit 08559b4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 86 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ docker:
proto:
$(RM) www/grpc/gen
cd www/grpc/buf && buf generate --template buf.gen.yaml ../proto
# Generate static assets for Swagger-UI
cd www/grpc/ && statik -m -f -src swagger-ui/

########################################
### Formatting the code
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.1
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
Expand Down
55 changes: 0 additions & 55 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion www/grpc/buf/openapi.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ openapiOptions:
security:
BasicAuth:
type: TYPE_BASIC
in: IN_HEADER
security:
- securityRequirement:
BasicAuth: {}
24 changes: 12 additions & 12 deletions www/grpc/gateway.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package grpc

import (
"embed"
"fmt"
"mime"
"io/fs"
"net"
"net/http"
"strings"
"time"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
_ "github.com/pactus-project/pactus/www/grpc/statik" // Static files.
"github.com/rakyll/statik/fs"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

//go:embed swagger-ui
var swaggerFS embed.FS

type GatewayConfig struct {
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
EnableCORS bool `toml:"enable_cors"`
}

// getOpenAPIHandler serves an OpenAPI UI.
// https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go
func (*Server) getOpenAPIHandler() (http.Handler, error) {
err := mime.AddExtensionType(".svg", "image/svg+xml")
if err != nil {
return nil, err
}
if _, err := swaggerFS.ReadFile("swagger-ui/index.html"); err == nil {
swagger, err := fs.Sub(swaggerFS, "swagger-ui")
if err != nil {
return nil, err
}

statikFS, err := fs.New()
if err != nil {
return nil, err
return http.FileServer(http.FS(swagger)), nil
}

return http.FileServer(statikFS), nil
return http.FileServer(http.Dir("swagger-ui")), nil
}

func (s *Server) startGateway(grpcAddr string) error {
Expand Down
12 changes: 0 additions & 12 deletions www/grpc/statik/statik.go

This file was deleted.

10 changes: 7 additions & 3 deletions www/grpc/swagger-ui/pactus.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2206,8 +2206,12 @@
},
"securityDefinitions": {
"BasicAuth": {
"type": "basic",
"in": "header"
"type": "basic"
}
}
},
"security": [
{
"BasicAuth": []
}
]
}

0 comments on commit 08559b4

Please sign in to comment.