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
#![feature(plugin)]
#![feature(custom_derive)]
#![plugin(rocket_codegen)]
extern crate rocket;
extern crate chrono;
extern crate num_traits;
extern crate postgres;
extern crate r2d2;
extern crate r2d2_postgres;
extern crate regex;
extern crate rocket_contrib;
#[macro_use] extern crate runtime_fmt;
mod test;
mod importer;
pub mod models;
pub mod routes;
use r2d2::Pool;
use r2d2_postgres::{PostgresConnectionManager, TlsMode};
use rocket::response::NamedFile;
use routes::api;
use routes::ui;
use routes::RoutesHandler;
use std::env;
use std::path::{Path, PathBuf};
use chrono::{NaiveDate, NaiveTime};
use std::{time,thread};
use models::api::result::Result;
use postgres::Error;
use importer::update_db;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate num_derive;
fn create_pool() -> Pool<PostgresConnectionManager> {
let mut hostname = "172.18.0.2";
let mut password = String::from("mysecretpassword");
let MAX_ATTEMPTS = 10;
let mut attempts = 1;
if env::var_os("IN_DOCKER").is_some()
&& env::var_os("IN_DOCKER").unwrap().to_str().unwrap() == "true"
{
hostname = "gtfs-db.service.dc1.consul";
password = String::from(
env::var_os("POSTGRES_PASSWORD")
.unwrap()
.to_str()
.unwrap()
.clone(),
);
}
let connection_string = format!("postgres://postgres:{}@{}:5432", password, hostname);
let manager = PostgresConnectionManager::new(
connection_string.clone(),
TlsMode::None).unwrap();
let mut pool = Pool::new(manager);
while pool.is_err() && attempts <= MAX_ATTEMPTS {
let manager = PostgresConnectionManager::new(
connection_string.clone(),
TlsMode::None).unwrap();
println!("Unable to connect, attempt {}/{}", attempts, MAX_ATTEMPTS);
pool = Pool::new(manager);
attempts += 1;
println!("Waiting 5s for the next try...");
thread::sleep_ms(5000);
}
if pool.is_ok() {
return pool.unwrap();
}
panic!(format!("Unable to connect to {}!", hostname));
}
#[get("/css/<file..>")]
fn static_css(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/dist/css/").join(file)).ok()
}
#[get("/js/<file..>")]
fn static_js(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/dist/js/").join(file)).ok()
}
fn start_server(rh: RoutesHandler) {
rocket::ignite()
.manage(rh)
.mount(
"/api",
routes![
api::main,
api::import::agency,
api::import::stops,
api::import::trips,
api::import::calendar,
api::import::routes,
api::import::times,
api::import::url,
api::import::fs,
api::db::update,
api::db::version,
api::agency::agency_by_id,
api::agency::agency,
api::routes::routes,
api::routes::routes_by_query,
api::routes::route_by_stop_uid,
api::routes::route_by_id,
api::routes::route_by_bbox,
api::stops::stops,
api::stops::stops_by_id,
api::stops::stops_near_default,
api::stops::stops_near,
api::stops::stops_by_trip,
api::stops::stops_in_bbox,
api::stops::stops_in_bbox_radius,
api::stops::stops_latlng_test,
api::stops::stops_latlng_test_zoom,
api::stop_times::stop_times_after_near,
api::stop_times::stop_times_between_near,
api::stop_times::stop_times_by_stop_after,
api::stop_times::stop_times_by_stop_between,
api::stop_times::stop_times_between_in,
api::trips::trips,
api::trips::trips_stopid,
api::trips::trip,
api::trips::trips_by_route,
api::trips::trips_by_query,
api::trips::trips_by_bbox,
api::trips::trips_by_bbox_query,
api::times::times_query,
api::times::times_by_trip,
api::times::times_stop,
api::times::times_stop_query
],
)
.mount("/", routes![ui::import::main])
.mount("/", routes![static_css, static_js])
.launch();
}
fn main() {
let pool = create_pool();
println!("Updating Database...");
update_db(&pool.clone());
let rh = RoutesHandler { pool };
start_server(rh);
}