Make GH auth query static

This commit is contained in:
Valentin Brandl 2019-07-27 17:14:08 +02:00
parent 8c3f870321
commit 1690585bd7
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
3 changed files with 26 additions and 10 deletions

View File

@ -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::<GitLab>),
)
})
.bind("0.0.0.0:8080")?
.workers(OPT.workers)
.bind((OPT.interface, OPT.port))?
.run()?)
}

View File

@ -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<String> {
pub(crate) fn auth_query() -> Option<String> {
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()
)
}

View File

@ -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();
}