-
Notifications
You must be signed in to change notification settings - Fork 1
/
owntracks.go
77 lines (68 loc) · 2.11 KB
/
owntracks.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
70
71
72
73
74
75
76
77
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
type owntracksStore interface {
// AddOTLocation persists the message, of type location
AddOTLocation(context.Context, owntracksMessage) error
}
// owntracksServer can handle receiving and persisting owntracks events from a
// phone
//
// https://owntracks.org/booklet/tech/http/
type owntracksServer struct {
log logger
store owntracksStore
}
func (o *owntracksServer) HandlePublish(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "bad method", http.StatusMethodNotAllowed)
return
}
if r.ContentLength == 0 {
// in some cases (deleting friends) a 0 length message can be
// intentionally sent. (ref:
// https://github.com/owntracks/ios/issues/580#issuecomment-495276821).
// If we get one of these simply do nothing, as there is nothing to
// deserialize and no known action we can take
o.log.Print("skipping publish empty body")
return
}
// parse message
msg := owntracksMessage{}
rawMsg, err := io.ReadAll(r.Body)
if err != nil {
metricOTSubmitErrorCount.Inc()
o.log.Printf("read owntracks message: %v", err)
http.Error(w, fmt.Sprintf("read owntracks message: %v", err), http.StatusInternalServerError)
return
}
if err := json.Unmarshal(rawMsg, &msg); err != nil {
metricOTSubmitErrorCount.Inc()
o.log.Printf("decoding owntracks message (%s): %v", string(rawMsg), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// ignore if not location
if !msg.IsLocation() {
o.log.Printf("ignoring payload type %s", msg.Type)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `[]`)
return
}
if err := o.store.AddOTLocation(r.Context(), msg); err != nil {
metricOTSubmitErrorCount.Inc()
o.log.Printf("persisting device location: %v", err)
http.Error(w, fmt.Sprintf("error: %s", err), http.StatusInternalServerError)
return
}
// return empty json array. Eventually we should support reading/publishing
// from a MQTT endpoint
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `[]`)
metricOTSubmitSuccessCount.Inc()
}