Skip to content

Commit 6b8ada9

Browse files
authored
Merge pull request #19 from fiji-flo/async
Async
2 parents 0ceba5d + 0fa7fad commit 6b8ada9

9 files changed

Lines changed: 946 additions & 584 deletions

File tree

Cargo.lock

Lines changed: 752 additions & 446 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
[package]
22
name = "dino-park-lookout"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Florian Merz <flomerz@gmail.com>"]
55
edition = "2018"
66

77
[dependencies]
8-
dino_park_gate = { git = "https://github.com/fiji-flo/dino-park-gate", branch = "0.2.0", version = "0.2.0" }
9-
cis_client = { git = "https://github.com/fiji-flo/cis_client-rust", branch = "0.2.0", version = "0.2.0" }
10-
cis_profile = { git = "https://github.com/fiji-flo/cis_profile-rust", branch = "0.2.0", version = "0.2.0" }
11-
actix-web = "1.0"
12-
actix-cors = "0.1"
13-
futures = "0.1"
8+
dino_park_gate = { git = "https://github.com/fiji-flo/dino-park-gate", branch = "0.3.0", version = "0.3.0" }
9+
cis_client = { git = "https://github.com/fiji-flo/cis_client-rust", branch = "0.4.0", version = "0.4.0", features = ["sync"] }
10+
cis_profile = { git = "https://github.com/fiji-flo/cis_profile-rust", branch = "0.3.1", version = "0.3.1" }
11+
actix-web = "2.0"
12+
actix-rt = "1.0"
13+
actix-cors = "0.2"
14+
futures = "0.3"
1415
log = "0.4"
1516
env_logger = "0.7"
1617
serde = "1.0.80"
1718
serde_json = "1.0.32"
1819
serde_derive = "1.0.80"
19-
reqwest = "0.9"
20+
reqwest = { version = "0.10", features = ["json"] }
2021
chrono = "0.4"
2122
config = "0.10"
2223
failure = "0.1"
2324
failure_derive = "0.1"
2425
biscuit = "0.4"
2526
url = "2.1"
27+
tokio = "0.2"

ci/install_deps.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
export RUSTUP_HOME=/usr/local/rustup
22
export CARGO_HOME=/usr/local/cargo
33
export PATH=/usr/local/cargo/bin:$PATH
4-
export RUST_VERSION=1.38.0
4+
export RUST_VERSION=1.40.0
55

66
set -eux
7+
78
dpkgArch="$(dpkg --print-architecture)"
89
case "${dpkgArch##*-}" in
9-
amd64) rustArch='x86_64-unknown-linux-gnu'; rustupSha256='a46fe67199b7bcbbde2dcbc23ae08db6f29883e260e23899a88b9073effc9076' ;;
10-
armhf) rustArch='armv7-unknown-linux-gnueabihf'; rustupSha256='6af5abbbae02e13a9acae29593ec58116ab0e3eb893fa0381991e8b0934caea1' ;;
11-
arm64) rustArch='aarch64-unknown-linux-gnu'; rustupSha256='51862e576f064d859546cca5f3d32297092a850861e567327422e65b60877a1b' ;;
12-
i386) rustArch='i686-unknown-linux-gnu'; rustupSha256='91456c3e6b2a3067914b3327f07bc182e2a27c44bff473263ba81174884182be' ;;
10+
amd64) rustArch='x86_64-unknown-linux-gnu'; rustupSha256='e68f193542c68ce83c449809d2cad262cc2bbb99640eb47c58fc1dc58cc30add' ;;
11+
armhf) rustArch='armv7-unknown-linux-gnueabihf'; rustupSha256='7c1c329a676e50c287d8183b88f30cd6afd0be140826a9fbbc0e3d717fab34d7' ;;
12+
arm64) rustArch='aarch64-unknown-linux-gnu'; rustupSha256='d861cc86594776414de001b96964be645c4bfa27024052704f0976dc3aed1b18' ;;
13+
i386) rustArch='i686-unknown-linux-gnu'; rustupSha256='89f1f797dca2e5c1d75790c3c6b7be0ee473a7f4eca9663e623a41272a358da0' ;;
1314
*) echo >&2 "unsupported architecture: ${dpkgArch}"; exit 1 ;;
1415
esac
15-
url="https://static.rust-lang.org/rustup/archive/1.18.3/${rustArch}/rustup-init"
16+
url="https://static.rust-lang.org/rustup/archive/1.20.2/${rustArch}/rustup-init"
1617
wget "$url"
1718
echo "${rustupSha256} *rustup-init" | sha256sum -c -
1819
chmod +x rustup-init
19-
./rustup-init -y --no-modify-path --default-toolchain $RUST_VERSION
20+
./rustup-init -y --no-modify-path --profile minimal --default-toolchain $RUST_VERSION
2021
rm rustup-init
2122
chmod -R a+w $RUSTUP_HOME $CARGO_HOME
2223
rustup --version

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ pub enum UpdateError {
88
SearchUpdate(Error),
99
#[fail(display = "error updating groups: {}", _0)]
1010
GroupsUpdate(Error),
11+
#[fail(display = "error updating")]
12+
Other,
1113
}

