-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add integration test, and expose more as library
- Loading branch information
Showing
10 changed files
with
119 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rust-analyzer.linkedProjects": [ | ||
"./Cargo.toml" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#![deny(clippy::pedantic)] | ||
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] | ||
|
||
extern crate core; | ||
|
||
pub mod app_config; | ||
pub mod file_cache; | ||
pub mod filesystem; | ||
pub mod render; | ||
pub mod templates; | ||
pub mod utils; | ||
pub mod webserver; | ||
|
||
use crate::app_config::AppConfig; | ||
use crate::filesystem::FileSystem; | ||
use crate::webserver::database::{FileCache, ParsedSqlFile}; | ||
use std::env; | ||
use std::net::SocketAddr; | ||
use std::path::PathBuf; | ||
use templates::AllTemplates; | ||
use webserver::Database; | ||
|
||
pub const TEMPLATES_DIR: &str = "sqlpage/templates"; | ||
pub const MIGRATIONS_DIR: &str = "sqlpage/migrations"; | ||
|
||
pub struct AppState { | ||
pub db: Database, | ||
all_templates: AllTemplates, | ||
sql_file_cache: FileCache<ParsedSqlFile>, | ||
file_system: FileSystem, | ||
} | ||
|
||
impl AppState { | ||
pub async fn init(config: &AppConfig) -> anyhow::Result<Self> { | ||
// Connect to the database | ||
let db = Database::init(config).await?; | ||
let all_templates = AllTemplates::init()?; | ||
let web_root = get_web_root(); | ||
let mut sql_file_cache = FileCache::new(); | ||
let file_system = FileSystem::init(&web_root, &db).await; | ||
sql_file_cache.add_static( | ||
PathBuf::from("index.sql"), | ||
ParsedSqlFile::new(&db, include_str!("../index.sql")).await, | ||
); | ||
Ok(AppState { | ||
db, | ||
all_templates, | ||
sql_file_cache, | ||
file_system, | ||
}) | ||
} | ||
} | ||
|
||
pub fn get_web_root() -> PathBuf { | ||
env::var("WEB_ROOT").map_or_else( | ||
|_| PathBuf::from(&std::path::Component::CurDir), | ||
PathBuf::from, | ||
) | ||
} | ||
|
||
pub struct Config { | ||
pub listen_on: SocketAddr, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use actix_web::{ | ||
http::{self, header::ContentType}, | ||
test, | ||
}; | ||
use sqlpage::{app_config::AppConfig, webserver::http::main_handler, AppState}; | ||
|
||
#[actix_web::test] | ||
async fn test_index_ok() { | ||
let config = test_config(); | ||
let state = AppState::init(&config).await.unwrap(); | ||
let data = actix_web::web::Data::new(state); | ||
let req = test::TestRequest::default() | ||
.app_data(data) | ||
.insert_header(ContentType::plaintext()) | ||
.to_srv_request(); | ||
let resp = main_handler(req).await.unwrap(); | ||
assert_eq!(resp.status(), http::StatusCode::OK); | ||
let body = test::read_body(resp).await; | ||
assert!(body.starts_with(b"<!DOCTYPE html>")); | ||
// the body should contain the strint "It works!" and should not contain the string "error" | ||
let body = String::from_utf8(body.to_vec()).unwrap(); | ||
assert!(body.contains("It works !")); | ||
assert!(!body.contains("error")); | ||
} | ||
|
||
pub fn test_config() -> AppConfig { | ||
serde_json::from_str::<AppConfig>( | ||
r#"{ | ||
"database_url": "sqlite::memory:", | ||
"listen_on": "111.111.111.111:1" | ||
}"#, | ||
) | ||
.unwrap() | ||
} |