-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhello.go
More file actions
73 lines (57 loc) · 1.71 KB
/
hello.go
File metadata and controls
73 lines (57 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package handler
import (
"encoding/json"
"fmt"
"log/slog"
"mime"
"net/http"
"github.com/1995parham-teaching/go-lecture/httpserver/request"
)
type Hello struct {
From string
Logger *slog.Logger
}
func (h Hello) User(w http.ResponseWriter, r *http.Request) {
value := r.PathValue("username")
h.Logger.Info("read username from path parameter", "username", value)
w.WriteHeader(http.StatusNoContent)
}
func (h Hello) Get(w http.ResponseWriter, r *http.Request) {
if value := r.FormValue("hello"); value != "" {
h.Logger.Info("read hello from query parameter", "hello", value)
}
enc, err := json.Marshal(fmt.Sprintf("Hello World from %s", h.From))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(enc)
}
func (h Hello) Post(w http.ResponseWriter, r *http.Request) {
// Parse the media type so we accept e.g. "application/json; charset=utf-8"
// instead of only the bare "application/json".
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil || mediaType != "application/json" {
http.Error(w, "expected Content-Type: application/json", http.StatusBadRequest)
return
}
var req request.Name
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if req.Count == nil {
h.Logger.Info("There is no count")
} else {
h.Logger.Info("There is a count", "count", *req.Count)
}
enc, err := json.Marshal(fmt.Sprintf("Hello to %s from %s", req.Name, h.From))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(enc)
}