src/events/app.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use actix_web::HttpResponse;
1010
use actix_web::Result;
1111
use serde_json::json;
1212

13-
fn update_event<U: UpdaterClient + Clone + 'static>(
13+
async fn update_event<U: UpdaterClient + Clone + 'static>(
1414
updater: Data<U>,
1515
n: Json<Notification>,
1616
) -> Result<HttpResponse> {
@@ -27,7 +27,8 @@ pub fn update_app<U: UpdaterClient + Clone + Send + 'static>(
2727
.allowed_methods(vec!["POST"])
2828
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
2929
.allowed_header(http::header::CONTENT_TYPE)
30-
.max_age(3600),
30+
.max_age(3600)
31+
.finish(),
3132
)
3233
.data(updater)
3334
.service(web::resource("").route(web::post().to(update_event::<U>)))

src/healthz.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub fn healthz_app() -> impl HttpServiceFactory {
1616
.allowed_methods(vec!["GET", "HEAD"])
1717
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
1818
.allowed_header(http::header::CONTENT_TYPE)
19-
.max_age(3600),
19+
.max_age(3600)
20+
.finish(),
2021
)
2122
.service(web::resource("").to(healthz))
2223
}

src/internal/app.rs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::bulk::Bulk;
22
use crate::settings::DinoParkSettings;
3+
use crate::updater::send_profile;
34
use crate::updater::UpdaterClient;
45
use actix_cors::Cors;
5-
use actix_web::client::Client;
66
use actix_web::dev::HttpServiceFactory;
77
use actix_web::error;
88
use actix_web::error::Error;
@@ -13,61 +13,37 @@ use actix_web::web::Json;
1313
use actix_web::HttpResponse;
1414
use actix_web::Result;
1515
use cis_profile::schema::Profile;
16-
use futures::Future;
16+
use futures::future::TryFutureExt;
1717
use serde_json::json;
1818
use serde_json::Value;
1919

