-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go~
More file actions
38 lines (32 loc) · 733 Bytes
/
main.go~
File metadata and controls
38 lines (32 loc) · 733 Bytes
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
type Post struct {
User string `json:"user"`
Message string `json:"message"`
Location Location `json:"location"`
}
func main() {
fmt.Println("started-service")
http.HandleFunc("/Post", handlerPost)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handlerPost(w http.ResponseWriter, r *http.Request) {
// Parse from body of request to get a json object.
fmt.Println("Received one post request")
decoder := json.NewDecoder(r.Body)
var p Post
if err := decoder.Decode(&p); err != nil {
panic(err)
return
}
fmt.Fprintf(w, "Post received: %s\n", p.Message)
}