2021-10-31 15:13:04 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-31 22:26:42 +05:30
|
|
|
use std::ops::{Bound, RangeBounds};
|
|
|
|
|
2021-11-02 17:56:39 +05:30
|
|
|
use actix_web::{http::header, web, HttpResponse, Responder};
|
|
|
|
use reqwest::header::CONTENT_TYPE;
|
2021-10-31 22:26:42 +05:30
|
|
|
use sailfish::TemplateOnce;
|
2021-10-31 15:13:04 +05:30
|
|
|
|
2021-11-02 15:30:25 +05:30
|
|
|
use crate::data::PostResp;
|
2021-10-31 22:26:42 +05:30
|
|
|
use crate::AppData;
|
2021-10-31 15:13:04 +05:30
|
|
|
|
2021-11-02 17:56:39 +05:30
|
|
|
const CACHE_AGE: u32 = 60 * 60 * 24;
|
|
|
|
|
2021-10-31 15:13:04 +05:30
|
|
|
pub mod routes {
|
|
|
|
pub struct Proxy {
|
2021-10-31 23:26:15 +05:30
|
|
|
pub index: &'static str,
|
2021-10-31 15:13:04 +05:30
|
|
|
pub page: &'static str,
|
2021-11-02 17:56:39 +05:30
|
|
|
pub asset: &'static str,
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
impl Proxy {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
2021-10-31 23:26:15 +05:30
|
|
|
index: "/",
|
2021-10-31 15:13:04 +05:30
|
|
|
page: "/{username}/{post}",
|
2021-11-02 17:56:39 +05:30
|
|
|
asset: "/asset/medium/{name}",
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn get_page(&self, username: &str, post: &str) -> String {
|
|
|
|
self.page
|
|
|
|
.replace("{username}", username)
|
|
|
|
.replace("{post}", post)
|
|
|
|
}
|
2021-11-02 17:56:39 +05:30
|
|
|
|
|
|
|
pub fn get_medium_asset(&self, asset_name: &str) -> String {
|
|
|
|
self.asset.replace("{name}", asset_name)
|
|
|
|
}
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 22:26:42 +05:30
|
|
|
// credits @carlomilanesi:
|
|
|
|
// https://users.rust-lang.org/t/how-to-get-a-substring-of-a-string/1351/11
|
|
|
|
trait StringUtils {
|
|
|
|
fn substring(&self, start: usize, len: usize) -> &str;
|
|
|
|
fn slice(&self, range: impl RangeBounds<usize>) -> &str;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StringUtils for str {
|
|
|
|
fn substring(&self, start: usize, len: usize) -> &str {
|
|
|
|
let mut char_pos = 0;
|
|
|
|
let mut byte_start = 0;
|
|
|
|
let mut it = self.chars();
|
|
|
|
loop {
|
|
|
|
if char_pos == start {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if let Some(c) = it.next() {
|
|
|
|
char_pos += 1;
|
|
|
|
byte_start += c.len_utf8();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char_pos = 0;
|
|
|
|
let mut byte_end = byte_start;
|
|
|
|
loop {
|
|
|
|
if char_pos == len {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if let Some(c) = it.next() {
|
|
|
|
char_pos += 1;
|
|
|
|
byte_end += c.len_utf8();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&self[byte_start..byte_end]
|
|
|
|
}
|
|
|
|
fn slice(&self, range: impl RangeBounds<usize>) -> &str {
|
|
|
|
let start = match range.start_bound() {
|
|
|
|
Bound::Included(bound) | Bound::Excluded(bound) => *bound,
|
|
|
|
Bound::Unbounded => 0,
|
|
|
|
};
|
|
|
|
let len = match range.end_bound() {
|
|
|
|
Bound::Included(bound) => *bound + 1,
|
|
|
|
Bound::Excluded(bound) => *bound,
|
|
|
|
Bound::Unbounded => self.len(),
|
|
|
|
} - start;
|
|
|
|
self.substring(start, len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(TemplateOnce)]
|
2021-10-31 23:26:15 +05:30
|
|
|
#[template(path = "post.html")]
|
2021-11-02 15:30:25 +05:30
|
|
|
#[template(rm_whitespace = true)]
|
2021-10-31 22:26:42 +05:30
|
|
|
pub struct Post {
|
2021-11-02 15:30:25 +05:30
|
|
|
pub data: PostResp,
|
2021-10-31 22:26:42 +05:30
|
|
|
pub id: String,
|
|
|
|
}
|
|
|
|
|
2021-10-31 23:26:15 +05:30
|
|
|
const INDEX: &str = include_str!("../templates/index.html");
|
|
|
|
|
|
|
|
#[my_codegen::get(path = "crate::V1_API_ROUTES.proxy.index")]
|
|
|
|
async fn index() -> impl Responder {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(INDEX)
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:56:39 +05:30
|
|
|
#[my_codegen::get(path = "crate::V1_API_ROUTES.proxy.asset")]
|
|
|
|
async fn assets(path: web::Path<String>, data: AppData) -> impl Responder {
|
|
|
|
println!("asset name: {}", path);
|
|
|
|
let res = data
|
|
|
|
.client
|
|
|
|
.get(format!("https://miro.medium.com/{}", path))
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
print!("got res");
|
|
|
|
let headers = res.headers();
|
|
|
|
let content_type = headers.get(CONTENT_TYPE).unwrap();
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type(content_type)
|
|
|
|
.body(res.bytes().await.unwrap())
|
|
|
|
}
|
|
|
|
|
2021-10-31 15:13:04 +05:30
|
|
|
#[my_codegen::get(path = "crate::V1_API_ROUTES.proxy.page")]
|
2021-10-31 22:26:42 +05:30
|
|
|
async fn page(path: web::Path<(String, String)>, data: AppData) -> impl Responder {
|
2021-11-02 17:56:39 +05:30
|
|
|
let post_id = path.1.split('-').last();
|
2021-10-31 15:13:04 +05:30
|
|
|
if post_id.is_none() {
|
2021-10-31 22:26:42 +05:30
|
|
|
return HttpResponse::BadRequest().finish();
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|
2021-11-02 15:30:25 +05:30
|
|
|
let id = post_id.unwrap();
|
2021-10-31 15:13:04 +05:30
|
|
|
|
2021-10-31 22:26:42 +05:30
|
|
|
let page = Post {
|
2021-11-02 15:30:25 +05:30
|
|
|
id: id.to_owned(),
|
2021-11-02 17:56:39 +05:30
|
|
|
data: data.get_post(id).await,
|
2021-10-31 22:26:42 +05:30
|
|
|
}
|
|
|
|
.render_once()
|
|
|
|
.unwrap();
|
2021-10-31 15:13:04 +05:30
|
|
|
HttpResponse::Ok()
|
2021-11-02 17:56:39 +05:30
|
|
|
.insert_header(header::CacheControl(vec![
|
|
|
|
header::CacheDirective::Public,
|
|
|
|
header::CacheDirective::Extension("immutable".into(), None),
|
|
|
|
header::CacheDirective::MaxAge(CACHE_AGE),
|
|
|
|
]))
|
2021-10-31 22:26:42 +05:30
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(page)
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
2021-11-02 17:56:39 +05:30
|
|
|
cfg.service(assets);
|
2021-10-31 15:13:04 +05:30
|
|
|
cfg.service(page);
|
2021-10-31 23:26:15 +05:30
|
|
|
cfg.service(index);
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use actix_web::{http::StatusCode, test, App};
|
|
|
|
|
2021-10-31 22:26:42 +05:30
|
|
|
use crate::{services, Data};
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn deploy_update_works() {
|
|
|
|
let data = Data::new();
|
|
|
|
let app = test::init_service(App::new().app_data(data.clone()).configure(services)).await;
|
|
|
|
let urls = vec![
|
|
|
|
"/@ftrain/big-data-small-effort-b62607a43a8c",
|
|
|
|
"/geekculture/rest-api-best-practices-decouple-long-running-tasks-from-http-request-processing-9fab2921ace8",
|
|
|
|
"/illumination/5-bugs-that-turned-into-features-e9a0e972a4e7",
|
2021-11-02 17:56:39 +05:30
|
|
|
"/",
|
|
|
|
"/asset/medium/1*LY2ohYsNa9nOV1Clko3zJA.png",
|
2021-10-31 22:26:42 +05:30
|
|
|
];
|
|
|
|
|
|
|
|
for uri in urls.iter() {
|
|
|
|
let resp =
|
|
|
|
test::call_service(&app, test::TestRequest::get().uri(uri).to_request()).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 15:13:04 +05:30
|
|
|
}
|