Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parameterize the fallback handler to httprule.Server #48

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/http"
"os"
"strings"

Expand Down Expand Up @@ -79,7 +80,7 @@ func (cs *cmdServe) Run(logLevel log.LogLevel) error {
}

if cs.HTTP {
h := httprule.NewServer(s.Files, s.UnknownHandler, logger, nil)
h := httprule.NewServer(s.Files, s.UnknownHandler, logger, nil, http.NotFoundHandler())
s.SetHTTPHandler(h)
}

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestHTTPRuleServer(t *testing.T) {
require.NoError(t, err)

ts := serve.NewUnstartedTestServer(serve.JsonnetEvaluator(), os.DirFS("serve/testdata/httpgreet"), opts...)
ts.SetHTTPHandler(httprule.NewServer(ts.Files, ts.UnknownHandler, log.DiscardLogger, nil))
ts.SetHTTPHandler(httprule.NewServer(ts.Files, ts.UnknownHandler, log.DiscardLogger, nil, http.NotFoundHandler()))
ts.Start()
defer ts.Stop()

Expand Down
7 changes: 5 additions & 2 deletions serve/httprule/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ type Server struct {
httpMethods []*httpMethod
grpcHandler grpc.StreamHandler
log log.Logger
next http.Handler
}

func NewServer(files *registry.Files, handler grpc.StreamHandler, l log.Logger, httpRuleTemplates []*annotations.HttpRule) *Server {
func NewServer(files *registry.Files, handler grpc.StreamHandler, l log.Logger, httpRuleTemplates []*annotations.HttpRule, next http.Handler) *Server {
return &Server{
httpMethods: loadHTTPRules(l, files, httpRuleTemplates),
grpcHandler: handler,
log: l,
next: next,
}
}

Expand All @@ -46,7 +48,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
}
http.NotFound(w, r)

s.next.ServeHTTP(w, r)
}

// Serve a google.api.http annotated method as HTTP
Expand Down
4 changes: 2 additions & 2 deletions serve/httprule/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestHTTP(t *testing.T) {
ts := serve.NewTestServer(serve.JsonnetEvaluator(), os.DirFS("testdata/greet"), withLogger)
defer ts.Stop()

h := NewServer(ts.Files, ts.UnknownHandler, log.DiscardLogger, nil)
h := NewServer(ts.Files, ts.UnknownHandler, log.DiscardLogger, nil, http.NotFoundHandler())
ts.SetHTTPHandler(h)

body := `{"first_name": "Stranger"}`
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestHTTPRuleInterpolation(t *testing.T) {
{Pattern: &annotations.HttpRule_Post{Post: "/post/{package}.{service}/{method}"}, Body: "*"},
{Pattern: &annotations.HttpRule_Get{Get: "/get/{method}"}},
}
h := NewServer(ts.Files, ts.UnknownHandler, logger, tmpl)
h := NewServer(ts.Files, ts.UnknownHandler, logger, tmpl, http.NotFoundHandler())
ts.SetHTTPHandler(h)

u := "http://" + ts.Addr() + "/get/SimpleHello"
Expand Down
Loading