This commit is contained in:
Dominic Harris 2022-03-16 19:38:43 -04:00
parent 4393106cd0
commit 99d4986f2b
No known key found for this signature in database
GPG Key ID: 93CCF85F3E2A4F65
4 changed files with 46 additions and 39 deletions

View File

@ -1,18 +1,22 @@
{
"server": {
"backend_host": "127.0.0.1",
"backend_port": 8000
},
"pastes": {
"character_limit": 40000,
"days_til_expiration": 7,
"id_length": 10
},
"ratelimits": {
"seconds_in_between_pastes": 2,
"allowed_pastes_before_ratelimit": 5
},
"databases": {
"postgres_uri": "postgres://postgres:postgres@localhost:5432/zer0bin"
}
"server": {
"backend_host": "127.0.0.1",
"backend_port": 8000
},
"pastes": {
"character_limit": 40000,
"days_til_expiration": 7,
"id_length": 10
},
"ratelimits": {
"seconds_in_between_pastes": 2,
"allowed_pastes_before_ratelimit": 5
},
"databases": {
"postgres_uri": "postgres://postgres:postgres@localhost:5432/zer0bin"
},
"logging": {
"on_post_paste": true,
"on_get_paste": true
}
}

View File

@ -8,6 +8,7 @@ pub struct Config {
pub pastes: PastesConfig,
pub ratelimits: RatelimitsConifg,
pub databases: DatabasesConfig,
pub logging: LoggingConfig
}
#[derive(Serialize, Deserialize, Clone)]
@ -34,6 +35,12 @@ pub struct DatabasesConfig {
pub postgres_uri: String,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct LoggingConfig {
pub on_post_paste: bool,
pub on_get_paste: bool
}
pub fn load(path: PathBuf) -> Config {
let file = File::open(path).expect("Failed to load config");
serde_json::from_reader(file).unwrap()

View File

@ -63,6 +63,10 @@ pub async fn get_paste(state: web::Data<AppState>, id: web::Path<String>) -> imp
.execute(&state.pool)
.await;
if state.config.logging.on_get_paste {
println!("[GET] id={} views={}", id, p.views + 1);
}
HttpResponse::Ok().json(ApiResponse {
success: true,
data: GetPasteResponse {
@ -165,41 +169,29 @@ pub async fn new_paste(
Some(Utc::now() + Duration::days(state.config.pastes.days_til_expiration))
};
/*
.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;');
*/
// let cleaned = data
// .content
// .clone()
// .replace("&", "&amp;")
// .replace("<", "&lt;")
// .replace(">", "&gt;")
// .replace(r#"""#, "&quot;");
let cleaned = data
.content
.clone();
let content = data.content.clone();
let res =
sqlx::query(r#"INSERT INTO pastes("id", "content", "expires_at") VALUES ($1, $2, $3)"#)
.bind(id.clone())
.bind(cleaned.clone())
.bind(content.clone())
.bind(expires_at)
.execute(&state.pool)
.await;
match res {
Ok(_) => HttpResponse::Ok().json(ApiResponse {
Ok(_) => {
if state.config.logging.on_post_paste {
println!("[POST] id={} length={}", id, content.len());
}
HttpResponse::Ok().json(ApiResponse {
success: true,
data: NewPasteResponse {
id,
content: cleaned,
content,
},
}),
})},
Err(e) => {
eprintln!("Error occurred while creating paste: {:?}", e);

View File

@ -166,14 +166,18 @@ function viewPaste(content: string, views: string) {
enable(newButton)
enable(copyButton)
hide(editor)
show(codeViewPre)
show(viewCounterLabel)
show(viewCounter)
viewCounter.textContent = views
try {
wrapper.classList.remove("text-area-proper")
} catch (error) {}
Scrollbar.init(document.querySelector(".scrollbar-container"))
viewCounter.textContent = views
}
saveButton.addEventListener("click", async function () {