summaryrefslogtreecommitdiff
path: root/rust/src/web.rs
blob: 45540e4f878fddeb1d7fc06f7ce90f47905d0ebf (plain)
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
use actix_web::{
    get, http::header::ContentType, middleware::Logger, post, web, App, HttpResponse, HttpServer,
    Responder,
};
use async_std::task;
use futures::future::join_all;
use futures::try_join;
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    env::args,
    io::{stdin, stdout, IsTerminal},
    sync::{Arc, Mutex},
};
use uuid::Uuid;

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct Registration {
    url: String,
}

trait AppState: Send + Sync {
    fn register(&mut self, registration: &Registration) -> RegistrationResult;
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
enum RegistrationResult {
    RegistrationSuccess { id: String, url: String },
    UrlAlreadyRegistered { url: String },
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
enum RegistrationFailure {
    UrlAlreadyRegistered { url: String },
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct Client {
    id: Uuid,
    url: String,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct State {
    clients: HashMap<String, Client>,
}

impl State {
    fn new() -> Self {
        Self {
            clients: HashMap::new(),
        }
    }
}

impl AppState for State {
    fn register(&mut self, registration: &Registration) -> RegistrationResult {
        if self.clients.contains_key(&registration.url) {
            RegistrationResult::UrlAlreadyRegistered {
                url: registration.url.clone(),
            }
        } else {
            let id = Uuid::new_v4();
            self.clients.insert(
                registration.url.clone(),
                Client {
                    id,
                    url: registration.url.clone(),
                },
            );
            RegistrationResult::RegistrationSuccess {
                id: id.to_string(),
                url: registration.url.clone(),
            }
        }
    }
}

#[post("/register")]
async fn register(
    app_state: web::Data<Arc<Mutex<dyn AppState>>>,
    registration: web::Json<Registration>,
) -> impl Responder {
    let result = app_state.lock().unwrap().register(&registration);
    match result {
        RegistrationResult::RegistrationSuccess { .. } => HttpResponse::Ok().json(result),
        RegistrationResult::UrlAlreadyRegistered { .. } => HttpResponse::BadRequest().json(result),
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let app_state = Arc::new(Mutex::new(State::new()));
    let send_state: Arc<Mutex<State>> = app_state.clone();
    let http_state: Arc<Mutex<dyn AppState>> = app_state.clone();
    env_logger::init();
    // let it run in the background
    task::spawn(async move { send_tests(send_state).await });
    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .app_data(web::Data::new(http_state.clone()))
            .service(register)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

async fn send_tests(clients: Arc<Mutex<State>>) -> ! {
    loop {
        task::sleep(std::time::Duration::from_secs(1)).await;
        let clients = clients.lock().unwrap();
        for client in clients.clients.values() {
            send_test(client);
        }
    }
}

fn send_test(client: &Client) -> Result<(), String> {
    println!("Sending test to {}", client.url);
    Ok(())
}

#[cfg(test)]
mod app_tests {
    use std::sync::Arc;

    use actix_web::http::header::TryIntoHeaderValue;
    use actix_web::{body, http::header::ContentType, middleware::Logger, test, App};

    use super::*;

    struct DummyAppState {
        id: String,
    }

    impl DummyAppState {
        fn new(id: String) -> Self {
            Self { id }
        }
    }

    impl AppState for DummyAppState {
        fn register(&mut self, registration: &Registration) -> RegistrationResult {
            if self.id == "" {
                RegistrationResult::UrlAlreadyRegistered {
                    url: registration.url.clone(),
                }
            } else {
                RegistrationResult::RegistrationSuccess {
                    id: self.id.clone(),
                    url: registration.url.clone(),
                }
            }
        }
    }

    #[actix_web::test]
    async fn post_registration_returns_success_with_unique_id() {
        let id = "0123456789abcdef0123456789abcdef".to_string();
        let dummy_state: Arc<Mutex<dyn AppState>> =
            Arc::new(Mutex::new(DummyAppState::new(id.clone())));
        // FIXME should only be called once, move to setup
        env_logger::init();

        let app = test::init_service(
            App::new()
                .wrap(Logger::default())
                .app_data(web::Data::new(dummy_state))
                .service(register),
        )
        .await;
        let url = "http://192.168.1.1".to_string();
        let req = test::TestRequest::post()
            .uri("/register")
            .set_json(Registration { url: url.clone() })
            .insert_header(ContentType::json())
            .to_request();

        let resp = test::call_service(&app, req).await;

        assert!(resp.status().is_success());

        let body = resp.into_body();
        let bytes = body::to_bytes(body).await;
        assert_eq!(
            RegistrationResult::RegistrationSuccess { id, url },
            serde_json::from_slice(&bytes.unwrap()).unwrap()
        );
    }

    #[actix_web::test]
    async fn post_registration_returns_400_when_register_fails() {
        let dummy_state: Arc<Mutex<dyn AppState>> =
            Arc::new(Mutex::new(DummyAppState::new("".to_string())));

        let app = test::init_service(
            App::new()
                .wrap(Logger::default())
                .app_data(web::Data::new(dummy_state))
                .service(register),
        )
        .await;
        let url = "http://192.168.1.1".to_string();

        let req = test::TestRequest::post()
            .uri("/register")
            .set_json(Registration { url: url.clone() })
            .insert_header(ContentType::json())
            .to_request();

        let resp = test::call_service(&app, req).await;

        assert!(resp.status().is_client_error());
        assert_eq!(
            ContentType::json().try_into_value().unwrap(),
            resp.headers().get("content-type").unwrap()
        );
    }

    #[test]
    async fn app_does_not_register_same_url_twice() {
        let mut app_state = State::new();
        let registration = Registration {
            url: "http://1.2.3.4".to_string(),
        };

        app_state.register(&registration);
        let result = app_state.register(&registration);

        assert_eq!(
            RegistrationResult::UrlAlreadyRegistered {
                url: "http://1.2.3.4".to_string()
            },
            result
        );
    }
}