From e00947806f57e76415762263f13bae237e649920 Mon Sep 17 00:00:00 2001 From: monastyr Date: Wed, 14 Nov 2018 00:19:38 +0100 Subject: [PATCH] pierwszy web serwer, struktura, urle --- backend/cw1.go | 26 ++++++++++++++++++++++++++ backend/duplication.go | 27 +++++++++++++++++++++++++++ backend/echo1.go | 16 ++++++++++++++++ backend/echo2.go | 20 ++++++++++++++++++++ backend/homepage.html | 10 ++++++++++ backend/serverTest.go | 15 +++++++++++++++ backend/serverTest2.go | 36 ++++++++++++++++++++++++++++++++++++ backend/tut1.go | 39 +++++++++++++++++++++++++++++++++++++++ backend/web1.go | 20 ++++++++++++++++++++ 9 files changed, 209 insertions(+) create mode 100644 backend/cw1.go create mode 100644 backend/duplication.go create mode 100644 backend/echo1.go create mode 100644 backend/echo2.go create mode 100644 backend/homepage.html create mode 100644 backend/serverTest.go create mode 100644 backend/serverTest2.go create mode 100644 backend/tut1.go create mode 100644 backend/web1.go diff --git a/backend/cw1.go b/backend/cw1.go new file mode 100644 index 0000000..3f5d7ab --- /dev/null +++ b/backend/cw1.go @@ -0,0 +1,26 @@ +//cwiczenie 1 str 23 + +package main + +import ( + "fmt" + "os" + "strings" + "time" +) + +func main() { + + start1 := time.Now() + for i := 0; i < len(os.Args); i++ { + fmt.Println(i, " ", os.Args[i]) + } + end1 := time.Now() + fmt.Println("Koniec1", end1.Sub(start1)) + + start2 := time.Now() + fmt.Println(strings.Join(os.Args, " ")) + end2 := time.Now() + fmt.Println("Koniec2", end2.Sub(start2)) + +} diff --git a/backend/duplication.go b/backend/duplication.go new file mode 100644 index 0000000..7948230 --- /dev/null +++ b/backend/duplication.go @@ -0,0 +1,27 @@ +// wyswietlanie tekstu kazdej linii która pojawiła się na IO +package main + +import ( + "bufio" + "fmt" + "os" +) + +func main() { + counts := make(map[string]int) + input := bufio.NewScanner(os.Stdin) + + for input.Scan() { + counts[input.Text()]++ + // fmt.Println(counts) + } + // //UWAGA: ignorowowanie potencjalnych błędów z funkcji input.Err() + for line, n := range counts { + // z jakiegos powodu + if n > 1 { + fmt.Printf("%d\t%s\n", n, line) + // fmt.Printf("%d", n) + } + } + +} diff --git a/backend/echo1.go b/backend/echo1.go new file mode 100644 index 0000000..c3da3ed --- /dev/null +++ b/backend/echo1.go @@ -0,0 +1,16 @@ +//Echo1 wyświetla swoje argumenty wiersza poleceń +package main + +import( + "fmt" + "os" +) +func main(){ + var s, sep string + for i :=1; i < len(os.Args); i++ { + s += sep + os.Args[i] + sep = "\n" + } + fmt.Println(s) + +} \ No newline at end of file diff --git a/backend/echo2.go b/backend/echo2.go new file mode 100644 index 0000000..7a10fc9 --- /dev/null +++ b/backend/echo2.go @@ -0,0 +1,20 @@ +//Echo2 wyświetla swoje argumenty wiersza poleceń +package main + +import ( + "fmt" + "os" +) + +func main() { + s, sep := "", "" + for _, arg := range os.Args { //taki foreach + fmt.Println(arg) + s += sep + arg //do zmiennej arg przypisana jest lista + sep = " " + } + fmt.Println(s) + // fmt.Println(os.Args) + // fmt.Println(strings.Join(os.Args, " ")) + +} diff --git a/backend/homepage.html b/backend/homepage.html new file mode 100644 index 0000000..44bb8cc --- /dev/null +++ b/backend/homepage.html @@ -0,0 +1,10 @@ + + + +My Page + + +

The date today is {{.Date}}

+

And the time is {{.Time}}

+ + \ No newline at end of file diff --git a/backend/serverTest.go b/backend/serverTest.go new file mode 100644 index 0000000..62bc4fb --- /dev/null +++ b/backend/serverTest.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "net/http" +) + +func helloWorld(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World") +} + +func main() { + http.HandleFunc("/", helloWorld) + http.ListenAndServe(":8080", nil) +} diff --git a/backend/serverTest2.go b/backend/serverTest2.go new file mode 100644 index 0000000..698e302 --- /dev/null +++ b/backend/serverTest2.go @@ -0,0 +1,36 @@ +package main + +import ( + "html/template" + "log" + "net/http" + "time" +) + +type PageVariables struct { + Date string + Time string +} + +func main() { + http.HandleFunc("/", HomePage) + log.Fatal(http.ListenAndServe(":8081", nil)) +} + +func HomePage(w http.ResponseWriter, r *http.Request) { + + now := time.Now() // find the time right now + HomePageVars := PageVariables{ //store the date and time in a struct + Date: now.Format("02-01-2006"), + Time: now.Format("15:04:05"), + } + + t, err := template.ParseFiles("homepage.html") //parse the html file homepage.html + if err != nil { // if there is an error + log.Print("template parsing error: ", err) // log it + } + err = t.Execute(w, HomePageVars) //execute the template and pass it the HomePageVars struct to fill in the gaps + if err != nil { // if there is an error + log.Print("template executing error: ", err) //log it + } +} diff --git a/backend/tut1.go b/backend/tut1.go new file mode 100644 index 0000000..ef93510 --- /dev/null +++ b/backend/tut1.go @@ -0,0 +1,39 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +//struktura artykuly +type Article struct { + Title string `json:"Title"` + Desc string `json:"desc"` + Content string `json:"content"` +} + +type Articles []Article + +func allArticles(w http.ResponseWriter, r *http.Request) { + articles := Articles{ + Article{Title: "Test Title", Desc: "Test Description", Content: "Hello World"}, //json i szesceinne nawiasy!, przecinek na koniec + } + fmt.Println("Endpoint Hit: All Articles Endpoint") + json.NewEncoder(w).Encode(articles) +} + +func homePage(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Homepage Enpoint Hit") +} + +func handleRequests() { + http.HandleFunc("/", homePage) + http.HandleFunc("/articles", allArticles) + log.Fatal(http.ListenAndServe(":8081", nil)) +} + +func main() { + handleRequests() +} diff --git a/backend/web1.go b/backend/web1.go new file mode 100644 index 0000000..42e885f --- /dev/null +++ b/backend/web1.go @@ -0,0 +1,20 @@ +package main + +import ( + "net/http" +) + +func main() { + mux := http.NewServeMux() + files := http.FileServer(http.Dir("public")) + mux.Handle("static/", http.StripPrefix("static", files)) + + mux.HandleFunc("/") + + server := &http.Server{ + Addr: "0.0.0.0:808", + Handler: mux, + } + + server.ListenAndServe() +}