/
restHello.go
48 lines (44 loc) · 1.08 KB
/
restHello.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
47
48
package main
import (
"fmt"
"httprouter"
"net/http"
"encoding/json"
"log"
)
type RequestData struct{
Name string
}
type ResponseData struct{
Greetings string
}
func hello(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
requestData := new(RequestData)
responseData := new(ResponseData)
decoder := json.NewDecoder(req.Body)
error := decoder.Decode(&requestData)
if error != nil {
log.Println(error.Error())
http.Error(rw, error.Error(), http.StatusInternalServerError)
return
}
responseData.Greetings = "Hello," + requestData.Name + "!"
outgoingJSON, err := json.Marshal(responseData)
if err != nil {
log.Println(error.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, string(outgoingJSON))
}
func main() {
mux := httprouter.New()
mux.POST("/hello", hello)
server := http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
server.ListenAndServe()
}