-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving utility functions shared between httpproxy and smtp to dedicat…
…ed package
- Loading branch information
Lucas Hinderberger
committed
Jun 24, 2024
1 parent
878c291
commit e3b244f
Showing
4 changed files
with
58 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package handlerutil | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// errorResponse definition | ||
type errorResponse struct { | ||
Error string `json:"error"` | ||
} | ||
|
||
// LogH is a middleware that logs requests via logrus.Debugf. | ||
func LogH(skipLogs bool, next http.Handler) http.Handler { | ||
if skipLogs { | ||
return next | ||
} | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
logrus.Debugf("http-server: %s: %q", r.Method, r.URL) | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
// RespondWithErr responds using a JSON-serialized error message. | ||
func RespondWithErr(w http.ResponseWriter, status int, err error) { | ||
RespondWithJSON(w, status, errorResponse{err.Error()}) | ||
} | ||
|
||
// RespondWithJSON responds with a JSON-serialized value. | ||
func RespondWithJSON(w http.ResponseWriter, status int, v any) { | ||
w.WriteHeader(status) | ||
|
||
err := json.NewEncoder(w).Encode(v) | ||
if err != nil { | ||
logrus.Errorf("Could not encode JSON response: %s (%v)", err, v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters