Skip to content

Commit e5d0cd3

Browse files
author
Léonard Besson
committed
Merge branch 'feature/docker-integration' into 'develop'
Feature/docker integration See merge request lbesson/postcode-service!2
2 parents 32cf636 + cd272fb commit e5d0cd3

9 files changed

Lines changed: 67 additions & 36 deletions

File tree

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
test_data
3+
.git
4+
.env

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM rust:1.38 as builder
2+
3+
RUN apt-get update && apt-get -y install ca-certificates cmake libssl-dev && rm -rf /var/lib/apt/lists/*
4+
5+
COPY . .
6+
7+
RUN rustup target add x86_64-unknown-linux-gnu
8+
ENV PKG_CONFIG_ALLOW_CROSS=1
9+
RUN cargo build --target x86_64-unknown-linux-gnu --release
10+
11+
FROM debian
12+
13+
# libpq is for diesel posgres: https://github.com/diesel-rs/diesel/blob/master/guide_drafts/backend_installation.md
14+
RUN apt-get update && apt-get -y install ca-certificates libpq-dev
15+
COPY --from=builder /target/x86_64-unknown-linux-gnu/release/postcode-service .
16+
17+
CMD ["/postcode-service"]

docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3'
2+
services:
3+
web:
4+
build: .
5+
depends_on:
6+
- postgres
7+
environment:
8+
- DATABASE_URL=postgres://postgres:password@postgres:5432/postcode-service
9+
- DATABASE_POOL_SIZE=15
10+
ports:
11+
- 3000:3000
12+
13+
postgres:
14+
image: mdillon/postgis
15+
environment:
16+
- POSTGRES_DB=postcode-service
17+
- POSTGRES_PASSWORD=password

src/data.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
use std::collections::HashMap;
22
use std::fmt::Formatter;
33

4-
5-
6-
7-
84
use actix_web::web;
9-
10-
use chrono::{Utc};
5+
use chrono::Utc;
116
use diesel::pg::upsert::excluded;
127
use diesel::prelude::*;
138
use futures::Future;
@@ -19,8 +14,7 @@ use zip::ZipArchive;
1914

2015
use crate::data::RefreshError::{NoData, OldData};
2116
use crate::db::Pool;
22-
use crate::models::{Address, NewAddress, NewState, State};
23-
use crate::postcode::AddressRecord;
17+
use crate::models::{Address, AddressRecord, NewAddress, NewState, State};
2418

2519
const APPROXIMATE_ZIP_SIZE_BYTES: usize = 200_097_152; // 200 MB
2620

@@ -238,8 +232,8 @@ pub fn create_or_update_addresses<'a>(
238232
let key = (record.postcode.clone(), record.number.clone());
239233
address_map.insert(key, NewAddress {
240234
id: Uuid::new_v4(),
241-
lon: record.lon as f64,
242235
lat: record.lat as f64,
236+
lon: record.lon as f64,
243237
number: record.number.as_str(),
244238
street: record.street.as_str(),
245239
city: record.city.as_str(),
@@ -302,4 +296,4 @@ pub fn get_addresses(
302296
.order(number.asc())
303297
.limit(ADDRESSES_RESULT_LIMIT)
304298
.load(&pool.get().unwrap())
305-
}
299+
}

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ mod schema;
2222
mod data;
2323
mod db;
2424
mod models;
25-
mod postcode;
2625
mod tests;
2726
mod state_refresher;
2827

@@ -74,7 +73,7 @@ fn main() -> io::Result<()> {
7473
.wrap(Logger::default())
7574
.route("/addresses", web::get().to_async(addresses))
7675
})
77-
.bind("127.0.0.1:3000")?
76+
.bind("0.0.0.0:3000")?
7877
.start();
7978

8079
system.run()

src/models.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub struct NewState<'a> {
2525
#[derive(Serialize, Deserialize, Queryable, Debug)]
2626
pub struct Address {
2727
pub id: Uuid,
28-
pub lon: f64,
2928
pub lat: f64,
29+
pub lon: f64,
3030
pub number: String,
3131
pub street: String,
3232
pub city: String,
@@ -38,11 +38,24 @@ pub struct Address {
3838
#[table_name="addresses"]
3939
pub struct NewAddress<'a> {
4040
pub id: Uuid,
41-
pub lon: f64,
4241
pub lat: f64,
42+
pub lon: f64,
4343
pub number: &'a str,
4444
pub street: &'a str,
4545
pub city: &'a str,
4646
pub region: &'a str,
4747
pub postcode: &'a str
4848
}
49+
50+
// Used as CSV record model
51+
#[derive(Debug, Deserialize)]
52+
#[serde(rename_all = "UPPERCASE")]
53+
pub struct AddressRecord {
54+
pub lon: f32,
55+
pub lat: f32,
56+
pub number: String,
57+
pub street: String,
58+
pub city: String,
59+
pub region: String,
60+
pub postcode: String
61+
}

src/postcode.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/state_refresher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::thread::JoinHandle;
88
pub struct StateRefresher {}
99

1010
impl StateRefresher {
11-
const INTERVAL_SECS: u64 = 3600 * 4;
11+
const INTERVAL_SECS: u64 = 3600 * 24;
1212

1313
pub fn start() -> io::Result<JoinHandle<()>> {
1414
thread::Builder::new()
@@ -22,4 +22,4 @@ impl StateRefresher {
2222
}
2323
})
2424
}
25-
}
25+
}

src/tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ mod tests {
88
http::{header, StatusCode}, HttpRequest, HttpResponse, test, web,
99
};
1010
use diesel::RunQueryDsl;
11+
1112
use lazy_static::lazy_static;
1213

1314
use crate::addresses;
1415
use crate::data::create_or_update_addresses;
1516
use crate::db::{init_test_connection_pool, Pool};
16-
use crate::models::Address;
17-
use crate::postcode::AddressRecord;
17+
use crate::models::{Address, AddressRecord};
1818

1919
embed_migrations!("./migrations");
2020

@@ -161,35 +161,35 @@ mod tests {
161161
&POOL.get().unwrap(),
162162
&[
163163
AddressRecord {
164-
lon: 1.0,
165164
lat: 2.0,
165+
lon: 1.0,
166166
number: "1".to_string(),
167167
street: "Street".to_string(),
168168
city: "City".to_string(),
169169
region: "Region".to_string(),
170170
postcode: "2222AA".to_string()
171171
},
172172
AddressRecord {
173-
lon: 2.0,
174173
lat: 3.0,
174+
lon: 2.0,
175175
number: "2".to_string(),
176176
street: "Street".to_string(),
177177
city: "City".to_string(),
178178
region: "Region".to_string(),
179179
postcode: "2222AA".to_string()
180180
},
181181
AddressRecord {
182-
lon: 3.0,
183182
lat: 4.0,
183+
lon: 3.0,
184184
number: "2A".to_string(),
185185
street: "Street".to_string(),
186186
city: "City".to_string(),
187187
region: "Region".to_string(),
188188
postcode: "2222AA".to_string()
189189
},
190190
AddressRecord {
191-
lon: 4.0,
192191
lat: 5.0,
192+
lon: 4.0,
193193
number: "2B".to_string(),
194194
street: "Street".to_string(),
195195
city: "City".to_string(),
@@ -199,4 +199,4 @@ mod tests {
199199
]
200200
);
201201
}
202-
}
202+
}

0 commit comments

Comments
 (0)