This repository has been archived on 2021-09-07. You can view files and clone it, but cannot push or open issues or pull requests.
2018-01-23 23:12:14 +03:00

32 lines
634 B
Go

package decorator
import (
"os"
"io"
"fmt"
"net/http"
)
// simple http server
type SimpleServer struct{
msg string
}
func (s *SimpleServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, s.msg)
}
// log decorator for simple server
type ServerWithLog struct {
handler http.Handler
logger io.Writer
}
func (s *ServerWithLog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(s.logger, "Got request with uri: %s \n", r.RequestURI)
s.handler.ServeHTTP(w, r)
}
func Demo(){
http.Handle("/", &ServerWithLog{&SimpleServer{"Simple server"}, os.Stdout})
http.ListenAndServe(":3000", nil)
}