Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WJ-1283] Change time serialization format #2128

Merged
merged 13 commits into from
Oct 8, 2024
Merged
2 changes: 1 addition & 1 deletion deepwell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ strum = "0.26"
strum_macros = "0.26"
subtle = "2.6"
thiserror = "1"
time = { version = "0.3", features = ["parsing", "serde", "serde-human-readable"], default-features = false }
time = { version = "0.3", features = ["parsing", "serde"], default-features = false }
tiny-keccak = { version = "2", features = ["k12"] }
toml = { version = "0.8", features = ["parse"] }
tokio = { version = "1", features = ["full"] }
Expand Down
11 changes: 5 additions & 6 deletions deepwell/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
use crate::config::{Config, Secrets};
use crate::endpoints::{
auth::*, blob::*, category::*, domain::*, email::*, file::*, file_revision::*,
link::*, locale::*, message::*, misc::*, page::*, page_revision::*, parent::*,
site::*, site_member::*, text::*, user::*, user_bot::*, view::*, vote::*,
info::*, link::*, locale::*, message::*, misc::*, page::*, page_revision::*,
parent::*, site::*, site_member::*, text::*, user::*, user_bot::*, view::*, vote::*,
};
use crate::locales::Localizations;
use crate::services::blob::MimeAnalyzer;
Expand Down Expand Up @@ -176,13 +176,12 @@ async fn build_module(app_state: ServerState) -> anyhow::Result<RpcModule<Server
register!("ping", ping);
register!("echo", echo);
register!("error", yield_error);
register!("version", version);
register!("version_full", full_version);
register!("hostname", hostname);
register!("config", config_dump);
register!("config_path", config_path);
register!("normalize", normalize_method);

// Server Information
register!("info", server_info);

// Localization
register!("locale", locale_info);
register!("translate", translate_strings);
Expand Down
87 changes: 87 additions & 0 deletions deepwell/src/endpoints/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* endpoints/info.rs
*
* DEEPWELL - Wikijump API provider and database manager
* Copyright (C) 2019-2024 Wikijump Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

//! Endpoints associated with getting DEEPWELL daemon information.

use super::prelude::*;
use crate::info;
use crate::utils::now;
use std::path::PathBuf;
use time::OffsetDateTime;

#[derive(Serialize, Debug, Clone)]
pub struct Info {
package: PackageInfo,
compile_info: CompileInfo,

#[serde(with = "time::serde::rfc3339")]
current_time: OffsetDateTime,
hostname: &'static str,
config_path: PathBuf,
}

#[derive(Serialize, Debug, Clone)]
pub struct PackageInfo {
name: &'static str,
description: &'static str,
license: &'static str,
repository: &'static str,
version: &'static str,
}

#[derive(Serialize, Debug, Clone)]
pub struct CompileInfo {
#[serde(with = "time::serde::rfc3339")]
built_at: OffsetDateTime,
rustc_version: &'static str,
endian: &'static str,
target: &'static str,
threads: u32,
git_commit: Option<&'static str>,
}

pub async fn server_info(
ctx: &ServiceContext<'_>,
_params: Params<'static>,
) -> Result<Info> {
let config = ctx.config();

info!("Building server information response");
Ok(Info {
package: PackageInfo {
name: info::PKG_NAME,
version: &info::VERSION_INFO,
description: info::PKG_DESCRIPTION,
license: info::PKG_LICENSE,
repository: info::PKG_REPOSITORY,
},
compile_info: CompileInfo {
built_at: *info::BUILT_TIME_UTC,
rustc_version: info::RUSTC_VERSION,
endian: info::CFG_ENDIAN,
target: info::TARGET,
threads: info::NUM_JOBS,
git_commit: info::GIT_COMMIT_HASH,
},
config_path: config.raw_toml_path.clone(),
hostname: &info::HOSTNAME,
current_time: now(),
})
}
34 changes: 0 additions & 34 deletions deepwell/src/endpoints/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
*/

use super::prelude::*;
use crate::info;
use sea_orm::{ConnectionTrait, DatabaseBackend, Statement};
use serde_json::Value as JsonValue;
use std::path::PathBuf;
use wikidot_normalize::normalize;

