Skip to content

Commit 208042e

Browse files
committed
Added an init function and made the ttc data a ref with delayed initialization
1 parent b34984f commit 208042e

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/transit/core.clj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
;; Our database with stops, routes, directions
2222

23-
(defonce ttc (nextbus/load-agency "ttc"))
23+
(defonce ttc (ref nil))
2424

2525
;; Helpers to parse request parameters
2626

@@ -43,6 +43,12 @@
4343
(handler
4444
(update-in req [:uri] #(if (= "/" %) "/index.html" %)))))
4545

46+
;;
47+
48+
(defn init []
49+
(dosync
50+
(ref-set ttc (nextbus/load-agency "ttc"))))
51+
4652
;; The routes and app handler
4753

4854
(defroutes api-routes
@@ -55,16 +61,16 @@
5561
:used (- (.totalMemory rt) (.freeMemory rt))}})))
5662
(GET "/api/stops" {params :params}
5763
(let [position (position-from-params params) radius (radius-from-params params)]
58-
(response {:stops (nextbus/find-stops ttc position radius)
64+
(response {:stops (nextbus/find-stops @ttc position radius)
5965
:position position})))
6066
(GET "/api/predictions-for-position" {params :params}
6167
(let [position (position-from-params params) radius (radius-from-params params)]
62-
(response {:predictions (nextbus/predictions ttc (nextbus/find-stops ttc position radius))
68+
(response {:predictions (nextbus/predictions @ttc (nextbus/find-stops @ttc position radius))
6369
:position position})))
6470
(GET "/api/predictions-for-stops" {params :params}
6571
(let [stops (stops-from-params params)]
6672
(prn stops)
67-
(response {:predictions (nextbus/predictions ttc stops)
73+
(response {:predictions (nextbus/predictions @ttc stops)
6874
:stops stops})))
6975
(route/resources "/")
7076
(route/not-found "Not found"))

0 commit comments

Comments
 (0)