20-
pub fn internal_update(
21-
dp: &DinoParkSettings,
22-
profile: &Profile,
23-
) -> impl Future<Item = Value, Error = Error> {
24-
let id = profile
25-
.user_id
26-
.value
27-
.clone()
28-
.unwrap_or_else(|| String::from("unknown"));
29-
info!("internally updating profile for: {}", &id);
30-
let id_c = id.clone();
31-
let orgchart_update = Client::default()
32-
.post(&dp.orgchart_update_endpoint)
33-
.send_json(profile)
34-
.map(move |_| info!("internally updated orgchart for: {}", id));
35-
36-
orgchart_update
37-
.join(
38-
Client::default()
39-
.post(&dp.search_update_endpoint)
40-
.send_json(profile)
41-
.map(move |_| info!("internally updated search for: {}", id_c)),
42-
)
43-
.map(|_| json!({}))
44-
.map_err(Into::into)
20+
pub async fn internal_update(dp: &DinoParkSettings, profile: Profile) -> Result<Value, Error> {
21+
send_profile(dp, profile).map_err(Into::into).await
4522
}
4623

47-
fn internal_update_event(
24+
async fn internal_update_event(
4825
dino_park_settings: Data<DinoParkSettings>,
4926
profile: Json<Profile>,
50-
) -> impl Future<Item = HttpResponse, Error = Error> {
27+
) -> Result<HttpResponse, Error> {
5128
let id = profile
5229
.user_id
5330
.value
5431
.clone()
5532
.unwrap_or_else(|| String::from("unknown"));
5633
info!("internally updating profile for: {}", &id);
5734
let id_c = id.clone();
58-
internal_update(&dino_park_settings, &profile)
59-
.map(move |res| {
60-
info!("internally updated profile for {}", id);
61-
HttpResponse::Ok().json(res)
62-
})
63-
.map_err(move |e| {
35+
let res = internal_update(&dino_park_settings, profile.into_inner()).await;
36+
info!("internally updated profile for {}", id);
37+
match res {
38+
Ok(j) => Ok(HttpResponse::Ok().json(j)),
39+
Err(e) => {
6440
error!("failed to internally update profile for {}: {}", id_c, e);
65-
error::ErrorInternalServerError(e)
66-
})
67-
.map_err(Into::into)
41+
Err(error::ErrorInternalServerError(e))
42+
}
43+
}
6844
}
6945

70-
fn bulk_update<U: UpdaterClient + Clone + 'static>(
46+
async fn bulk_update<U: UpdaterClient + Clone + 'static>(
7147
updater: Data<U>,
7248
bulk: Json<Bulk>,
7349
) -> Result<HttpResponse> {
@@ -85,11 +61,12 @@ pub fn internal_app<U: UpdaterClient + Clone + Send + 'static>(
8561
.allowed_methods(vec!["POST"])
8662
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
8763
.allowed_header(http::header::CONTENT_TYPE)
88-
.max_age(3600),
64+
.max_age(3600)
65+
.finish(),
8966
)
9067
.data(updater)
9168
.data(dino_park_settings)
92-
.data(web::JsonConfig::default().limit(1_048_576))
69+
.app_data(web::JsonConfig::default().limit(1_048_576))
9370
.service(web::resource("/bulk").route(web::post().to(bulk_update::<U>)))
94-
.service(web::resource("/update").route(web::post().to_async(internal_update_event)))
71+
.service(web::resource("/update").route(web::post().to(internal_update_event)))
9572
}

src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::internal::app::internal_app;
2020
use crate::updater::InternalUpdater;
2121
use crate::updater::Updater;
2222
use crate::updater::UpdaterClient;
23+
use actix_rt::System;
2324
use actix_web::middleware::Logger;
2425
use actix_web::web;
2526
use actix_web::App;
@@ -34,7 +35,7 @@ use std::thread::spawn;
3435
fn main() -> Result<(), Error> {
3536
::std::env::set_var(
3637
"RUST_LOG",
37-
"actix_web=info,dino_park_lookout=info,dino_park_gate=info",
38+
"actix_web=info,dino_park_lookout=info,dino_park_gate=info,cis_client=info,shared_expiry_get=info",
3839
);
3940
env_logger::init();
4041
info!("building the lookout");
@@ -43,9 +44,10 @@ fn main() -> Result<(), Error> {
4344
.map_err(|e| format_err!("unable to create cis_client: {}", e))?;
4445
let dino_park = s.dino_park.clone();
4546
let validation_settings = s.auth.validation.clone();
46-
let provider = Provider::from_issuer(&s.auth.issuer)?;
47+
let mut rt = tokio::runtime::Runtime::new()?;
48+
let provider = rt.block_on(Provider::from_issuer(&s.auth.issuer))?;
4749
// Start http server
48-
let updater = InternalUpdater::new(cis_client.clone(), dino_park.clone());
50+
let updater = InternalUpdater::new(cis_client, dino_park.clone());
4951

5052
let client = updater.client();
5153
let stop_client = updater.client();
@@ -54,7 +56,6 @@ fn main() -> Result<(), Error> {
5456
error!("unable to start updater: {}", e);
5557
}
5658
});
57-
5859
let server = HttpServer::new(move || {
5960
let auth_middleware = SimpleAuth {
6061
checker: provider.clone(),
@@ -74,9 +75,9 @@ fn main() -> Result<(), Error> {
7475
.service(healthz_app())
7576
})
7677
.bind("0.0.0.0:8082")?;
77-
server
78-
.run()
79-
.map_err(|_| format_err!("unable to run server!"))?;
78+
79+
System::new("lookout-actix-rt").block_on(async move { server.run().await })?;
80+
8081
info!("Stopped http server");
8182
stop_client.stop();
8283
updater_thread

0 commit comments

Comments
 (0)