-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
46 lines (38 loc) · 906 Bytes
/
handlers.go
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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func RootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Root path")
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Home path")
}
func PostRequest(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var metadata MetaData
err := decoder.Decode(&metadata)
if err != nil {
fmt.Fprintf(w, "error: %v", err)
return
}
fmt.Fprintf(w, "Payload %v\n", metadata)
}
func UserPostRequest(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var user User
err := decoder.Decode(&user)
if err != nil {
fmt.Fprintf(w, "error: %v", err)
return
}
response, jsonErr := user.ToJson()
if jsonErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}