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
|
use rocket::response;
use rocket::State;
use rocket_contrib::templates::Template;
use std::collections::HashMap;
use crate::Config;
use crate::strava;
#[get("/")]
fn index() -> Template {
let mut context = HashMap::new();
context.insert("parent", "layout");
context.insert("message", "Hello, World");
Template::render("index", context)
}
#[get("/link_strava_callback?<code>")]
fn link_strava_callback(config: State<Config>, code: String) -> Result<String, impl std::error::Error> {
strava::exchange_token(
&config.client_id, &config.client_secret, &code)
.map(|t| format!("{:#?}", t))
}
#[get("/link_strava")]
fn link_strava(config: State<Config>) -> response::Redirect {
response::Redirect::to(
format!(
concat!(
"https://www.strava.com/oauth/authorize?",
"client_id={}&",
"response_type=code&",
"redirect_uri={}&",
"approval_prompt=force&",
"scope=read",
),
config.client_id,
format!("{}/link_strava_callback", config.base_url))
)
}
pub fn start(config: Config) {
rocket::ignite()
.manage(config)
.mount("/", routes![index, link_strava, link_strava_callback])
.attach(Template::fairing())
.launch();
}
|