-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
72 lines (64 loc) · 1.86 KB
/
endpoint.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
package services
import (
"encoding/json"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
type suntimez struct {
Photo int `json:"photo"`
Sunrise int64 `json:"sunrise"`
Sunset int64 `json:"sunset"`
NowUnix int64 `json:"now_unix"`
SunriseStr string `json:"sunrise_str"`
SunsetStr string `json:"sunset_str"`
}
type WebEndpoint struct {
todayService *Today
}
func NewWebEndpoint(tService *Today) *WebEndpoint {
return &WebEndpoint{
todayService: tService,
}
}
func (we *WebEndpoint) StartWebHandler() {
mux := http.NewServeMux()
mux.HandleFunc("/phototimez", we.handler)
go we.listenerRoutine(mux)
}
func (we *WebEndpoint) listenerRoutine(mux *http.ServeMux) {
if err := http.ListenAndServe(":5000", mux); err != nil {
logrus.Errorf("endpoint listener failed: %v", err)
}
}
func (we *WebEndpoint) handler(w http.ResponseWriter, r *http.Request) {
now := time.Now()
sunrisePre := we.todayService.GetSunrise() - 1800
sunsetPost := we.todayService.GetSunset() + 1800
resp := 0
if now.Unix() >= sunrisePre && now.Unix() <= sunsetPost {
resp = 1
}
// since sometimes the light lingers longer than 30 min after sunset
// use the color profile of the last photo to extend photo hours as
// necessary
if now.Unix() > sunsetPost && we.todayService.GetDarkPercent() < 95.0 {
logrus.Infof("Extending photo hours; still some light (%f) at %v", we.todayService.GetDarkPercent(), now)
resp = 1
}
riseTime := time.Unix(we.todayService.GetSunrise(), 0)
setTime := time.Unix(we.todayService.GetSunset(), 0)
sresp := suntimez{
Photo: resp,
Sunrise: we.todayService.GetSunrise(),
Sunset: we.todayService.GetSunset(),
NowUnix: now.Unix(),
SunriseStr: riseTime.String(),
SunsetStr: setTime.String(),
}
b, err := json.Marshal(sresp)
if err != nil {
logrus.Errorf("can't marshal suntimes JSON: %v", err)
}
w.Write(b)
}