summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorConrad Ludgate <conrad.ludgate@truelayer.com>2023-03-09 09:22:57 +0000
committerConrad Ludgate <conrad.ludgate@truelayer.com>2023-03-09 12:19:24 +0000
commit44780e4251032e0f6e9b5e3012df5180bd9326bf (patch)
tree00527b511e6353b5afacddc0389b6f856021de94
parent1638cb57cb8897ee463b91ed67423fb5d8b8aebb (diff)
make atuin-server a standalone binary03-09-make_atuin-server_a_standalone_binary
-rw-r--r--Cargo.lock3
-rw-r--r--Cargo.toml8
-rw-r--r--atuin-server/Cargo.toml8
-rw-r--r--atuin-server/src/calendar.rs6
-rw-r--r--atuin-server/src/database.rs6
-rw-r--r--atuin-server/src/handlers/history.rs6
-rw-r--r--atuin-server/src/lib.rs56
-rw-r--r--atuin-server/src/main.rs39
-rw-r--r--src/command/mod.rs5
-rw-r--r--src/command/server.rs44
10 files changed, 107 insertions, 74 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c4f0c8c2..b14fcaa1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -100,7 +100,6 @@ dependencies = [
"serde_json",
"tiny-bip39",
"tokio",
- "tracing-subscriber",
"unicode-segmentation",
"unicode-width",
"whoami",
@@ -164,6 +163,7 @@ dependencies = [
"base64 0.21.0",
"chrono",
"chronoutil",
+ "clap",
"config",
"eyre",
"fs-err",
@@ -178,6 +178,7 @@ dependencies = [
"tower",
"tower-http",
"tracing",
+ "tracing-subscriber",
"uuid",
"whoami",
]
diff --git a/Cargo.toml b/Cargo.toml
index 5b47505e..56901f91 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -41,7 +41,7 @@ members = ["./atuin-client", "./atuin-server", "./atuin-common"]
default = ["client", "sync", "server"]
client = ["atuin-client"]
sync = ["atuin-client/sync"]
-server = ["atuin-server", "tracing-subscriber"]
+server = ["atuin-server"]
[dependencies]
atuin-server = { path = "atuin-server", version = "13.0.1", optional = true }
@@ -78,9 +78,3 @@ futures-util = "0.3"
bitflags = "1.3"
cassowary = "0.3"
unicode-segmentation = "1.2"
-
-[dependencies.tracing-subscriber]
-version = "0.3"
-default-features = false
-features = ["ansi", "fmt", "registry", "env-filter"]
-optional = true
diff --git a/atuin-server/Cargo.toml b/atuin-server/Cargo.toml
index 59afa0ac..07768fe9 100644
--- a/atuin-server/Cargo.toml
+++ b/atuin-server/Cargo.toml
@@ -16,6 +16,7 @@ chrono = { version = "0.4", features = ["serde"] }
eyre = "0.6"
uuid = { version = "1.2", features = ["v4"] }
whoami = "1.1.2"
+clap = { version = "4.0.18", features = ["derive"] }
config = { version = "0.13", default-features = false, features = ["toml"] }
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.86"
@@ -34,8 +35,13 @@ http = "0.2"
fs-err = "2.9"
chronoutil = "0.2.3"
tower = "0.4"
-tower-http = { version = "0.3", features = ["trace"] }
+tower-http = { version = "0.3.4", features = ["trace"] }
reqwest = { version = "0.11", features = [
"json",
"rustls-tls-native-roots",
], default-features = false }
+
+[dependencies.tracing-subscriber]
+version = "0.3"
+default-features = false
+features = ["ansi", "fmt", "registry", "env-filter"]
diff --git a/atuin-server/src/calendar.rs b/atuin-server/src/calendar.rs
index 7c05dce3..22f4d60f 100644
--- a/atuin-server/src/calendar.rs
+++ b/atuin-server/src/calendar.rs
@@ -3,9 +3,9 @@
use serde::{Deserialize, Serialize};
pub enum TimePeriod {
- YEAR,
- MONTH,
- DAY,
+ Year,
+ Month,
+ Day,
}
#[derive(Debug, Serialize, Deserialize)]
diff --git a/atuin-server/src/database.rs b/atuin-server/src/database.rs
index ef6c6d85..3f0efc06 100644
--- a/atuin-server/src/database.rs
+++ b/atuin-server/src/database.rs
@@ -364,7 +364,7 @@ impl Database for Postgres {
// interpret the stored date with a different TZ
match period {
- TimePeriod::YEAR => {
+ TimePeriod::Year => {
let mut ret = HashMap::new();
// First we need to work out how far back to calculate. Get the
// oldest history item
@@ -390,7 +390,7 @@ impl Database for Postgres {
Ok(ret)
}
- TimePeriod::MONTH => {
+ TimePeriod::Month => {
let mut ret = HashMap::new();
for month in 1..13 {
@@ -413,7 +413,7 @@ impl Database for Postgres {
Ok(ret)
}
- TimePeriod::DAY => {
+ TimePeriod::Day => {
let mut ret = HashMap::new();
for day in 1..get_days_from_month(year as i32, month as u32) {
diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs
index 7cf18323..017fc399 100644
--- a/atuin-server/src/handlers/history.rs
+++ b/atuin-server/src/handlers/history.rs
@@ -119,7 +119,7 @@ pub async fn calendar<DB: Database>(
let db = &state.0.database;
let focus = match focus {
"year" => db
- .calendar(&user, TimePeriod::YEAR, *year, *month)
+ .calendar(&user, TimePeriod::Year, *year, *month)
.await
.map_err(|_| {
ErrorResponse::reply("failed to query calendar")
@@ -127,7 +127,7 @@ pub async fn calendar<DB: Database>(
}),
"month" => db
- .calendar(&user, TimePeriod::MONTH, *year, *month)
+ .calendar(&user, TimePeriod::Month, *year, *month)
.await
.map_err(|_| {
ErrorResponse::reply("failed to query calendar")
@@ -135,7 +135,7 @@ pub async fn calendar<DB: Database>(
}),
"day" => db
- .calendar(&user, TimePeriod::DAY, *year, *month)
+ .calendar(&user, TimePeriod::Day, *year, *month)
.await
.map_err(|_| {
ErrorResponse::reply("failed to query calendar")
diff --git a/atuin-server/src/lib.rs b/atuin-server/src/lib.rs
index 571c09bb..ed733703 100644
--- a/atuin-server/src/lib.rs
+++ b/atuin-server/src/lib.rs
@@ -3,20 +3,22 @@
use std::net::{IpAddr, SocketAddr};
use axum::Server;
+use clap::Parser;
use database::Postgres;
use eyre::{Context, Result};
+use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use crate::settings::Settings;
-pub mod auth;
-pub mod calendar;
-pub mod database;
-pub mod handlers;
-pub mod models;
-pub mod router;
-pub mod settings;
+mod auth;
+mod calendar;
+mod database;
+mod handlers;
+mod models;
+mod router;
+mod settings;
-pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> {
+async fn launch(settings: Settings, host: String, port: u16) -> Result<()> {
let host = host.parse::<IpAddr>()?;
let postgres = Postgres::new(settings.clone())
@@ -31,3 +33,41 @@ pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> {
Ok(())
}
+
+#[derive(Parser)]
+#[clap(infer_subcommands = true)]
+pub enum Cmd {
+ /// Start the server
+ Start {
+ /// The host address to bind
+ #[clap(long)]
+ host: Option<String>,
+
+ /// The port to bind
+ #[clap(long, short)]
+ port: Option<u16>,
+ },
+}
+
+impl Cmd {
+ #[tokio::main]
+ pub async fn run(self) -> Result<()> {
+ tracing_subscriber::registry()
+ .with(fmt::layer())
+ .with(EnvFilter::from_default_env())
+ .init();
+
+ let settings = Settings::new().wrap_err("could not load server settings")?;
+
+ match self {
+ Self::Start { host, port } => {
+ let host = host
+ .as_ref()
+ .map_or(settings.host.clone(), std::string::ToString::to_string);
+ let port = port.map_or(settings.port, |p| p);
+
+ launch(settings, host, port).await
+ }
+ }
+ }
+}
diff --git a/atuin-server/src/main.rs b/atuin-server/src/main.rs
new file mode 100644
index 00000000..e6c2b631
--- /dev/null
+++ b/atuin-server/src/main.rs
@@ -0,0 +1,39 @@
+#![warn(clippy::pedantic, clippy::nursery)]
+#![allow(clippy::use_self, clippy::missing_const_for_fn)] // not 100% reliable
+
+use clap::Parser;
+use eyre::Result;
+
+const VERSION: &str = env!("CARGO_PKG_VERSION");
+
+static HELP_TEMPLATE: &str = "\
+{before-help}{name} {version}
+{author}
+{about}
+
+{usage-heading}
+ {usage}
+
+{all-args}{after-help}";
+
+/// Magical shell history
+#[derive(Parser)]
+#[command(
+ author = "Ellie Huxtable <e@elm.sh>",
+ version = VERSION,
+ help_template(HELP_TEMPLATE),
+)]
+struct Atuin {
+ #[command(subcommand)]
+ atuin: atuin_server::Cmd,
+}
+
+impl Atuin {
+ fn run(self) -> Result<()> {
+ self.atuin.run()
+ }
+}
+
+fn main() -> Result<()> {
+ Atuin::parse().run()
+}
diff --git a/src/command/mod.rs b/src/command/mod.rs
index 1411bfd2..cb5e61fd 100644
--- a/src/command/mod.rs
+++ b/src/command/mod.rs
@@ -5,9 +5,6 @@ use eyre::Result;
#[cfg(feature = "client")]
mod client;
-#[cfg(feature = "server")]
-mod server;
-
mod init;
mod contributors;
@@ -22,7 +19,7 @@ pub enum AtuinCmd {
/// Start an atuin server
#[cfg(feature = "server")]
#[command(subcommand)]
- Server(server::Cmd),
+ Server(atuin_server::Cmd),
/// Output shell setup
Init(init::Cmd),
diff --git a/src/command/server.rs b/src/command/server.rs
deleted file mode 100644
index 495f85d0..00000000
--- a/src/command/server.rs
+++ /dev/null
@@ -1,44 +0,0 @@
-use tracing_subscriber::{fmt, prelude::*, EnvFilter};
-
-use clap::Parser;
-use eyre::{Context, Result};
-
-use atuin_server::{launch, settings::Settings};
-
-#[derive(Parser)]
-#[clap(infer_subcommands = true)]
-pub enum Cmd {
- /// Start the server
- Start {
- /// The host address to bind
- #[clap(long)]
- host: Option<String>,
-
- /// The port to bind
- #[clap(long, short)]
- port: Option<u16>,
- },
-}
-
-impl Cmd {
- #[tokio::main]
- pub async fn run(self) -> Result<()> {
- tracing_subscriber::registry()
- .with(fmt::layer())
- .with(EnvFilter::from_default_env())
- .init();
-
- let settings = Settings::new().wrap_err("could not load server settings")?;
-
- match self {
- Self::Start { host, port } => {
- let host = host
- .as_ref()
- .map_or(settings.host.clone(), std::string::ToString::to_string);
- let port = port.map_or(settings.port, |p| p);
-
- launch(settings, host, port).await
- }
- }
- }
-}