1
1
mirror of https://github.com/ellie/atuin.git synced 2024-10-05 17:07:20 +03:00

make atuin-server a standalone binary

This commit is contained in:
Conrad Ludgate 2023-03-09 09:22:57 +00:00
parent 1638cb57cb
commit 44780e4251
No known key found for this signature in database
GPG Key ID: 197E3CACA1C980B5
10 changed files with 107 additions and 74 deletions

3
Cargo.lock generated
View File

@ -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",
]

View File

@ -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

View File

@ -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"]

View File

@ -3,9 +3,9 @@
use serde::{Deserialize, Serialize};
pub enum TimePeriod {
YEAR,
MONTH,
DAY,
Year,
Month,
Day,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@ -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) {

View File

@ -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")

View File

@ -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
}
}
}
}

39
atuin-server/src/main.rs Normal file
View File

@ -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()
}

View File

@ -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),

View File

@ -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
}
}
}
}