Skip to content

Commit

Permalink
feat: thiserror, tracingクレートの導入 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
HMasataka authored Jan 9, 2025
1 parent 8f9d6bf commit 887d13c
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 19 deletions.
164 changes: 164 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions clocking-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ edition = "2021"
clap = { version = "4.5.15", features = ["derive", "env"] }
dotenvy = "0.15.7"
rustls-pemfile = "2.1.3"
thiserror = "1.0.64"
tokio = { version = "1", features = ["full"] }
tokio-rustls = "0.26.0"
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = "0.3.18"
tracing_spanned = { git = "https://github.com/comnipl/tracing_spanned", rev = "6fed6097d13d117e4d35120f56450b156c33b77e" }
34 changes: 34 additions & 0 deletions clocking-server/src/cert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::err::ClockerError;

use std::fs::File;
use std::io::BufReader;
use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer};
use tracing::instrument;
use tracing_spanned::SpanErr;

#[instrument(skip_all, name = "read_cert_file", level = "trace")]
pub fn read_cert_file(
cert_path: String,
) -> Result<Vec<CertificateDer<'static>>, SpanErr<ClockerError>> {
let mut cert_file = File::open(cert_path).map_err(ClockerError::LoadCertFile)?;

let cert = rustls_pemfile::certs(&mut BufReader::new(&mut cert_file))
.collect::<Result<Vec<_>, _>>()
.map_err(ClockerError::LoadCertFile)?;

Ok(cert)
}

#[instrument(skip_all, name = "read_private_key_file", level = "trace")]
pub fn read_private_key_file(
private_key_path: String,
) -> Result<PrivateKeyDer<'static>, SpanErr<ClockerError>> {
let mut private_key_file =
File::open(private_key_path).map_err(ClockerError::LoadPrivateKeyFile)?;

let private_key = rustls_pemfile::private_key(&mut BufReader::new(&mut private_key_file))
.map_err(ClockerError::LoadPrivateKeyFile)?
.ok_or(ClockerError::ReadPrivateKeyPEMSection)?;

Ok(private_key)
}
29 changes: 29 additions & 0 deletions clocking-server/src/err.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::{io, string::FromUtf8Error};

use thiserror::Error;
use tokio_rustls::rustls;
use tracing_subscriber::util::TryInitError;

#[derive(Error, Debug)]
pub enum ClockerError {
#[error("initialize tracing subscriber error: {0}")]
InitializeTracingSubscriber(TryInitError),
#[error("create tcp listener error: {0}, port: {1}")]
CreateTCPListener(io::Error, u16),
#[error("failed to accept new connection")]
AcceptNewConnection(io::Error),
#[error("failed to accept new stream")]
AcceptNewStream(io::Error),
#[error("private key pem section not found")]
ReadPrivateKeyPEMSection,
#[error("build tls server error: {0}")]
BuildServer(rustls::Error),
#[error("failed to load cert file: {0}")]
LoadCertFile(io::Error),
#[error("failed to load private key file: {0}")]
LoadPrivateKeyFile(io::Error),
#[error("failed to read buffer: {0}")]
ReadBuffer(io::Error),
#[error("failed to convert buffer to string: {0}")]
ConvertBufferToString(FromUtf8Error),
}
Loading

0 comments on commit 887d13c

Please sign in to comment.