-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
288 lines (235 loc) · 6.94 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/paulmach/go.geojson"
"gopkg.in/gcfg.v1"
)
var mainTemplate *template.Template = template.Must(template.ParseFiles("main.html"))
var detailsTemplate *template.Template = template.Must(template.ParseFiles("details.html"))
func main() {
var config Config
// sane default config values:
config.Connection.Host = "localhost"
config.Connection.Port = 5432
config.Connection.User = "teslamate"
config.Connection.DB = "teslamate"
config.Service.Port = 4001
err := gcfg.ReadFileInto(&config, "tesla_journal.cfg")
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read configuration: %v\n", err)
os.Exit(1)
}
err = connectDB(config)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer database.Close()
// start serving requests:
port := strconv.Itoa(config.Service.Port)
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.HandleFunc("/", serveGet).Methods(http.MethodGet)
r.HandleFunc("/", servePost).Methods(http.MethodPost)
r.HandleFunc("/details/{id}", serveDriveDetails).Methods(http.MethodGet)
r.HandleFunc("/drive/{id}", getDriveDetails).Methods(http.MethodGet)
r.HandleFunc("/drive/group/{id}", getGroupDriveDetails).Methods(http.MethodGet)
r.HandleFunc("/groupdetails/{id}", serveGroupedDriveDetails).Methods(http.MethodGet)
r.HandleFunc("/action", postAction).Methods(http.MethodPost)
secure := config.Service.CertFile != "" && config.Service.KeyFile != ""
if secure {
fmt.Println("Listening on secure port " + port)
err = http.ListenAndServeTLS(":"+port, config.Service.CertFile, config.Service.KeyFile, r)
if err != nil {
fmt.Println("Secure mode failed: " + err.Error())
secure = false
}
}
if !secure {
fmt.Println("Listening on non-secure port " + port)
err = http.ListenAndServe(":"+port, r)
}
log.Fatal(err)
}
func getIntParamPost(r *http.Request, param string, into *int) error {
val, err := strconv.Atoi(r.Form.Get(param))
if err != nil {
return err
}
*into = val
return nil
}
func serveGet(w http.ResponseWriter, r *http.Request) {
year := int(time.Now().Year())
month := int(time.Now().Month())
car := 1
data := generateMain(year, month, car)
err := mainTemplate.Execute(w, data)
if err != nil {
log.Fatal("Error while executing template: " + err.Error())
}
}
func servePost(w http.ResponseWriter, r *http.Request) {
year := int(time.Now().Year())
month := int(time.Now().Month())
car := 1
err := r.ParseForm()
if err != nil {
panic(err)
}
getIntParamPost(r, "year", &year)
getIntParamPost(r, "month", &month)
getIntParamPost(r, "car", &car)
data := generateMain(year, month, car)
err = mainTemplate.Execute(w, data)
if err != nil {
log.Fatal("Error while executing template: " + err.Error())
}
}
func getDriveDetails(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var ids []string
ids = append(ids, vars["id"])
positions, err := getPositions(ids)
if err != nil {
log.Println("Error while getting positions")
}
var coordinates [][]float64
for _, pos := range positions {
var c []float64
c = append(c, pos.Longitude)
c = append(c, pos.Latitude)
coordinates = append(coordinates, c)
}
featureCollection := geojson.NewFeatureCollection()
feature := geojson.NewLineStringFeature(coordinates)
featureCollection.AddFeature(feature)
var response GetDriveResponse
response.MapData = *featureCollection
response.Drive, response.Comment, err = getDriveById(vars["id"])
if err != nil {
log.Println("Error getting drive details: " + err.Error())
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func getGroupDriveDetails(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var ids []string
ids = append(ids, vars["id"])
driveIds, err := getDriveIdsForGroups(ids)
if err != nil {
log.Println("Error while getting drive ids for grouped drive")
}
positions, err := getPositions(driveIds)
if err != nil {
log.Println("Error while getting positions")
}
var coordinates [][]float64
for _, pos := range positions {
var c []float64
c = append(c, pos.Longitude)
c = append(c, pos.Latitude)
coordinates = append(coordinates, c)
}
featureCollection := geojson.NewFeatureCollection()
feature := geojson.NewLineStringFeature(coordinates)
featureCollection.AddFeature(feature)
var response GetGroupedDrivesResponse
response.MapData = *featureCollection
response.Drives, err = getGroupedDrivesById(vars["id"])
if err != nil {
log.Println("Error getting drive details: " + err.Error())
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func serveDriveDetails(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var data struct {
Id string
Group bool
}
data.Id = vars["id"]
data.Group = false
err := detailsTemplate.Execute(w, data)
if err != nil {
log.Fatal("Error while executing template: " + err.Error())
}
}
func serveGroupedDriveDetails(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var data struct {
Id string
Group bool
}
data.Id = vars["id"]
data.Group = true
err := detailsTemplate.Execute(w, data)
if err != nil {
log.Fatal("Error while executing template: " + err.Error())
}
}
func postAction(w http.ResponseWriter, r *http.Request) {
year := int(time.Now().Year())
month := int(time.Now().Month())
car := 1
err := r.ParseForm()
if err != nil {
panic(err)
}
getIntParamPost(r, "year", &year)
getIntParamPost(r, "month", &month)
getIntParamPost(r, "car", &car)
var from, to *time.Time
action := r.Form.Get("action")
if action == "classify" {
from, to, err = changeClassification(getClassificationId(r.Form.Get("classification")), r.Form["drive"], r.Form["groupeddrive"])
if err != nil {
log.Println("Error changing drive classification")
}
} else if action == "group" {
from, to, err = groupDrives(car, r.Form["drive"])
if err != nil {
log.Println("Error grouping drives")
}
} else if action == "ungroup" {
from, to, err = ungroupDrives(car, r.Form["groupeddrive"])
if err != nil {
log.Println("Error ungrouping drives")
}
}
var affectedDays []Day
if from != nil && to != nil {
affectedDays, err = getDays(*from, *to, car)
if err != nil {
log.Println("Error retrieving affected days")
}
} else {
log.Println("The action did not return a useful date range")
}
totals, err := getTotals(year, month, car)
if err != nil {
log.Println("Error retrieving totals")
}
var response PostResponse
response.Totals = totals
response.AffectedDays = affectedDays
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
type PostResponse struct {
Totals Totals
AffectedDays []Day
}