Add persistent databases

main
Elnu 2 years ago
parent f71cbc33be
commit 3238fcb8f3

1
.gitignore vendored

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

@ -17,7 +17,7 @@ ${commentForm()}
<h3 id="soudan-comments-header">Comments</h3> <h3 id="soudan-comments-header">Comments</h3>
<div id="soudan-comments"></div>`; <div id="soudan-comments"></div>`;
const md = window.markdownit().disable("image"); 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 form = document.getElementById("soudan-comment-form");
const commentContainer = document.getElementById("soudan-comments"); const commentContainer = document.getElementById("soudan-comments");
const commentContainerHeader = document.getElementById("soudan-comments-header"); const commentContainerHeader = document.getElementById("soudan-comments-header");

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

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

Loading…
Cancel
Save