From 3b90b8208270f3f0bbbe8c2848b77bc386dda3f0 Mon Sep 17 00:00:00 2001 From: sashachernyshova Date: Mon, 8 Oct 2018 17:24:00 +0300 Subject: [PATCH] Add file --- server.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server.go diff --git a/server.go b/server.go new file mode 100644 index 0000000..762b013 --- /dev/null +++ b/server.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "strconv" + "strings" + "time" +) + +func HomeRouterHandler(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + fmt.Println(r.Form) + fmt.Println("path", r.URL.Path) + fmt.Println("scheme", r.URL.Scheme) + fmt.Println(r.Form["url_long"]) + for k, v := range r.Form { + fmt.Println("key:", k) + fmt.Println("val:", strings.Join(v, "")) + } + fmt.Fprintln(w, "Hello Sasha!") + fmt.Fprintln(w, "Try new features: Current Date && Delay!") +} + +func main() { + http.HandleFunc("/", HomeRouterHandler) + http.HandleFunc("/date", CurrDate) + http.HandleFunc("/wait", Delay) + err := http.ListenAndServe(":9000", nil) + if err != nil { + log.Fatal("ListenAndServe: ", err) + } +} + +func Delay(w http.ResponseWriter, r *http.Request){ + value := r.URL.Query().Get("delay") + if value != ""{ + SleepTime, err := strconv.Atoi(value) + if err ==nil { + time.Sleep(time.Duration(SleepTime) * time.Millisecond) + fmt.Fprintln(w, "Sleep for " +value+ "ms completed") + }else{ + fmt.Fprint(w,"Error. Write another duration of delay.") + } + } +} + + +func CurrDate(w http.ResponseWriter, r* http.Request) { + fmt.Fprintf(w, "Current date: %s", time.Now()) +} \ No newline at end of file