Skip to content

Commit

Permalink
fix(web): superfluous write
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 19, 2023
1 parent e0148ca commit 63786d0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkg/web/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewContextHandler(ctx context.Context) func(http.Handler) http.Handler {
ctx = db.WithContext(ctx, dbx)
ctx = store.WithContext(ctx, datastore)
r = r.WithContext(ctx)

next.ServeHTTP(w, r)
})
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/web/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var (
}, []string{"repo", "file"})
)

func withParams(h http.Handler) http.Handler {
func withParams(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cfg := config.FromContext(ctx)
Expand All @@ -97,7 +97,8 @@ func withParams(h http.Handler) http.Handler {
// Add repo suffix (.git)
r.URL.Path = fmt.Sprintf("%s.git/%s", repo, vars["file"])
r = mux.SetURLVars(r, vars)
h.ServeHTTP(w, r)

next.ServeHTTP(w, r)
})
}

Expand All @@ -111,7 +112,7 @@ func GitController(_ context.Context, r *mux.Router) {
}

// Handle go-get
r.Handle(basePrefix, withParams(withAccess(GoGetHandler{}))).Methods(http.MethodGet)
r.Handle(basePrefix, withParams(withAccess(http.HandlerFunc(GoGetHandler)))).Methods(http.MethodGet)
}

var gitRoutes = []GitRoute{
Expand Down
6 changes: 1 addition & 5 deletions pkg/web/goget.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ Redirecting to docs at <a href="https://godoc.org/{{ .ImportRoot }}/{{ .Repo }}"
`))

// GoGetHandler handles go get requests.
type GoGetHandler struct{}

var _ http.Handler = (*GoGetHandler)(nil)

func (g GoGetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func GoGetHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cfg := config.FromContext(ctx)
be := backend.FromContext(ctx)
Expand Down
5 changes: 5 additions & 0 deletions pkg/web/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func (r *logWriter) WriteHeader(code int) {
r.ResponseWriter.WriteHeader(code)
}

// Unwrap returns the underlying http.ResponseWriter.
func (r *logWriter) Unwrap() http.ResponseWriter {
return r.ResponseWriter
}

// Flush implements http.Flusher.
func (r *logWriter) Flush() {
if f, ok := r.ResponseWriter.(http.Flusher); ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/web/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func renderStatus(code int) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, fmt.Sprintf("%d %s", code, http.StatusText(code))) // nolint: errcheck
w.WriteHeader(code)
io.WriteString(w, fmt.Sprintf("%d %s", code, http.StatusText(code))) // nolint: errcheck
}
}

0 comments on commit 63786d0

Please sign in to comment.