diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d7f7b98 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,56 @@ +#![feature(proc_macro_hygiene)] +#![feature(decl_macro)] +#[macro_use] +extern crate rocket; +use rocket::response; +use rocket::State; +use rocket_contrib::templates::Template; +use std::collections::HashMap; + +mod strava; + +#[derive(Debug)] +pub struct Config { + pub client_id: String, + pub client_secret: String, + pub base_url: String, +} + +#[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) -> String { + strava::exchange_token(&config.client_id, &config.client_secret, &code); + "OK".to_string() +} + +#[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_server(config: Config) { + rocket::ignite() + .manage(config) + .mount("/", routes![index, link_strava, link_strava_callback]) + .attach(Template::fairing()) + .launch(); +} |