Start implementing server

This commit is contained in:
Ellie Huxtable
2023-04-06 08:52:23 +01:00
parent 33eabd392e
commit 2f74010bad
7 changed files with 59 additions and 1 deletions

1
Cargo.lock generated
View File

@@ -135,6 +135,7 @@ version = "14.0.1"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"atuin-common", "atuin-common",
"axum",
"base64 0.21.0", "base64 0.21.0",
"chrono", "chrono",
"clap", "clap",

View File

@@ -64,5 +64,8 @@ semver = { workspace = true }
xsalsa20poly1305 = { version = "0.9.0", optional = true } xsalsa20poly1305 = { version = "0.9.0", optional = true }
generic-array = { version = "0.14", optional = true, features = ["serde"] } generic-array = { version = "0.14", optional = true, features = ["serde"] }
# daemon
axum = "0.6.4"
[dev-dependencies] [dev-dependencies]
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }

View File

@@ -0,0 +1,8 @@
const VERSION: &str = env!("CARGO_PKG_VERSION");
pub async fn index() -> Json<IndexResponse> {
Json(IndexResponse {
version: VERSION.to_string(),
})
}

View File

@@ -0,0 +1 @@
pub mod router;

View File

@@ -0,0 +1,44 @@
use std::net::{IpAddr, SocketAddr};
use axum::{
response::IntoResponse,
routing::{get},
Router,
Server,
};
use eyre::{Result};
use crate::settings::Settings;
async fn teapot() -> impl IntoResponse {
(http::StatusCode::IM_A_TEAPOT, "")
}
fn router(
settings: Settings,
) -> Router {
let routes = Router::new()
.route("/", get(handlers::index));
let path = settings.path.as_str();
if path.is_empty() {
routes
} else {
Router::new().nest(path, routes)
}
.fallback(teapot)
.with_state(AppState { database, settings })
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
}
pub async fn listen(settings: Settings, host: String, port: u16) -> Result<()> {
let host = host.parse::<IpAddr>()?;
let r = router(settings);
Server::bind(&SocketAddr::new(host, port))
.serve(r.into_make_service())
.await?;
Ok(())
}

View File

@@ -10,6 +10,7 @@ pub mod encryption;
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub mod sync; pub mod sync;
pub mod daemon;
pub mod database; pub mod database;
pub mod history; pub mod history;
pub mod import; pub mod import;

View File

@@ -6,7 +6,7 @@ use tracing_subscriber::FmtSubscriber;
use daemonize::Daemonize; use daemonize::Daemonize;
use atuin_client::settings::Settings; use atuin_client::{daemon, settings::Settings};
use tracing::{error, info, Level}; use tracing::{error, info, Level};
pub fn start(settings: Settings) -> Result<()> { pub fn start(settings: Settings) -> Result<()> {