Remove some clones by using Cow
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0729909bab
commit
387b6241a2
@ -24,7 +24,7 @@ impl Cloudflare {
|
||||
"https://api.cloudflare.com/client/v4/zones/{}/purge_cache",
|
||||
Self::identifier()
|
||||
))
|
||||
.header(header::USER_AGENT, statics::USER_AGENT.as_str())
|
||||
.header(header::USER_AGENT, &*statics::USER_AGENT.as_str())
|
||||
.header("X-Auth-Email", Self::auth_email())
|
||||
.header("X-Auth-Key", Self::auth_key())
|
||||
.content_type("application/json")
|
||||
@ -36,7 +36,7 @@ impl Cloudflare {
|
||||
// "https://api.cloudflare.com/client/v4/zones/{}/purge_cache",
|
||||
// Self::identifier()
|
||||
// ))
|
||||
// .header(header::USER_AGENT, statics::USER_AGENT.as_str())
|
||||
// .header(header::USER_AGENT, statics::USER_AGENT.as_ref())
|
||||
// .header("X-Auth-Email", Self::auth_email())
|
||||
// .header("X-Auth-Key", Self::auth_key())
|
||||
// .content_type("application/json")
|
||||
@ -69,7 +69,7 @@ impl CfPurgeRequest {
|
||||
Self {
|
||||
files: vec![format!(
|
||||
"https://{}/{}/{}",
|
||||
statics::HOSTNAME.as_str(),
|
||||
statics::HOSTNAME.as_ref(),
|
||||
T::path(),
|
||||
file
|
||||
)],
|
||||
|
@ -20,9 +20,9 @@ impl FilePath {
|
||||
pub(crate) fn to_key<T: service::Service>(&self) -> Key {
|
||||
Key::new(
|
||||
T::cache_service(),
|
||||
self.user.clone(),
|
||||
self.repo.clone(),
|
||||
self.commit.clone(),
|
||||
Arc::clone(&self.user),
|
||||
Arc::clone(&self.repo),
|
||||
Arc::clone(&self.commit),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ fn purge_local_cache<T: 'static + Service>(
|
||||
cache: web::Data<State>,
|
||||
data: web::Path<FilePath>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let cache = cache.clone();
|
||||
let cache = Arc::clone(&cache);
|
||||
futures::future::ok(()).map(move |_| {
|
||||
if let Ok(mut cache) = cache.write() {
|
||||
info!("Invalidating local cache for {}/{}", T::path(), data.path());
|
||||
@ -188,7 +188,7 @@ fn main() -> Result<()> {
|
||||
Ok(HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(Client::new())
|
||||
.data(state.clone())
|
||||
.data(Arc::clone(&state))
|
||||
.wrap(middleware::Logger::default())
|
||||
.wrap(middleware::NormalizePath)
|
||||
.service(favicon32)
|
||||
|
@ -12,6 +12,7 @@ use actix_web::{
|
||||
use awc::{error::PayloadError, Client, ClientResponse};
|
||||
use bytes::Bytes;
|
||||
use futures::{Future, Stream};
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub(crate) trait ApiResponse {
|
||||
fn commit_ref(&self) -> &str;
|
||||
@ -126,15 +127,17 @@ pub(crate) trait Service: Sized {
|
||||
pub(crate) struct Github;
|
||||
|
||||
impl Github {
|
||||
pub(crate) fn auth_query() -> Option<String> {
|
||||
pub(crate) fn auth_query() -> Option<Cow<'static, str>> {
|
||||
OPT.github_id
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(Cow::from)
|
||||
.or_else(|| load_env_var("GITHUB_CLIENT_ID"))
|
||||
.and_then(|id| {
|
||||
OPT.github_secret
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(Cow::from)
|
||||
.or_else(|| load_env_var("GITHUB_CLIENT_SECRET"))
|
||||
.map(|secret| format!("?client_id={}&client_secret={}", id, secret))
|
||||
.map(|secret| format!("?client_id={}&client_secret={}", id, secret).into())
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -167,7 +170,7 @@ impl Service for Github {
|
||||
path.user,
|
||||
path.repo,
|
||||
path.commit,
|
||||
GITHUB_AUTH_QUERY.as_str()
|
||||
GITHUB_AUTH_QUERY.as_ref()
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::{config::Opt, service::Github};
|
||||
use std::{env, time::Duration};
|
||||
use std::{borrow::Cow, env, time::Duration};
|
||||
use structopt::StructOpt;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
@ -8,31 +8,43 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
|
||||
lazy_static! {
|
||||
pub(crate) static ref USER_AGENT: String = format!("yagcdn/{}", VERSION);
|
||||
pub(crate) static ref OPT: Opt = Opt::from_args();
|
||||
pub(crate) static ref GITHUB_AUTH_QUERY: String = Github::auth_query().unwrap_or_default();
|
||||
pub(crate) static ref CF_ZONE_IDENT: String = OPT
|
||||
pub(crate) static ref GITHUB_AUTH_QUERY: Cow<'static, str> =
|
||||
Github::auth_query().unwrap_or_default().into();
|
||||
pub(crate) static ref CF_ZONE_IDENT: Cow<'static, str> = OPT
|
||||
.cf_zone
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(Cow::from)
|
||||
.or_else(|| load_env_var("CF_ZONE_IDENT"))
|
||||
.expect("Cloudflare zone identifier not set");
|
||||
pub(crate) static ref CF_AUTH_KEY: String = OPT
|
||||
.expect("Cloudflare zone identifier not set")
|
||||
.into();
|
||||
pub(crate) static ref CF_AUTH_KEY: Cow<'static, str> = OPT
|
||||
.cf_auth_key
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(Cow::from)
|
||||
.or_else(|| load_env_var("CF_AUTH_KEY"))
|
||||
.expect("Cloudflare auth key not set");
|
||||
pub(crate) static ref CF_AUTH_USER: String = OPT
|
||||
.expect("Cloudflare auth key not set")
|
||||
.into();
|
||||
pub(crate) static ref CF_AUTH_USER: Cow<'static, str> = OPT
|
||||
.cf_auth_user
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(Cow::from)
|
||||
.or_else(|| load_env_var("CF_AUTH_USER"))
|
||||
.expect("Cloudflare auth user not set");
|
||||
pub(crate) static ref HOSTNAME: String = OPT
|
||||
.expect("Cloudflare auth user not set")
|
||||
.into();
|
||||
pub(crate) static ref HOSTNAME: Cow<'static, str> = OPT
|
||||
.hostname
|
||||
.clone()
|
||||
.as_ref()
|
||||
.map(Cow::from)
|
||||
.or_else(|| load_env_var("YAGCDN_HOSTNAME"))
|
||||
.unwrap_or_else(|| "yagcdn.tk".to_string());
|
||||
.unwrap_or_else(|| "yagcdn.tk".into());
|
||||
}
|
||||
|
||||
pub(crate) fn load_env_var(key: &str) -> Option<String> {
|
||||
env::var(key)
|
||||
.ok()
|
||||
.and_then(|val| if val.is_empty() { None } else { Some(val) })
|
||||
pub(crate) fn load_env_var(key: &str) -> Option<Cow<'static, str>> {
|
||||
env::var(key).ok().and_then(|val| {
|
||||
if val.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(val.into())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user