Help us shape the future of Go! Take the 2026 Go Developer Survey

rewrite

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2022 License: MIT Imports: 4 Imported by: 1

README

pkg rewrite

pkg rewrite provides the http.ResponseWriter implementation and Replacer of the response.

Installation

go get -u github.com/Qs-F/rewrite

Example

func main() {
  rw := &Rule{
    {
      Old: "world",
      New: "gopher",
    },
  }
  http.Handle("/", rw.MapFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello world")
  }))
  http.ListenAndServe(":8080", nil)
}
// Open localhost:8080, then you will get "hello gopher"

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Replace

type Replace struct {
	Old string
	New string
}

Replace is struct consisting of Old and New string

type ResponseWrite

type ResponseWrite struct {
	// contains filtered or unexported fields
}

ResponseWrite is http.ResponseWriter interface implementation.

func NewResponseWrite

func NewResponseWrite(w http.ResponseWriter, rw Rewriter) *ResponseWrite

func (*ResponseWrite) Header

func (w *ResponseWrite) Header() http.Header

func (*ResponseWrite) Write

func (w *ResponseWrite) Write(p []byte) (int, error)

Write writes content which is returned from Rewriter to p.

func (*ResponseWrite) WriteHeader

func (w *ResponseWrite) WriteHeader(statusCode int)

type Rewriter

type Rewriter interface {
	Rewrite(p []byte) []byte
}

Rewriter interface is used to rewrite content based on original content.

type Rule

type Rule []*Replace

Rule is slice of Replace

func (*Rule) Map

func (rl *Rule) Map(h http.Handler) http.Handler

Map provides wrapper of http.Handler. Map returns http.Handler. By passing http.Handler, Map autonatically rewrite the content which handler write out.

func (*Rule) MapFunc

func (rl *Rule) MapFunc(hf http.HandlerFunc) http.Handler

MapFunc provides wrapper of http.HandlerFunc. MapFunc returns http.Handler. By passing http.Handler, Map autonatically rewrite the content which handler write out.

func (*Rule) Rewrite

func (rl *Rule) Rewrite(p []byte) []byte

Rewrite is implementation of Rewriter. If the content is binary, Rewrite returns original content.

func (*Rule) ToReplacer

func (rl *Rule) ToReplacer() *strings.Replacer

Replacer returns strings.Replacer by converting from Rule to strings.Replacer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL