/u/point initial

This commit is contained in:
Denis Zheleztsov 2018-11-04 16:11:37 +03:00
parent 9c23daf783
commit 915fc24cd4
4 changed files with 66 additions and 2 deletions

View File

@ -1,11 +1,14 @@
package node package node
import ( import (
"log" "fmt"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -28,7 +31,7 @@ func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) {
// XFeaturesHandler list supported features // XFeaturesHandler list supported features
func XFeaturesHandler(w http.ResponseWriter, r *http.Request) { 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) LogRequest(r)
@ -135,6 +138,34 @@ func (es ESConf) XCHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(strings.Join(counts, "\n"))) 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 ... // Serve ...
func Serve(listen string, es ESConf) { func Serve(listen string, es ESConf) {
r := mux.NewRouter() 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("/u/m/{ids:[a-zA-Z0-9-_/.:]+}", es.UMHandler).Methods("GET")
r.HandleFunc("/x/c/{echoes:[a-zA-Z0-9-_/.:]+}", es.XCHandler).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) http.Handle("/", r)
srv := http.Server{ srv := http.Server{

7
node/auth.go Normal file
View File

@ -0,0 +1,7 @@
package node
// checkAuth token in point request
// TODO: implement logic
func (es ESConf) checkAuth(req PointRequest) bool {
return true
}

17
node/point.go Normal file
View File

@ -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
}

View File

@ -2,6 +2,12 @@ package node
import "gitea.difrex.ru/Umbrella/fetcher/i2es" import "gitea.difrex.ru/Umbrella/fetcher/i2es"
// PointRequest with message
type PointRequest struct {
Pauth string `json:"pauth"`
Tmsg string `json:"tmsg"`
}
// ESConf ... // ESConf ...
type ESConf i2es.ESConf type ESConf i2es.ESConf