morethantext-web/src/main.rs
2022-12-02 10:34:45 -05:00

62 lines
1.9 KiB
Rust

use tide::{
http::StatusCode,
sessions::{MemoryStore, SessionMiddleware},
Request, Response,
};
mod morethantext;
mod settings;
use morethantext::MoreThanText;
use settings::Settings;
#[async_std::main]
async fn main() -> tide::Result<()> {
let sett = Settings::new().unwrap();
let app = app_setup(sett.data_dir.as_str()).await;
app.listen(format!("{}:{}", sett.address, sett.port))
.await?;
Ok(())
}
async fn app_setup(data_dir: &str) -> tide::Server<MoreThanText> {
let db = MoreThanText::new(data_dir).await.unwrap();
let mut app = tide::with_state(db);
app.at("/").get(home);
app.with(
SessionMiddleware::new(MemoryStore::new(), b"361f953f-56ba-45e6-86ab-9efbf61b745d")
.with_cookie_name("morethantext.sid"),
);
return app;
}
async fn home(_req: Request<MoreThanText>) -> tide::Result {
let mut res = Response::new(StatusCode::Ok);
res.set_body("<!DOCTYPE html>
<html>
<body>
<h1>Welcome to BaskinPrattle.</h1>
<p>The code for the site is <a href=\"https://gitea.baskinprattle.com/jbaskin/morethantext-web\">here</a>.</p>
<p>And the latest x86_64 Linux build is <a href=\"https://jenkins.baskinprattle.com/job/morethantext-web/\">here</a>.</p>
<p>There is also an ansible role for installation <a href=\"https://gitea.baskinprattle.com/jbaskin/role-morethantext.git\">here</a>.</p>
</body>
</html>");
res.append_header("Content-Type", "text/html; charset=UTF-8");
Ok(res)
}
#[cfg(test)]
mod server_app {
use super::*;
use tempfile::tempdir;
use tide_testing::TideTestingExt;
#[async_std::test]
async fn home_page_available() {
let dir = tempdir().unwrap();
let app = app_setup(dir.path().to_str().unwrap()).await;
let response = app.get("/").await.unwrap();
assert_eq!(response.status(), StatusCode::Ok);
}
}