mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-23 00:59:18 +02:00
something
This commit is contained in:
parent
4e6172d222
commit
fbbcede694
8 changed files with 788 additions and 18 deletions
71
src/logger/mod.rs
Normal file
71
src/logger/mod.rs
Normal file
|
@ -0,0 +1,71 @@
|
|||
use std::io::{self, Write};
|
||||
use std::thread;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use chrono::NaiveDateTime;
|
||||
|
||||
pub struct Logger;
|
||||
|
||||
impl Logger {
|
||||
fn print_colored(message: &str, color_code: &str) {
|
||||
// ANSI escape codes for color formatting
|
||||
const ANSI_RESET: &str = "\x1B[0m";
|
||||
const ANSI_DEFAULT: &str = "\x1B[39m";
|
||||
|
||||
let colored_message = format!("{}{}{}\n", color_code, message, ANSI_RESET);
|
||||
|
||||
// Print the colored message to stdout
|
||||
print!("{}", colored_message);
|
||||
io::stdout().flush().unwrap(); // Ensure the message is immediately printed
|
||||
print!("{}", ANSI_DEFAULT); // Reset color to default for subsequent text
|
||||
}
|
||||
|
||||
fn log(level: &str, message: &str) {
|
||||
let binding = thread::current();
|
||||
let thread_name = binding.name().unwrap_or("unnamed");
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("SystemTime before UNIX EPOCH!")
|
||||
.as_secs();
|
||||
|
||||
let formatted_time = format!("[{}]", Logger::format_time(timestamp));
|
||||
let log_entry = format!(
|
||||
"{} [{}/{}] {}",
|
||||
formatted_time, thread_name, level, message
|
||||
);
|
||||
|
||||
match level {
|
||||
"INFO" => println!("{}", log_entry),
|
||||
"WARN" => Logger::print_colored(&log_entry, "\x1B[33m"), // Orange
|
||||
"ERROR" => Logger::print_colored(&log_entry, "\x1B[31m"), // Red
|
||||
"PANIC" => Logger::print_colored(&log_entry, "\x1B[35m"), // Magenta
|
||||
_ => println!("{}", log_entry), // Default case (e.g., for custom log levels)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_time(timestamp: u64) -> String {
|
||||
let datetime = NaiveDateTime::from_timestamp(timestamp as i64, 0);
|
||||
datetime.format("%H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
pub fn info(message: &str) {
|
||||
Self::log("INFO", message);
|
||||
}
|
||||
|
||||
pub fn warn(message: &str) {
|
||||
Self::log("WARN", message);
|
||||
}
|
||||
|
||||
pub fn error(message: &str) {
|
||||
Self::log("ERROR", message);
|
||||
}
|
||||
|
||||
pub fn panic(message: &str) {
|
||||
Self::log("PANIC", message);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Logger::info("This is an informational log message.");
|
||||
Logger::warn("This is a warning log message.");
|
||||
Logger::error("This is an error log message.");
|
||||
}
|
44
src/main.rs
44
src/main.rs
|
@ -1,13 +1,51 @@
|
|||
use warp::Filter;
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
|
||||
mod v1;
|
||||
mod logger;
|
||||
|
||||
use logger::Logger;
|
||||
|
||||
|
||||
// load .env file
|
||||
fn load_env() -> Vec<String> {
|
||||
dotenv().ok();
|
||||
|
||||
let env_var_value = match env::var("YOUR_ENV_VARIABLE") {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
Logger::panic("Environment variable not found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
env_var_value.split('.').map(String::from).collect()
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
// load .env file
|
||||
load_env();
|
||||
|
||||
|
||||
|
||||
|
||||
logger::Logger::info("Starting server on {}:{}", );
|
||||
logger::Logger::warn("This is a warning!");
|
||||
logger::Logger::error("This is an error!");
|
||||
|
||||
v1::builtin_help();
|
||||
|
||||
//print env variables
|
||||
println!("PORT: {}", env::var("API_PORT").unwrap());
|
||||
|
||||
// GET /hello/warp => 200 OK with body "Hello, warp!"
|
||||
let hello = warp::path!("hello" / String)
|
||||
.map(|name| format!("Hello, {}!", name));
|
||||
|
||||
let root = warp::path::end()
|
||||
.map(|| "Hello, World!");
|
||||
.map(|| warp::reply::html("<h1>Hello, World!</h1>"));
|
||||
|
||||
|
||||
let routes = hello.or(root);
|
||||
|
@ -15,8 +53,4 @@ async fn main() {
|
|||
warp::serve(routes)
|
||||
.run(([127, 0, 0, 1], 3030))
|
||||
.await;
|
||||
|
||||
warp::serve(routes)
|
||||
.run(([192,168,0,55], 3030))
|
||||
.await;
|
||||
}
|
||||
|
|
11
src/v1/builtin/mod.rs
Normal file
11
src/v1/builtin/mod.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
pub fn help() -> &'static str {
|
||||
return "Please refer to the wiki at https://wiki.jonasjones.dev/Api/"
|
||||
}
|
||||
|
||||
pub fn ping() -> &'static str {
|
||||
return "pong"
|
||||
}
|
||||
|
||||
pub fn version() -> &'static str {
|
||||
return option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
|
||||
}
|
5
src/v1/mod.rs
Normal file
5
src/v1/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod builtin;
|
||||
|
||||
pub use builtin::help as builtin_help;
|
||||
pub use builtin::ping as builtin_ping;
|
||||
pub use builtin::version as builtin_version;
|
Loading…
Add table
Add a link
Reference in a new issue