about summary refs log tree commit diff
path: root/corp/rih/backend/src/main.rs
blob: 208e0367c6a8c7dada1024b4189ec6d8a614f4c7 (plain) (blame)
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
use anyhow::{bail, Context, Result};
use log::{debug, error, info, warn, LevelFilter};
use rouille::{Request, Response};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::env;
use std::net::SocketAddr;
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

mod yandex_log;

/// Represents the request sent by the frontend application.
#[derive(Debug, Deserialize)]
struct FrontendReq {
    captcha_token: String,
    record: Record,
}

/// Represents a single record as filled in by a user. This is the
/// primary data structure we want to populate and persist somewhere.
#[derive(Debug, Deserialize, Serialize)]
struct Record {
    // Record-specific metadata
    uuid: Uuid,

    // Personal information
    name: String,
    email: String,
    citizenship: String, // TODO
    personal_details: String,

    // Job information
    position: String,
    technologies: BTreeSet<String>,
    job_details: String,
    work_background: String,
}

impl Record {
    fn validate(&self) -> bool {
        true
    }
}

fn validate_captcha(token: &str) -> Result<()> {
    // TODO(tazjin): pass `ip` parameter
    let url = "https://smartcaptcha.yandexcloud.net/validate";
    let backend_key =
        env::var("YANDEX_SMARTCAPTCHA_KEY").context("captcha verification key not provided")?;

    #[derive(Deserialize)]
    struct CaptchaResponse {
        status: String,
        message: String,
    }

    let response: CaptchaResponse = attohttpc::get(url)
        .param("secret", backend_key)
        .param("token", token)
        .send()
        .context("failed to send captcha verification request")?
        .error_for_status()
        .context("captcha verification request failed")?
        .json()
        .context("failed to deserialize captcha verification response")?;

    if response.status != "ok" {
        warn!(
            "invalid captcha: {} ({})",
            response.message, response.status
        );
    }

    info!("captcha token was valid");

    Ok(())
}

fn persist_record(ip: &SocketAddr, record: &Record) -> Result<()> {
    let bucket_name = "rih-backend-data";
    let credentials =
        s3::creds::Credentials::from_env().context("failed to initialise storage credentials")?;

    let yandex_region: s3::Region = s3::Region::Custom {
        region: "ru-central1".to_string(),
        endpoint: "storage.yandexcloud.net".to_string(),
    };

    let bucket = s3::Bucket::new(bucket_name, yandex_region, credentials)
        .context("failed to initialise storage client")?;

    let path_uuid = Uuid::new_v4();
    let epoch = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("failed to get current time")?
        .as_secs();

    let path = format!("/records/{}-{}.json", epoch, path_uuid);

    info!("writing record to '{}'", path);

    let data = serde_json::json!({
        "ip": ip.to_string(),
        "record": record,
    });

    let response = bucket
        .put_object(path, data.to_string().as_bytes())
        .context("failed to persist storage object")?;

    debug!(
        "Object Storage response: ({}) {}",
        response.status_code(),
        response.as_str().unwrap_or("<unprintable>")
    );

    Ok(())
}

fn handle_submit(req: &Request) -> Result<Response> {
    let submitted: FrontendReq =
        rouille::input::json::json_input(req).context("failed to deserialise frontend request")?;

    validate_captcha(&submitted.captcha_token)?;

    if !submitted.record.validate() {
        bail!("invalid record: {:?}", submitted.record);
    }

    persist_record(req.remote_addr(), &submitted.record).context("failed to persist record")?;

    Ok(Response::text("success"))
}

fn main() -> Result<()> {
    log::set_logger(&yandex_log::YANDEX_CLOUD_LOGGER)
        .map(|()| log::set_max_level(LevelFilter::Info))
        .expect("log configuration must succeed");
    let port = env::var("PORT").unwrap_or_else(|_| /* rihb = */ "7442".to_string());
    let listen = format!("0.0.0.0:{port}");

    info!("launching rih-backend on: {}", listen);

    rouille::start_server(&listen, move |request| {
        if request.method() == "POST" && request.url() == "/submit" {
            info!("handling submit request from {}", request.remote_addr());
            match handle_submit(request) {
                Ok(response) => {
                    info!("submit handled successfully");
                    response
                }
                Err(err) => {
                    error!("failed to handle submit: {}", err);
                    Response::empty_400()
                }
            }
        } else {
            warn!(
                "no matching route for request: {} {}",
                request.method(),
                request.url()
            );

            Response::empty_404()
        }
    });
}