This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
server.go
69 lines (59 loc) · 1.64 KB
/
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
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
//
// Copyright (c) 2017 Cavium
//
// SPDX-License-Identifier: Apache-2.0
//
package distro
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/edgexfoundry/export-go"
"github.com/go-zoo/bone"
"go.uber.org/zap"
)
func replyPing(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/text; charset=utf-8")
w.WriteHeader(http.StatusOK)
str := `pong`
io.WriteString(w, str)
}
func replyNotifyRegistrations(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Error("Failed read body", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, err.Error())
return
}
update := export.NotifyUpdate{}
if err := json.Unmarshal(data, &update); err != nil {
logger.Error("Failed to parse", zap.ByteString("json", data))
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, err.Error())
return
}
if update.Name == "" || update.Operation == "" {
logger.Error("Missing json field", zap.Any("update", update))
w.WriteHeader(http.StatusBadRequest)
return
}
if update.Operation != export.NotifyUpdateAdd &&
update.Operation != export.NotifyUpdateUpdate &&
update.Operation != export.NotifyUpdateDelete {
logger.Error("Invalid value for operation",
zap.String("operation", update.Operation))
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
RefreshRegistrations(update)
}
// HTTPServer function
func httpServer() http.Handler {
mux := bone.New()
mux.Get("/api/v1/ping", http.HandlerFunc(replyPing))
mux.Put("/api/v1/notify/registrations", http.HandlerFunc(replyNotifyRegistrations))
return mux
}