From 1690585bd7681ec4af941f9e4942613bbd1a1ea0 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sat, 27 Jul 2019 17:14:08 +0200 Subject: [PATCH] Make GH auth query static --- backend/src/main.rs | 8 ++++++-- backend/src/service.rs | 23 +++++++++++++++-------- backend/src/statics.rs | 5 +++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index b68619f..d9db139 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -4,7 +4,10 @@ extern crate actix_web; extern crate lazy_static; #[macro_use] extern crate serde_derive; +#[macro_use] +extern crate structopt; +mod config; mod data; mod error; mod service; @@ -14,7 +17,7 @@ use crate::{ data::FilePath, error::Result, service::{Bitbucket, GitLab, Github, Service}, - statics::FAVICON, + statics::{FAVICON, OPT}, }; use actix_web::{ http::header::{self, CacheControl, CacheDirective}, @@ -113,6 +116,7 @@ fn main() -> Result<()> { web::get().to_async(handle_request::), ) }) - .bind("0.0.0.0:8080")? + .workers(OPT.workers) + .bind((OPT.interface, OPT.port))? .run()?) } diff --git a/backend/src/service.rs b/backend/src/service.rs index 08ce369..d6018fd 100644 --- a/backend/src/service.rs +++ b/backend/src/service.rs @@ -1,4 +1,7 @@ -use crate::data::FilePath; +use crate::{ + data::FilePath, + statics::{GITHUB_AUTH_QUERY, OPT}, +}; use actix_web::{ http::{header::LOCATION, StatusCode}, web, Error, HttpResponse, @@ -105,13 +108,17 @@ pub(crate) trait Service { pub(crate) struct Github; impl Github { - fn auth_query() -> Option { + pub(crate) fn auth_query() -> Option { use std::env::var; - var("GITHUB_CLIENT_ID").ok().and_then(|id| { - var("GITHUB_CLIENT_SECRET") - .ok() - .map(|secret| format!("?client_id={}&client_secret={}", id, secret)) - }) + OPT.github_id + .clone() + .or_else(|| var("GITHUB_CLIENT_ID").ok()) + .and_then(|id| { + OPT.github_secret + .clone() + .or_else(|| var("GITHUB_CLIENT_SECRET").ok()) + .map(|secret| format!("?client_id={}&client_secret={}", id, secret)) + }) } } @@ -131,7 +138,7 @@ impl Service for Github { path.user, path.repo, path.commit, - Self::auth_query().unwrap_or_default() + GITHUB_AUTH_QUERY.as_str() ) } diff --git a/backend/src/statics.rs b/backend/src/statics.rs index cd3fcb5..0729595 100644 --- a/backend/src/statics.rs +++ b/backend/src/statics.rs @@ -1,5 +1,10 @@ +use crate::{config::Opt, service::Github}; +use structopt::StructOpt; + const VERSION: &str = env!("CARGO_PKG_VERSION"); pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png"); lazy_static! { pub(crate) static ref USER_AGENT: String = format!("gitache/{}", VERSION); + pub(crate) static ref OPT: Opt = Opt::from_args(); + pub(crate) static ref GITHUB_AUTH_QUERY: String = Github::auth_query().unwrap_or_default(); }