Add persistent databases

main
Elnu 2 years ago
parent f71cbc33be
commit 3238fcb8f3

1
.gitignore vendored

@ -1 +1,2 @@
/target
*.db

@ -17,7 +17,7 @@ ${commentForm()}
<h3 id="soudan-comments-header">Comments</h3>
<div id="soudan-comments"></div>`;
const md = window.markdownit().disable("image");
const url = "http://127.0.0.1:8080";
const url = "http://localhost:8080";
const form = document.getElementById("soudan-comment-form");
const commentContainer = document.getElementById("soudan-comments");
const commentContainerHeader = document.getElementById("soudan-comments-header");

@ -6,13 +6,17 @@ pub struct Database {
}
impl Database {
pub fn new(testing: bool) -> Result<Self> {
if !testing {
unimplemented!("Persistent databases unimplemented!");
}
let conn = Connection::open_in_memory()?;
pub fn new(testing: bool, name: &str) -> Result<Self> {
let name = name
.replace("http://", "")
.replace("https://", "");
let conn = if testing {
Connection::open_in_memory()
} else {
Connection::open(format!("{name}.db"))
}?;
conn.execute(
"CREATE TABLE comment (
"CREATE TABLE IF NOT EXISTS comment (
id INTEGER PRIMARY KEY,
email TEXT,
author TEXT,

@ -203,7 +203,7 @@ async fn main() -> Result<(), std::io::Error> {
for domain in domains.iter() {
databases.insert(
domain.to_owned(),
Mutex::new(Database::new(testing).unwrap()),
Mutex::new(Database::new(testing, domain).unwrap()),
);
}
let state = web::Data::new(AppState { databases });
@ -212,11 +212,18 @@ async fn main() -> Result<(), std::io::Error> {
.service(get_comments)
.service(post_comment)
.app_data(state.clone())
.wrap(if testing {
// Issue with CORS on POST requests,
// keeping permissive for now
.wrap(Cors::permissive() /* if testing {
Cors::permissive()
} else {
Cors::default()
})
let mut cors = Cors::default()
.allowed_methods(vec!["GET", "POST"]);
for domain in domains.iter() {
cors = cors.allowed_origin(domain);
}
cors
} */)
})
.bind(("127.0.0.1", 8080))?
.run()

Loading…
Cancel
Save