diff options
author | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2020-01-28 21:18:50 -0500 |
---|---|---|
committer | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2020-01-28 21:18:50 -0500 |
commit | 94cbd545217d85f6e975131ee70a26194b7fdb3c (patch) | |
tree | 36e788b6554662db8e2fdcfd9358d9afc6d57384 /src |
Initial rocket app framework
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 56 | ||||
-rw-r--r-- | src/main.rs | 36 | ||||
-rw-r--r-- | src/strava.rs | 15 |
3 files changed, 107 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(); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..32333f1 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +#[macro_use] +extern crate clap; + +fn main() { + let matches = clap_app!(pjournal => + (version: "0.1") + (author: "KJ Ørbekk <kj@orbekk.com>") + (about: "Practice Journaling") + (@arg strava_client_secret: + --strava_client_secret + +required +takes_value + "Client secret for strava authentication") + (@arg strava_client_id: + --strava_client_id + +required +takes_value + "Client id for strava authentication") + (@arg base_url: + --base_url + +takes_value + "Endpoint for this web app") + ) + .get_matches(); + + let config = pjournal::Config { + client_id: matches + .value_of("strava_client_id") + .unwrap().to_string(), + client_secret: matches + .value_of("strava_client_secret") + .unwrap().to_string(), + base_url: matches + .value_of("base_url") + .unwrap_or("http://localhost:8000").to_string(), + }; + pjournal::start_server(config); +} diff --git a/src/strava.rs b/src/strava.rs new file mode 100644 index 0000000..490964b --- /dev/null +++ b/src/strava.rs @@ -0,0 +1,15 @@ +use reqwest; + +pub fn exchange_token( + client_id: &str, + client_secret: &str, + code: &str) { + let client = reqwest::blocking::Client::new(); + let params = [("client_id", client_id), + ("client_secret", client_secret), + ("code", code)]; + let uri = "https://www.strava.com/oauth/token"; + let req = client.post(uri).form(¶ms); + let mut res = req.send().unwrap().text(); + println!("{:?}", res); +} |