-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
55 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod notifier; | ||
pub mod server; | ||
pub mod state; |
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,55 @@ | ||
use std::path::PathBuf; | ||
|
||
use a2::{Client, Endpoint}; | ||
use anyhow::{Context as _, Result}; | ||
use async_std::sync::Arc; | ||
use log::*; | ||
use std::io::Seek; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct State { | ||
inner: Arc<InnerState>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct InnerState { | ||
db: sled::Db, | ||
|
||
production_client: Client, | ||
|
||
sandbox_client: Client, | ||
} | ||
|
||
impl State { | ||
pub fn new(db: &PathBuf, mut certificate: std::fs::File, password: &str) -> Result<Self> { | ||
let db = sled::open(db)?; | ||
let production_client = | ||
Client::certificate(&mut certificate, password, Endpoint::Production) | ||
.context("Failed to create production client")?; | ||
certificate.rewind()?; | ||
let sandbox_client = Client::certificate(&mut certificate, password, Endpoint::Sandbox) | ||
.context("Failed to create sandbox client")?; | ||
|
||
info!("{} devices registered currently", db.len()); | ||
|
||
Ok(State { | ||
inner: Arc::new(InnerState { | ||
db, | ||
production_client, | ||
sandbox_client, | ||
}), | ||
}) | ||
} | ||
|
||
pub fn db(&self) -> &sled::Db { | ||
&self.inner.db | ||
} | ||
|
||
pub fn production_client(&self) -> &Client { | ||
&self.inner.production_client | ||
} | ||
|
||
pub fn sandbox_client(&self) -> &Client { | ||
&self.inner.sandbox_client | ||
} | ||
} |