Dmitry Telenkov 1 day ago
parent
commit
dd8db56e43
1 changed files with 45 additions and 0 deletions
  1. 45 0
      go/stepik/4/handler.go

+ 45 - 0
go/stepik/4/handler.go

@@ -0,0 +1,45 @@
+package main
+
+import (
+	"log"
+	"net/http"
+	"time"
+)
+
+type timeHandler struct {
+	format string
+}
+
+func (th *timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	tm := time.Now().Format(th.format)
+	w.Write([]byte("The time is: " + tm))
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+	w.Write([]byte("OK!"))
+}
+
+func main() {
+	/*
+		// создаем свой serveMux
+		serveMux := http.NewServeMux()
+		serveMux.HandleFunc("/", handler)
+		// Запускаем веб-сервер на порту 8080 с нашим serveMux
+		// (в прошлых примерах был nil)
+		err := http.ListenAndServe(":8080" serverMux)
+		if err != nil {
+			fmt.Println("Ошибка запуска сервера:", err)
+		}
+	*/
+
+	mux := http.NewServeMux()
+
+	th1123 := &timeHandler{format: time.RFC1123}
+	mux.Handle("/time/rfc1123", th1123)
+
+	th3339 := &timeHandler{format: time.RFC3339}
+	mux.Handle("/time/rfc3339", th3339)
+
+	log.Println("Listening...")
+	http.ListenAndServe(":3000", mux)
+}