pierwszy web serwer, struktura, urle
This commit is contained in:
parent
502ec9a807
commit
e00947806f
26
backend/cw1.go
Normal file
26
backend/cw1.go
Normal file
@ -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))
|
||||
|
||||
}
|
27
backend/duplication.go
Normal file
27
backend/duplication.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
16
backend/echo1.go
Normal file
16
backend/echo1.go
Normal file
@ -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)
|
||||
|
||||
}
|
20
backend/echo2.go
Normal file
20
backend/echo2.go
Normal file
@ -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, " "))
|
||||
|
||||
}
|
10
backend/homepage.html
Normal file
10
backend/homepage.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>The date today is {{.Date}}</p>
|
||||
<p>And the time is {{.Time}}</p>
|
||||
</body>
|
||||
</html>
|
15
backend/serverTest.go
Normal file
15
backend/serverTest.go
Normal file
@ -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)
|
||||
}
|
36
backend/serverTest2.go
Normal file
36
backend/serverTest2.go
Normal file
@ -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
|
||||
}
|
||||
}
|
39
backend/tut1.go
Normal file
39
backend/tut1.go
Normal file
@ -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()
|
||||
}
|
20
backend/web1.go
Normal file
20
backend/web1.go
Normal file
@ -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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user