55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
pub mod manager;
|
|
pub mod protobuf;
|
|
|
|
use actix_web::{App, HttpResponse, HttpServer, Responder, get, web};
|
|
use chrono::Local;
|
|
use colored::{Color, Colorize};
|
|
use fern::Dispatch;
|
|
use log::Level;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
init_log();
|
|
HttpServer::new(|| App::new().service(root).service(manager::ws_connect))
|
|
.bind(("0.0.0.0", 2018))
|
|
.unwrap()
|
|
.run()
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
fn get_time() -> String {
|
|
let now = Local::now();
|
|
now.format("%Y-%m-%d %H:%M:%S").to_string()
|
|
}
|
|
|
|
fn init_log() {
|
|
let mut console_dispatch = Dispatch::new()
|
|
.format(|out, message, record| {
|
|
let (color, title) = match record.level() {
|
|
Level::Error => (Color::Red, "Error"),
|
|
Level::Warn => (Color::Yellow, "Warn"),
|
|
Level::Info => (Color::Green, "Info"),
|
|
Level::Debug => (Color::BrightWhite, "Debug"),
|
|
Level::Trace => (Color::White, "Trace"),
|
|
};
|
|
|
|
out.finish(format_args!(
|
|
"[{} {}]\t{}",
|
|
get_time(),
|
|
title.color(color),
|
|
message
|
|
))
|
|
})
|
|
.chain(std::io::stdout())
|
|
.level(log::LevelFilter::Debug);
|
|
console_dispatch.apply().expect("写入日志失败");
|
|
}
|
|
|
|
#[get("/")]
|
|
async fn root() -> impl Responder {
|
|
HttpResponse::Ok()
|
|
.content_type("text/html;charset=utf-8")
|
|
.body("<h1>侯浩彬是我儿子</h1>")
|
|
}
|