async fn postgres_check(ctx: &ServiceContext<'_>) -> Result<()> {
Expand Down Expand Up @@ -78,30 +76,6 @@ pub async fn yield_error(
Err(ServiceError::BadRequest)
}

pub async fn version(
_ctx: &ServiceContext<'_>,
_params: Params<'static>,
) -> Result<&'static str> {
info!("Getting DEEPWELL version");
Ok(info::VERSION.as_str())
}

pub async fn full_version(
_ctx: &ServiceContext<'_>,
_params: Params<'static>,
) -> Result<&'static str> {
info!("Getting DEEPWELL version (full)");
Ok(info::FULL_VERSION.as_str())
}

pub async fn hostname(
_ctx: &ServiceContext<'_>,
_params: Params<'static>,
) -> Result<&'static str> {
info!("Getting DEEPWELL hostname");
Ok(info::HOSTNAME.as_str())
}

pub async fn config_dump(
ctx: &ServiceContext<'_>,
_params: Params<'static>,
Expand All @@ -110,14 +84,6 @@ pub async fn config_dump(
Ok(ctx.config().raw_toml.to_string())
}

pub async fn config_path(
ctx: &ServiceContext<'_>,
_params: Params<'static>,
) -> Result<PathBuf> {
info!("Dumping DEEPWELL configuration path for debugging");
Ok(ctx.config().raw_toml_path.to_path_buf())
}

pub async fn normalize_method(
_ctx: &ServiceContext<'_>,
params: Params<'static>,
Expand Down
1 change: 1 addition & 0 deletions deepwell/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub mod domain;
pub mod email;
pub mod file;
pub mod file_revision;
pub mod info;
pub mod link;
pub mod locale;
pub mod message;
Expand Down
31 changes: 20 additions & 11 deletions deepwell/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ mod build {
}

use once_cell::sync::Lazy;
use time::format_description::well_known::Rfc2822;
use time::OffsetDateTime;

#[allow(unused_imports)]
pub use self::build::{
BUILT_TIME_UTC, GIT_COMMIT_HASH, NUM_JOBS, PKG_AUTHORS, PKG_DESCRIPTION, PKG_LICENSE,
PKG_NAME, PKG_REPOSITORY, PKG_VERSION, RUSTC_VERSION, TARGET,
BUILT_TIME_UTC as BUILT_TIME_UTC_STR, CFG_ENDIAN, GIT_COMMIT_HASH, NUM_JOBS,
PKG_AUTHORS, PKG_DESCRIPTION, PKG_LICENSE, PKG_NAME, PKG_REPOSITORY, PKG_VERSION,
RUSTC_VERSION, TARGET,
};

pub static BUILT_TIME_UTC: Lazy<OffsetDateTime> = Lazy::new(|| {
OffsetDateTime::parse(BUILT_TIME_UTC_STR, &Rfc2822)
.expect("Unable to parse built time string")
});

pub static VERSION_INFO: Lazy<String> = Lazy::new(|| {
let mut version = format!("v{PKG_VERSION}");

Expand All @@ -40,19 +48,20 @@ pub static VERSION_INFO: Lazy<String> = Lazy::new(|| {
version
});

pub static FULL_VERSION: Lazy<String> = Lazy::new(|| {
let mut version = format!("{PKG_NAME} {}\n\nCompiled:\n", *VERSION_INFO);

str_writeln!(&mut version, "* across {NUM_JOBS} threads");
str_writeln!(&mut version, "* by {RUSTC_VERSION}");
str_writeln!(&mut version, "* for {TARGET}");
str_writeln!(&mut version, "* on {BUILT_TIME_UTC}");

version
pub static COMPILE_INFO: Lazy<String> = Lazy::new(|| {
let mut info = str!("Compile info:\n");
str_writeln!(&mut info, "* on {BUILT_TIME_UTC_STR}");
str_writeln!(&mut info, "* by {RUSTC_VERSION}");
str_writeln!(&mut info, "* for {TARGET}");
str_writeln!(&mut info, "* across {NUM_JOBS} threads");
info
});

pub static VERSION: Lazy<String> = Lazy::new(|| format!("{PKG_NAME} {}", *VERSION_INFO));

pub static FULL_VERSION: Lazy<String> =
Lazy::new(|| format!("{}\n\n{}", *VERSION, *COMPILE_INFO));

pub static GIT_COMMIT_HASH_SHORT: Lazy<Option<&'static str>> =
Lazy::new(|| build::GIT_COMMIT_HASH.map(|s| &s[..8]));

Expand Down
1 change: 1 addition & 0 deletions deepwell/src/models/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub alias_id: i64,
pub alias_type: AliasType,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
pub created_by: i64,
pub target_id: i64,
Expand Down
2 changes: 2 additions & 0 deletions deepwell/src/models/blob_pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub external_id: String,
pub created_by: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339")]
pub expires_at: TimeDateTimeWithTimeZone,
pub expected_length: i64,
#[sea_orm(column_type = "Text")]
Expand Down
3 changes: 3 additions & 0 deletions deepwell/src/models/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub file_id: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
#[serde(with = "time::serde::rfc3339::option")]
pub deleted_at: Option<TimeDateTimeWithTimeZone>,
pub from_wikidot: bool,
#[sea_orm(column_type = "Text")]
Expand Down
1 change: 1 addition & 0 deletions deepwell/src/models/file_revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub revision_id: i64,
pub revision_type: FileRevisionType,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
pub revision_number: i32,
pub file_id: i64,
Expand Down
3 changes: 3 additions & 0 deletions deepwell/src/models/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub filter_id: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
#[serde(with = "time::serde::rfc3339::option")]
pub deleted_at: Option<TimeDateTimeWithTimeZone>,
pub site_id: Option<i64>,
pub affects_user: bool,
Expand Down
3 changes: 3 additions & 0 deletions deepwell/src/models/message_draft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub external_id: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
pub user_id: i64,
pub recipients: Json,
Expand All @@ -18,6 +20,7 @@ pub struct Model {
pub wikitext_hash: Vec<u8>,
#[sea_orm(column_type = "VarBinary(StringLen::None)")]
pub compiled_hash: Vec<u8>,
#[serde(with = "time::serde::rfc3339")]
pub compiled_at: TimeDateTimeWithTimeZone,
#[sea_orm(column_type = "Text")]
pub compiled_generator: String,
Expand Down
4 changes: 4 additions & 0 deletions deepwell/src/models/message_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub external_id: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339")]
pub drafted_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub retracted_at: Option<TimeDateTimeWithTimeZone>,
pub sender_id: i64,
#[sea_orm(column_type = "Text")]
Expand All @@ -18,6 +21,7 @@ pub struct Model {
pub wikitext_hash: Vec<u8>,
#[sea_orm(column_type = "VarBinary(StringLen::None)")]
pub compiled_hash: Vec<u8>,
#[serde(with = "time::serde::rfc3339")]
pub compiled_at: TimeDateTimeWithTimeZone,
#[sea_orm(column_type = "Text")]
pub compiled_generator: String,
Expand Down
2 changes: 2 additions & 0 deletions deepwell/src/models/message_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub struct Model {
pub message_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub reported_to_site_id: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
#[sea_orm(column_type = "Text")]
pub reason: String,
Expand Down
3 changes: 3 additions & 0 deletions deepwell/src/models/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub page_id: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
#[serde(with = "time::serde::rfc3339::option")]
pub deleted_at: Option<TimeDateTimeWithTimeZone>,
pub from_wikidot: bool,
pub site_id: i64,
Expand Down
1 change: 1 addition & 0 deletions deepwell/src/models/page_attribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Model {
pub attribution_type: String,
#[sea_orm(primary_key, auto_increment = false)]
pub attribution_date: TimeDate,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
}

Expand Down
2 changes: 2 additions & 0 deletions deepwell/src/models/page_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub category_id: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
pub site_id: i64,
#[sea_orm(column_type = "Text")]
Expand Down
2 changes: 2 additions & 0 deletions deepwell/src/models/page_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct Model {
pub to_page_id: i64,
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub connection_type: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
pub count: i32,
}
Expand Down
2 changes: 2 additions & 0 deletions deepwell/src/models/page_connection_missing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub struct Model {
pub to_page_slug: String,
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub connection_type: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: TimeDateTimeWithTimeZone,
#[serde(with = "time::serde::rfc3339::option")]
pub updated_at: Option<TimeDateTimeWithTimeZone>,
pub count: i32,
}
Expand Down
Loading
Loading