Use redirect cache for tags and branches
This commit is contained in:
parent
b65df17a4e
commit
902bad4ca5
@ -7,6 +7,9 @@ extern crate serde_derive;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate structopt;
|
extern crate structopt;
|
||||||
|
|
||||||
|
// TODO: cow instead of string
|
||||||
|
|
||||||
|
mod cache;
|
||||||
mod cdn;
|
mod cdn;
|
||||||
mod config;
|
mod config;
|
||||||
mod data;
|
mod data;
|
||||||
@ -15,19 +18,21 @@ mod service;
|
|||||||
mod statics;
|
mod statics;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
cache::{Cache, CacheResult},
|
||||||
cdn::Cloudflare,
|
cdn::Cloudflare,
|
||||||
data::FilePath,
|
data::{FilePath, State},
|
||||||
error::Result,
|
error::Result,
|
||||||
service::{Bitbucket, GitLab, Github, Service},
|
service::{Bitbucket, GitLab, Github, Service},
|
||||||
statics::{FAVICON, OPT},
|
statics::{FAVICON, OPT},
|
||||||
};
|
};
|
||||||
use actix_files;
|
use actix_files;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::header::{self, CacheControl, CacheDirective},
|
http::header::{self, CacheControl, CacheDirective, LOCATION},
|
||||||
middleware, web, App, Error, HttpResponse, HttpServer,
|
middleware, web, App, Error, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
use awc::{http::StatusCode, Client};
|
use awc::{http::StatusCode, Client};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
fn proxy_file<T: Service>(
|
fn proxy_file<T: Service>(
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
@ -62,26 +67,55 @@ fn proxy_file<T: Service>(
|
|||||||
|
|
||||||
fn redirect<T: Service>(
|
fn redirect<T: Service>(
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
|
cache: web::Data<State>,
|
||||||
data: web::Path<FilePath>,
|
data: web::Path<FilePath>,
|
||||||
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
||||||
|
let invalid = {
|
||||||
|
if let Ok(cache) = cache.read() {
|
||||||
|
let key = data.to_key::<T>();
|
||||||
|
match cache.get(&key) {
|
||||||
|
CacheResult::Cached(head) => {
|
||||||
|
let head = head.clone();
|
||||||
|
return Box::new(futures::future::ok(()).map(move |_| {
|
||||||
|
HttpResponse::SeeOther()
|
||||||
|
.header(
|
||||||
|
LOCATION,
|
||||||
|
T::redirect_url(&data.user, &data.repo, &head, &data.file).as_str(),
|
||||||
|
)
|
||||||
|
.finish()
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
CacheResult::Invalid => true,
|
||||||
|
CacheResult::Empty => false,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if invalid {
|
||||||
|
if let Ok(mut cache) = cache.write() {
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
Box::new(
|
Box::new(
|
||||||
client
|
client
|
||||||
.get(&T::api_url(&data))
|
.get(&T::api_url(&data))
|
||||||
.header(header::USER_AGENT, statics::USER_AGENT.as_str())
|
.header(header::USER_AGENT, statics::USER_AGENT.as_str())
|
||||||
.send()
|
.send()
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(move |response| T::request_head(response, data, client)),
|
.and_then(move |response| T::request_head(response, data, client, Arc::clone(&cache))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_request<T: Service>(
|
fn handle_request<T: Service>(
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
|
cache: web::Data<State>,
|
||||||
data: web::Path<FilePath>,
|
data: web::Path<FilePath>,
|
||||||
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
||||||
if data.commit.len() == 40 {
|
if data.commit.len() == 40 {
|
||||||
proxy_file::<T>(client, data)
|
proxy_file::<T>(client, data)
|
||||||
} else {
|
} else {
|
||||||
redirect::<T>(client, data)
|
redirect::<T>(client, cache, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,19 +158,49 @@ fn favicon32() -> HttpResponse {
|
|||||||
.body(FAVICON)
|
.body(FAVICON)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn purge_cache<T: Service>(
|
fn purge_cache<T: 'static + Service>(
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
file: web::Path<String>,
|
cache: web::Data<State>,
|
||||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
data: web::Path<FilePath>,
|
||||||
Cloudflare::purge_cache::<T>(&client, &file)
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
||||||
.map(|success| HttpResponse::Ok().body(success.to_string()))
|
if data.commit.len() == 40 {
|
||||||
|
Box::new(
|
||||||
|
Cloudflare::purge_cache::<T>(&client, &data.path())
|
||||||
|
.map(|success| HttpResponse::Ok().body(success.to_string())),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
let cache = cache.clone();
|
||||||
|
Box::new(futures::future::ok(()).map(move |_| {
|
||||||
|
if let Ok(mut cache) = cache.write() {
|
||||||
|
let key = data.to_key::<T>();
|
||||||
|
cache.invalidate(&key);
|
||||||
|
HttpResponse::Ok().finish()
|
||||||
|
} else {
|
||||||
|
HttpResponse::InternalServerError().finish()
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dbg<T: Service>(
|
fn dbg<T: 'static + Service>(
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
file: web::Path<String>,
|
cache: web::Data<State>,
|
||||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
data: web::Path<FilePath>,
|
||||||
Cloudflare::dbg::<T>(&client, &file)
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
||||||
|
if data.commit.len() == 40 {
|
||||||
|
Box::new(Cloudflare::dbg::<T>(&client, &data.path()))
|
||||||
|
} else {
|
||||||
|
let cache = cache.clone();
|
||||||
|
Box::new(futures::future::ok(()).map(move |_| {
|
||||||
|
if let Ok(mut cache) = cache.write() {
|
||||||
|
let key = data.to_key::<T>();
|
||||||
|
cache.invalidate(&key);
|
||||||
|
HttpResponse::Ok().finish()
|
||||||
|
} else {
|
||||||
|
HttpResponse::InternalServerError().finish()
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
@ -144,9 +208,11 @@ fn main() -> Result<()> {
|
|||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
openssl_probe::init_ssl_cert_env_vars();
|
openssl_probe::init_ssl_cert_env_vars();
|
||||||
|
|
||||||
|
let state: State = Arc::new(RwLock::new(Cache::new()));
|
||||||
Ok(HttpServer::new(move || {
|
Ok(HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(Client::new())
|
.data(Client::new())
|
||||||
|
.data(state.clone())
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.wrap(middleware::NormalizePath)
|
.wrap(middleware::NormalizePath)
|
||||||
.service(favicon32)
|
.service(favicon32)
|
||||||
@ -154,13 +220,16 @@ fn main() -> Result<()> {
|
|||||||
"/github/{user}/{repo}/{commit}/{file:.*}",
|
"/github/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::get().to_async(handle_request::<Github>),
|
web::get().to_async(handle_request::<Github>),
|
||||||
)
|
)
|
||||||
.route("/github/{file:.*}", web::delete().to_async(dbg::<Github>))
|
.route(
|
||||||
|
"/github/{user}/{repo}/{commit}/{file:.*}",
|
||||||
|
web::delete().to_async(dbg::<Github>),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
|
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::get().to_async(handle_request::<Bitbucket>),
|
web::get().to_async(handle_request::<Bitbucket>),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/bitbucket//{file:.*}",
|
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::delete().to_async(dbg::<Bitbucket>),
|
web::delete().to_async(dbg::<Bitbucket>),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
@ -171,7 +240,10 @@ fn main() -> Result<()> {
|
|||||||
"/gist/{user}/{repo}/{commit}/{file:.*}",
|
"/gist/{user}/{repo}/{commit}/{file:.*}",
|
||||||
web::get().to_async(serve_gist),
|
web::get().to_async(serve_gist),
|
||||||
)
|
)
|
||||||
.route("/gitlab/{file:.*}", web::delete().to_async(dbg::<GitLab>))
|
.route(
|
||||||
|
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
|
||||||
|
web::delete().to_async(dbg::<GitLab>),
|
||||||
|
)
|
||||||
.service(actix_files::Files::new("/", "./public").index_file("index.html"))
|
.service(actix_files::Files::new("/", "./public").index_file("index.html"))
|
||||||
})
|
})
|
||||||
.workers(OPT.workers)
|
.workers(OPT.workers)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
data::FilePath,
|
cache,
|
||||||
|
data::{FilePath, State},
|
||||||
statics::{load_env_var, GITHUB_AUTH_QUERY, OPT},
|
statics::{load_env_var, GITHUB_AUTH_QUERY, OPT},
|
||||||
};
|
};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
@ -62,11 +63,13 @@ impl ApiResponse for GitLabApiResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait Service {
|
pub(crate) trait Service: Sized {
|
||||||
type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static;
|
type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static;
|
||||||
|
|
||||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
||||||
|
|
||||||
|
fn cache_service() -> cache::Service;
|
||||||
|
|
||||||
fn api_url(path: &FilePath) -> String;
|
fn api_url(path: &FilePath) -> String;
|
||||||
|
|
||||||
fn path() -> &'static str;
|
fn path() -> &'static str;
|
||||||
@ -77,6 +80,7 @@ pub(crate) trait Service {
|
|||||||
mut response: ClientResponse<S>,
|
mut response: ClientResponse<S>,
|
||||||
data: web::Path<FilePath>,
|
data: web::Path<FilePath>,
|
||||||
_client: web::Data<Client>,
|
_client: web::Data<Client>,
|
||||||
|
cache: State,
|
||||||
) -> Box<dyn Future<Item = HttpResponse, Error = Error>>
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>>
|
||||||
where
|
where
|
||||||
S: 'static + Stream<Item = Bytes, Error = PayloadError>,
|
S: 'static + Stream<Item = Bytes, Error = PayloadError>,
|
||||||
@ -86,6 +90,10 @@ pub(crate) trait Service {
|
|||||||
response
|
response
|
||||||
.json::<Self::Response>()
|
.json::<Self::Response>()
|
||||||
.map(move |resp| {
|
.map(move |resp| {
|
||||||
|
if let Ok(mut cache) = cache.write() {
|
||||||
|
let key = data.to_key::<Self>();
|
||||||
|
cache.store(key, resp.commit_ref().to_string());
|
||||||
|
}
|
||||||
HttpResponse::SeeOther()
|
HttpResponse::SeeOther()
|
||||||
.header(
|
.header(
|
||||||
LOCATION,
|
LOCATION,
|
||||||
@ -126,6 +134,10 @@ impl Github {
|
|||||||
impl Service for Github {
|
impl Service for Github {
|
||||||
type Response = GitHubApiResponse;
|
type Response = GitHubApiResponse;
|
||||||
|
|
||||||
|
fn cache_service() -> cache::Service {
|
||||||
|
cache::Service::GitHub
|
||||||
|
}
|
||||||
|
|
||||||
fn path() -> &'static str {
|
fn path() -> &'static str {
|
||||||
"github"
|
"github"
|
||||||
}
|
}
|
||||||
@ -157,6 +169,10 @@ pub(crate) struct Bitbucket;
|
|||||||
impl Service for Bitbucket {
|
impl Service for Bitbucket {
|
||||||
type Response = BitbucketApiResponse;
|
type Response = BitbucketApiResponse;
|
||||||
|
|
||||||
|
fn cache_service() -> cache::Service {
|
||||||
|
cache::Service::Bitbucket
|
||||||
|
}
|
||||||
|
|
||||||
fn path() -> &'static str {
|
fn path() -> &'static str {
|
||||||
"bitbucket"
|
"bitbucket"
|
||||||
}
|
}
|
||||||
@ -185,6 +201,10 @@ pub(crate) struct GitLab;
|
|||||||
impl Service for GitLab {
|
impl Service for GitLab {
|
||||||
type Response = GitLabApiResponse;
|
type Response = GitLabApiResponse;
|
||||||
|
|
||||||
|
fn cache_service() -> cache::Service {
|
||||||
|
cache::Service::GitLab
|
||||||
|
}
|
||||||
|
|
||||||
fn path() -> &'static str {
|
fn path() -> &'static str {
|
||||||
"gitlab"
|
"gitlab"
|
||||||
}
|
}
|
||||||
@ -209,6 +229,7 @@ impl Service for GitLab {
|
|||||||
mut response: ClientResponse<S>,
|
mut response: ClientResponse<S>,
|
||||||
data: web::Path<FilePath>,
|
data: web::Path<FilePath>,
|
||||||
client: web::Data<Client>,
|
client: web::Data<Client>,
|
||||||
|
cache: State,
|
||||||
) -> Box<dyn Future<Item = HttpResponse, Error = Error>>
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>>
|
||||||
where
|
where
|
||||||
S: 'static + Stream<Item = Bytes, Error = PayloadError>,
|
S: 'static + Stream<Item = Bytes, Error = PayloadError>,
|
||||||
@ -232,6 +253,10 @@ impl Service for GitLab {
|
|||||||
respo
|
respo
|
||||||
.json::<Self::Response>()
|
.json::<Self::Response>()
|
||||||
.map(move |resp| {
|
.map(move |resp| {
|
||||||
|
if let Ok(mut cache) = cache.write() {
|
||||||
|
let key = data.to_key::<Self>();
|
||||||
|
cache.store(key, resp.commit_ref().to_string());
|
||||||
|
}
|
||||||
HttpResponse::SeeOther()
|
HttpResponse::SeeOther()
|
||||||
.header(
|
.header(
|
||||||
LOCATION,
|
LOCATION,
|
||||||
|
Loading…
Reference in New Issue
Block a user