在 Gorilla mux 处理之后运行中间件。

huangapple go评论90阅读模式
英文:

Run middleware after Gorilla mux handling

问题

我正在尝试使用Golang中间件在处理Gorilla Mux路由之后运行,以便更改每个请求的响应。

以下是代码示例。目前它返回'run before, run test',目标是使用runsafter中间件返回'run before, run test, run after'。

在Gorilla Mux中是否可能实现这样的功能?

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

func runsbefore(h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("run before, "))
		h.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

func runsafter(h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("run after, "))
		h.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

func runtest(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("run test, "))
}

func main() {
	fmt.Println("Server starting on port 8000")
	r := mux.NewRouter()
	r.HandleFunc("/", runtest).Methods("GET")
	http.ListenAndServe(":8000", runsbefore(r))
	// something like: http.ListenAndServe(":8000", runsbefore(r(runsafter)))
}

请注意,我只是提供了代码的翻译,没有回答你是否可以在Gorilla Mux中实现这样的功能。

英文:

I'm trying to use Golang middleware to run after handling Gorilla Mux routing, in order to change the response of each request.

Code sample see below. Currently it returns 'run before, run test', goal is to return 'run before, run test, run after' using the runsafter middleware.

Is something like this possible with Gorilla Mux?

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

func runsbefore(h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("run before, "))
		h.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

func runsafter(h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("run after, "))
		h.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

func runtest(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("run test, "))
}

func main() {
	fmt.Println("Server starting on port 8000")
	r := mux.NewRouter()
	r.HandleFunc("/", runtest).Methods("GET")
	http.ListenAndServe(":8000", runsbefore(r))
	// something like: http.ListenAndServe(":8000", runsbefore(r(runsafter)))
}

答案1

得分: 3

使用以下代码并修复runsAfter中的错误:

http.ListenAndServe(":8000", runsAfter(runsBefore(r)))

func runsAfter(h http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
    h.ServeHTTP(w, r)
    w.Write([]byte("run after, "))
  }

  return http.HandlerFunc(fn)
}

请注意,我已经将代码中的runsafter更正为runsAfter,并将run after,更正为run after, ,以便与原始代码保持一致。

英文:

Use

http.ListenAndServe(":8000", runsAfter(runsbefore(r)))

and fix the error in runsAfter:

func runsafter(h http.Handler) http.Handler {
  fn := func(w http.ResponseWriter, r *http.Request) {
    h.ServeHTTP(w, r)
    w.Write([]byte("run after, "))
  }

  return http.HandlerFunc(fn)
}

huangapple
  • 本文由 发表于 2017年5月15日 17:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/43976539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定