forked from WEB-IB-SS26/development-ib
07: Demos
parent
277cd15541
commit
6f4035aef8
|
|
@ -0,0 +1,26 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wert-Receiver: arbeitet auf einer Kopie
|
||||||
|
func (p Person) Greet() {
|
||||||
|
fmt.Println("Hallo,", p.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zeiger-Receiver: kann das Original ändern
|
||||||
|
func (p *Person) Birthday() {
|
||||||
|
p.Age++
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := Person{Name: "Karl", Age: 30}
|
||||||
|
|
||||||
|
a.Greet() // Ausgabe: Hallo, Karl
|
||||||
|
a.Birthday() // Alter wird erhöht
|
||||||
|
fmt.Println(a.Age) // Ausgabe: 31
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type helloHandler int
|
||||||
|
|
||||||
|
func (hello helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "Hello World %d!", hello)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var world helloHandler
|
||||||
|
world = 42
|
||||||
|
http.ListenAndServe("localhost:8080", world)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type respExampleHandler int
|
||||||
|
|
||||||
|
func (hello respExampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("my-http-header", "Ein beliebiger Text")
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusFound)
|
||||||
|
w.Write([]byte("<h1>Dies ist eine HTML-Überschrift<h1>"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var r respExampleHandler
|
||||||
|
http.ListenAndServe("localhost:8080", r)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type reqExampleHandler int
|
||||||
|
|
||||||
|
func (hello reqExampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Only GET requests are allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Method:", r.Method)
|
||||||
|
fmt.Println("URL:", r.URL.String())
|
||||||
|
fmt.Println("Host:", r.Host)
|
||||||
|
fmt.Println("Accept-Header", r.Header.Get("Accept"))
|
||||||
|
|
||||||
|
queryParams := r.URL.Query()
|
||||||
|
fmt.Println("Query Parameters:")
|
||||||
|
for key, values := range queryParams {
|
||||||
|
for _, value := range values {
|
||||||
|
fmt.Printf(" %s = %s\n", key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte("<h1>GET: http.Request-Beispiel<h1>"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var r reqExampleHandler
|
||||||
|
http.ListenAndServe("localhost:8080", r)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type formExampleHandler int
|
||||||
|
|
||||||
|
func (formHandler formExampleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Only POST requests are allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Formulardaten parsen und Überprüfen, ob ungültige Formulardaten gesendet wurde
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, "Fehler beim Parsen des Formulars", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Überprüfen, ob leeres Formular gesendet wurde
|
||||||
|
if len(r.PostForm) == 0 {
|
||||||
|
http.Error(w, "Kein POST-Formular gesendet", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zugriff auf PostForm-Daten
|
||||||
|
// string mit Wert des Feldes "name"
|
||||||
|
name := r.PostForm.Get("name")
|
||||||
|
// []string mit allen ausgewählten Werten der Checkbox "hobby"
|
||||||
|
hobbies := r.PostForm["hobby"]
|
||||||
|
|
||||||
|
// Ausgabe der Werte
|
||||||
|
fmt.Fprintf(w, "Name: %s\n", name)
|
||||||
|
fmt.Fprintf(w, "Hobbies:\n")
|
||||||
|
for _, h := range hobbies {
|
||||||
|
fmt.Fprintf(w, "- %s\n", h)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var r formExampleHandler
|
||||||
|
http.ListenAndServe("localhost:8080", r)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsonEncodeHandler int
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Firstname string `json:"firstname"`
|
||||||
|
Lastname string `json:"lastname"`
|
||||||
|
Age int `json:"age"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jsonHandler jsonEncodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mariam := User{
|
||||||
|
Firstname: "Mariam",
|
||||||
|
Lastname: "Okonkwo",
|
||||||
|
Age: 25,
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(mariam)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var enc jsonEncodeHandler
|
||||||
|
http.ListenAndServe("localhost:8080", enc)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsonDecodeHandler int
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Firstname string `json:"firstname"`
|
||||||
|
Lastname string `json:"lastname"`
|
||||||
|
Age int `json:"age"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jsonHandler jsonDecodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var user User
|
||||||
|
json.NewDecoder(r.Body).Decode(&user)
|
||||||
|
fmt.Fprintf(w, "%s %s is %d years old!", user.Firstname, user.Lastname, user.Age)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var dec jsonDecodeHandler
|
||||||
|
http.ListenAndServe("localhost:8080", dec)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Einfaches Formular für Backend</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<form method="POST" action="http://localhost:8080">
|
||||||
|
<label for="nameLabel">Name</label>
|
||||||
|
<input name="name" id="nameLabel" />
|
||||||
|
<p>Hobbies:
|
||||||
|
<label><input type="checkbox" name="hobby" value="lesen"> Lesen</label>
|
||||||
|
<label><input type="checkbox" name="hobby" value="kochen"> Kochen</label>
|
||||||
|
<label><input type="checkbox" name="hobby" value="wandern"> Wandern</label>
|
||||||
|
</p>
|
||||||
|
<button type="submit">Absenden</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue