This commit is contained in:
2022-09-18 09:37:28 +00:00
committed by GitHub
parent 7670d13405
commit be5c5c4a94
6 changed files with 140 additions and 11 deletions

View File

@@ -1,18 +1,17 @@
use super::cors::CorsContext;
use crate::db::{open_database, PixivDownloaderDb};
use crate::db::{open_and_init_database, PixivDownloaderDb};
use crate::gettext;
use std::default::Default;
pub struct ServerContext {
pub cors: CorsContext,
pub db: Box<dyn PixivDownloaderDb + Send + Sync>,
}
impl Default for ServerContext {
fn default() -> Self {
impl ServerContext {
pub async fn default() -> Self {
Self {
cors: CorsContext::default(),
db: match open_database() {
db: match open_and_init_database().await {
Ok(db) => db,
Err(e) => panic!("{} {}", gettext("Failed to open database:"), e),
},

View File

@@ -64,9 +64,9 @@ pub struct PixivDownloaderMakeSvc {
}
impl PixivDownloaderMakeSvc {
pub fn new() -> Self {
pub async fn new() -> Self {
Self {
context: Arc::new(ServerContext::default()),
context: Arc::new(ServerContext::default().await),
routes: Arc::new(ServerRoutes::new()),
}
}
@@ -90,8 +90,8 @@ impl<T> Service<T> for PixivDownloaderMakeSvc {
}
/// Start the server
pub fn start_server(
pub async fn start_server(
addr: &SocketAddr,
) -> Result<Server<AddrIncoming, PixivDownloaderMakeSvc>, hyper::Error> {
Ok(Server::try_bind(addr)?.serve(PixivDownloaderMakeSvc::new()))
Ok(Server::try_bind(addr)?.serve(PixivDownloaderMakeSvc::new().await))
}