Skip to content

Commit

Permalink
feat: add HTTP log; fix alpha server reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Oct 29, 2024
1 parent 387d76a commit a512f77
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/fauxrpc/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type RunCmd struct {
Schema []string `help:"The modules to use for the RPC schema. It can be protobuf descriptors (binpb, json, yaml), a URL for reflection or a directory of descriptors."`
Addr string `short:"a" help:"Address to bind to." default:"127.0.0.1:6660"`
NoReflection bool `help:"Disables the server reflection service."`
NoHTTPLog bool `help:"Disables the HTTP log."`
NoDocPage bool `help:"Disables the documentation page."`
HTTPS bool `help:"Enables HTTPS, requires cert and certkey"`
Cert string `help:"Path to certificate file"`
Expand All @@ -33,7 +34,7 @@ type RunCmd struct {
}

func (c *RunCmd) Run(globals *Globals) error {
srv, err := server.NewServer(version, !c.NoDocPage, !c.NoReflection)
srv, err := server.NewServer(version, !c.NoDocPage, !c.NoReflection, !c.NoHTTPLog)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
connectrpc.com/grpcreflect v1.2.0
connectrpc.com/validate v0.1.0
connectrpc.com/vanguard v0.2.0
github.com/MadAppGang/httplog v1.3.0
github.com/alecthomas/kong v0.9.0
github.com/brianvoe/gofakeit/v7 v7.0.4
github.com/bufbuild/protocompile v0.14.0
Expand All @@ -22,16 +23,19 @@ require (
github.com/sudorandom/protoc-gen-connect-openapi v0.13.0
golang.org/x/net v0.30.0
golang.org/x/sync v0.8.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f
google.golang.org/protobuf v1.35.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20240618133044-5a0af90af097 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
Expand All @@ -42,6 +46,8 @@ require (
github.com/kr/pretty v0.3.1 // indirect
github.com/lmittmann/tint v1.0.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/onsi/ginkgo/v2 v2.9.5 // indirect
github.com/pb33f/libopenapi v0.18.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -58,5 +64,4 @@ require (
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,11 @@ git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3p
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/MadAppGang/httplog v1.3.0 h1:1XU54TO8kiqTeO+7oZLKAM3RP/cJ7SadzslRcKspVHo=
github.com/MadAppGang/httplog v1.3.0/go.mod h1:gpYEdkjh/Cda6YxtDy4AB7KY+fR7mb3SqBZw74A5hJ4=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down Expand Up @@ -693,6 +697,8 @@ github.com/envoyproxy/protoc-gen-validate v0.6.7/go.mod h1:dyJXwwfPK2VSqiB9Klm1J
github.com/envoyproxy/protoc-gen-validate v0.9.1/go.mod h1:OKNgG7TCp5pF4d6XftA0++PMirau2/yoOwVac3AbF2w=
github.com/envoyproxy/protoc-gen-validate v1.1.0 h1:tntQDh69XqOCOZsDz0lVJQez/2L6Uu2PdjCQwWCJ3bM=
github.com/envoyproxy/protoc-gen-validate v1.1.0/go.mod h1:sXRDRVmzEbkM7CVcM06s9shE/m23dg3wzjl0UWqJ2q4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
Expand Down Expand Up @@ -832,6 +838,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -862,8 +870,14 @@ github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuz
github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3/go.mod h1:RagcQ7I8IeTMnF8JTXieKnO4Z6JCsikNEzj0DwauVzE=
Expand Down
17 changes: 10 additions & 7 deletions private/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"connectrpc.com/grpcreflect"
"connectrpc.com/vanguard"
"github.com/MadAppGang/httplog"
"github.com/sudorandom/fauxrpc/private/registry"
"github.com/sudorandom/fauxrpc/private/stubs"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -34,9 +35,10 @@ type server struct {
version string
renderDocPage bool
useReflection bool
withHTTPLog bool
}

func NewServer(version string, renderDocPage, useReflection bool) (*server, error) {
func NewServer(version string, renderDocPage, useReflection, withHTTPLog bool) (*server, error) {
serviceRegistry, err := registry.NewServiceRegistry()
if err != nil {
return nil, err
Expand All @@ -52,6 +54,7 @@ func NewServer(version string, renderDocPage, useReflection bool) (*server, erro
handlerReflectorV1: NewWrappedHandler(),
handlerReflectorV1Alpha: NewWrappedHandler(),
handlerTranscoder: NewWrappedHandler(),
withHTTPLog: withHTTPLog,
}, nil
}

Expand Down Expand Up @@ -100,7 +103,7 @@ func (s *server) rebuildHandlers() error {
s.handlerReflectorV1.SetHandler(v1Handler)

_, v1alphaHandler := grpcreflect.NewHandlerV1Alpha(reflector)
s.handlerReflectorV1.SetHandler(v1alphaHandler)
s.handlerReflectorV1Alpha.SetHandler(v1alphaHandler)
}

if s.renderDocPage {
Expand All @@ -118,17 +121,17 @@ func (s *server) Mux() (*http.ServeMux, error) {
return nil, err
}
mux := http.NewServeMux()
mux.Handle("/", s.handlerTranscoder)
mux.Handle("/", httplog.Logger(s.handlerTranscoder))

if s.useReflection {
mux.Handle("/grpc.reflection.v1.ServerReflection/", s.handlerReflectorV1)
mux.Handle("/grpc.reflection.v1alpha.ServerReflection/", s.handlerReflectorV1Alpha)
mux.Handle("/grpc.reflection.v1.ServerReflection/", httplog.Logger(s.handlerReflectorV1))
mux.Handle("/grpc.reflection.v1alpha.ServerReflection/", httplog.Logger(s.handlerReflectorV1Alpha))
}

// OpenAPI Stuff
if s.renderDocPage {
mux.Handle("GET /fauxrpc/openapi.html", singleFileHandler(openapiHTML))
mux.Handle("GET /fauxrpc/openapi.yaml", s.handlerOpenAPI)
mux.Handle("GET /fauxrpc/openapi.html", httplog.Logger(singleFileHandler(openapiHTML)))
mux.Handle("GET /fauxrpc/openapi.yaml", httplog.Logger(s.handlerOpenAPI))
}
return mux, nil
}
Expand Down

0 comments on commit a512f77

Please sign in to comment.