Skip to content

Instantly share code, notes, and snippets.

@tyjak
Created March 14, 2025 21:23
Show Gist options
  • Save tyjak/fc9b3cfc6d616fdc5df1718729d44d0f to your computer and use it in GitHub Desktop.
Save tyjak/fc9b3cfc6d616fdc5df1718729d44d0f to your computer and use it in GitHub Desktop.
package bodyreplace
import (
"bytes"
"io"
"net/http"
"regexp"
)
type Config struct {
Search string `json:"search,omitempty"`
Replace string `json:"replace,omitempty"`
}
func CreateConfig() *Config {
return &Config{}
}
type BodyReplace struct {
next http.Handler
name string
search *regexp.Regexp
replace string
}
func New(next http.Handler, config *Config, name string) (http.Handler, error) {
re, err := regexp.Compile(config.Search)
if err != nil {
return nil, err
}
return &BodyReplace{
next: next,
name: name,
search: re,
replace: config.Replace,
}, nil
}
func (br *BodyReplace) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
buffer := &bytes.Buffer{}
writer := &responseWriter{ResponseWriter: rw, buffer: buffer}
br.next.ServeHTTP(writer, req)
modifiedBody := br.search.ReplaceAllString(buffer.String(), br.replace)
rw.WriteHeader(writer.statusCode)
_, _ = rw.Write([]byte(modifiedBody))
}
type responseWriter struct {
http.ResponseWriter
buffer *bytes.Buffer
statusCode int
}
func (rw *responseWriter) Write(b []byte) (int, error) {
return rw.buffer.Write(b)
}
func (rw *responseWriter) WriteHeader(statusCode int) {
rw.statusCode = statusCode
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment