-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.go
44 lines (37 loc) · 868 Bytes
/
server.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
package rtfdoc
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func rtfHandler(w http.ResponseWriter, r *http.Request) {
// Here we'll be processing reauest body
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("error reading body: %v", err)
}
// Parse body as json
var document []map[string]interface{}
err = json.Unmarshal(body, &document)
if err != nil {
log.Fatalf("can't unmarshal body: %v", err)
}
// for _, docItem := range document {
// }
fmt.Println(string(body))
} else {
_, _ = w.Write([]byte("Method is not applicable"))
}
return
}
// RunServer run listener server for doc generate
func RunServer(port int) {
http.HandleFunc("/generate_rtf_doc", rtfHandler)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
log.Fatal(err)
}
}