From 915fc24cd4fd70fe9ed1c69e8ba08ea55370b721 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Sun, 4 Nov 2018 16:11:37 +0300 Subject: [PATCH] /u/point initial --- node/api.go | 38 ++++++++++++++++++++++++++++++++++++-- node/auth.go | 7 +++++++ node/point.go | 17 +++++++++++++++++ node/structs.go | 6 ++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 node/auth.go create mode 100644 node/point.go diff --git a/node/api.go b/node/api.go index e2de149..b8a8dbf 100644 --- a/node/api.go +++ b/node/api.go @@ -1,11 +1,14 @@ package node import ( - "log" + "fmt" "net/http" "strings" "time" + "encoding/json" + + log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" ) @@ -28,7 +31,7 @@ func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) { // XFeaturesHandler list supported features func XFeaturesHandler(w http.ResponseWriter, r *http.Request) { - features := []string{"list.txt", "u/e", "u/m"} + features := []string{"list.txt", "u/e", "u/m", "x/c"} LogRequest(r) @@ -135,6 +138,34 @@ func (es ESConf) XCHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte(strings.Join(counts, "\n"))) } +// UPointHandler /u/point scheme +func (es ESConf) UPointHandler(w http.ResponseWriter, r *http.Request) { + var req PointRequest + LogRequest(r) + + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + w.WriteHeader(400) + w.Write([]byte(fmt.Sprintf("error: %s", err.Error()))) + return + } + + // Authorization check + if !es.checkAuth(req) { + w.WriteHeader(403) + w.Write([]byte("Permission denied")) + return + } + + // Proccess point message + err = es.PointMessage(req) + if err != nil { + log.Error(err.Error()) + w.WriteHeader(500) + return + } +} + // Serve ... func Serve(listen string, es ESConf) { r := mux.NewRouter() @@ -150,6 +181,9 @@ func Serve(listen string, es ESConf) { r.HandleFunc("/u/m/{ids:[a-zA-Z0-9-_/.:]+}", es.UMHandler).Methods("GET") r.HandleFunc("/x/c/{echoes:[a-zA-Z0-9-_/.:]+}", es.XCHandler).Methods("GET") + // Point methods + r.HandleFunc("/u/point", es.UPointHandler).Methods("POST") + http.Handle("/", r) srv := http.Server{ diff --git a/node/auth.go b/node/auth.go new file mode 100644 index 0000000..f0cc0ef --- /dev/null +++ b/node/auth.go @@ -0,0 +1,7 @@ +package node + +// checkAuth token in point request +// TODO: implement logic +func (es ESConf) checkAuth(req PointRequest) bool { + return true +} diff --git a/node/point.go b/node/point.go new file mode 100644 index 0000000..4a0884a --- /dev/null +++ b/node/point.go @@ -0,0 +1,17 @@ +package node + +import ( + log "github.com/Sirupsen/logrus" + "github.com/idec-net/go-idec" +) + +// PointMessage add point message into DB +func (es ESConf) PointMessage(req PointRequest) error { + msg, err := idec.ParsePointMessage(req.Tmsg) + if err != nil { + return err + } + + log.Infof("%+v", msg) + return nil +} diff --git a/node/structs.go b/node/structs.go index 6afef3b..54768d7 100644 --- a/node/structs.go +++ b/node/structs.go @@ -2,6 +2,12 @@ package node import "gitea.difrex.ru/Umbrella/fetcher/i2es" +// PointRequest with message +type PointRequest struct { + Pauth string `json:"pauth"` + Tmsg string `json:"tmsg"` +} + // ESConf ... type ESConf i2es.ESConf