Golang简单静态服务器在终端上打印。

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

golang simple static server print on terminal

问题

这是我的初始 Golang 代码:

  1. package main
  2. import (
  3. "net/http"
  4. "io"
  5. "log"
  6. )
  7. const hello = `hello world`
  8. func helloHandler(w http.ResponseWriter, r *http.Request) {
  9. io.WriteString(w, hello)
  10. }
  11. func logHandler(next http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL.Path)
  14. next.ServeHTTP(w, r)
  15. })
  16. }
  17. func main() {
  18. http.HandleFunc("/", logHandler(helloHandler))
  19. http.ListenAndServe(":1088", nil)
  20. }

这是一个简单的 HTTP 服务器,你想要添加一个新的功能,即每个 GET 请求都在 Linux 终端中打印出 IP、METHOD 和 /request。

你可以通过添加一个名为 logHandler 的中间件来实现这个功能。这个中间件将在处理每个请求之前打印出请求的信息。在 main 函数中,将 logHandler 应用到 helloHandler 上,这样每个请求都会经过 logHandler

希望对你有帮助!

英文:

this is my initial golang code :

  1. package main
  2. import (
  3. "net/http"
  4. "io"
  5. )
  6. const hello = `hello world`
  7. func helloHandler(w http.ResponseWriter, r *http.Request) {
  8. io.WriteString(w, hello)
  9. }
  10. func main() {
  11. http.HandleFunc("/", helloHandler)
  12. http.ListenAndServe(":1088", nil)
  13. }

it is a simple http server, i need add new function, every get request print in linux terminal ip, METHOD, /request.

example output in terminal need:

  1. 95.250.33.36 GET /
  2. 95.250.33.36 GET /favicon.ico
  3. 95.250.33.36 GET /robots.txt

how i can do this ?

答案1

得分: 2

Golang最好的地方就是接口。你的helloHandler实际上实现了HandlerFunc接口。使用开闭原则,我们可以对helloHandler进行扩展,以记录请求的方式如下:

  1. func wrapHandlerWithLogging(wrappedHandler http.Handler) http.HandlerFunc {
  2. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  3. log.Printf("--> %s %s", req.Method, req.URL.Path)
  4. wrappedHandler.ServeHTTP(w, req)
  5. })
  6. }
  7. func main() {
  8. ...
  9. http.HandleFunc("/", wrapHandlerWithLogging(http.HandlerFunc(helloHandler)))
  10. ...
  11. }

所以基本上,我们用另一个HandlerFunc包装了实现HandlerFunchelloHandler

在这个例子中,我们只记录了请求方法(GET、POST、PUT等)和请求路径(例如'/')。然而,你也可以记录其他数据:

  • req.RemoteAddr:发送请求的网络地址
  • req.Proto:协议版本
  • req.Host:指定所请求的主机名
英文:

The best thing about Golang is interfaces.
Your helloHandler actually implements the HandlerFunc interface.
Using the Open/Close Principle we can take helloHandler and extend it for logging the request in the following way:

  1. func wrapHandlerWithLogging(wrappedHandler http.Handler) http.HandlerFunc {
  2. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  3. log.Printf("--> %s %s", req.Method, req.URL.Path)
  4. wrappedHandler.ServeHTTP(w, req)
  5. })
  6. }
  7. func main() {
  8. ...
  9. http.HandleFunc("/", wrapHandlerWithLogging(http.HandlerFunc(helloHandler)))
  10. ...
  11. }

So basically, we wrap helloHandler which implements HandlerFunc with another HandlerFunc.

In this example, we only log the request method (GET, POST, PUT and etc) and the request path (e.g. '/'). However, you can log other data:

  • req.RemoteAddr network address that sent the request
  • req.Proto the protocol version
  • req.Host specifies the host on which URL is sought

huangapple
  • 本文由 发表于 2017年8月28日 06:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/45909745.html
匿名

发表评论

匿名网友

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

确定