2019-05-06 01:40:41 +03:00
|
|
|
package routes
|
2019-05-05 23:36:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
"github.com/n1try/wakapi/services"
|
|
|
|
|
2019-05-05 23:36:49 +03:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/n1try/wakapi/models"
|
|
|
|
)
|
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
type HeartbeatHandler struct {
|
|
|
|
HeartbeatSrvc *services.HeartbeatService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
|
2019-05-05 23:36:49 +03:00
|
|
|
if r.Method != "POST" {
|
|
|
|
w.WriteHeader(415)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
var heartbeats []models.Heartbeat
|
|
|
|
err := dec.Decode(&heartbeats)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
err = h.HeartbeatSrvc.InsertBatch(heartbeats, user)
|
2019-05-05 23:36:49 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
os.Stderr.WriteString(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(201)
|
|
|
|
}
|