updates
This commit is contained in:
parent
cf185c833b
commit
c4d44c68a0
9 changed files with 146 additions and 87551 deletions
|
@ -48,9 +48,9 @@ LEFT OUTER JOIN
|
|||
alive_hosts al
|
||||
ON
|
||||
mtn.macaddr = al.macaddr
|
||||
AND al.erfda > NOW() - INTERVAL 24 DAY
|
||||
AND al.erfda > NOW() - INTERVAL 30 MINUTE
|
||||
WHERE
|
||||
nickname = ?
|
||||
nickname LIKE ?
|
||||
ORDER BY
|
||||
al.erfda DESC
|
||||
",
|
||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -5,13 +5,12 @@ use std::io;
|
|||
use tide::sessions::{MemoryStore, SessionMiddleware};
|
||||
mod db;
|
||||
mod forms;
|
||||
mod middleware;
|
||||
mod routes;
|
||||
mod session;
|
||||
mod templates;
|
||||
|
||||
pub use session::AppSession as Session;
|
||||
|
||||
pub const USER: &str = "foosinn";
|
||||
pub const USER: &str = "hansi";
|
||||
|
||||
/// Configuration
|
||||
#[derive(FromArgs, Debug)]
|
||||
|
@ -30,6 +29,10 @@ struct Config {
|
|||
/// session secret
|
||||
#[argh(option, default = "\"thisisnotasecretthisisnotasecret\".into()")]
|
||||
session_secret: String,
|
||||
|
||||
/// debug
|
||||
#[argh(switch)]
|
||||
log: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -44,6 +47,16 @@ pub enum Level {
|
|||
Error,
|
||||
}
|
||||
|
||||
impl Level {
|
||||
fn color(&self) -> &'static str {
|
||||
match self {
|
||||
Level::Info => "success",
|
||||
Level::Warn => "warning",
|
||||
Level::Error => "danger",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Message = (Level, String);
|
||||
|
||||
pub type Request = tide::Request<State>;
|
||||
|
@ -51,6 +64,9 @@ pub type Request = tide::Request<State>;
|
|||
#[async_std::main]
|
||||
async fn main() -> Result<(), io::Error> {
|
||||
let config: Config = argh::from_env();
|
||||
if config.log {
|
||||
tide::log::start();
|
||||
}
|
||||
|
||||
let pool = MySqlPool::connect(&config.dsn)
|
||||
.await
|
||||
|
@ -60,6 +76,7 @@ async fn main() -> Result<(), io::Error> {
|
|||
SessionMiddleware::new(MemoryStore::new(), config.session_secret.as_bytes());
|
||||
|
||||
let mut app = tide::with_state(State { pool });
|
||||
app.with(middleware::ErrorHandler::default());
|
||||
app.with(session_store);
|
||||
app.at("/").get(routes::index);
|
||||
app.at("/change").post(routes::change);
|
||||
|
|
23
src/middleware.rs
Normal file
23
src/middleware.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use crate::Request;
|
||||
use crate::State;
|
||||
use tide::log;
|
||||
use tide::utils::async_trait;
|
||||
use tide::{Middleware, Next, Redirect, Result};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ErrorHandler {}
|
||||
|
||||
#[async_trait]
|
||||
impl Middleware<State> for ErrorHandler {
|
||||
async fn handle(&self, request: Request, next: Next<'_, State>) -> Result {
|
||||
let mut resp = next.run(request).await;
|
||||
if let Some(err) = resp.take_error() {
|
||||
log::error!("middleware caught error", { error: err.to_string() });
|
||||
return Ok(Redirect::see_other("/").into());
|
||||
}
|
||||
Ok(resp)
|
||||
}
|
||||
fn name(&self) -> &str {
|
||||
"ErrorHandler"
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use crate::db;
|
||||
use crate::forms;
|
||||
use crate::templates;
|
||||
use crate::Session;
|
||||
use crate::forms::ChangeForm;
|
||||
use crate::session::Session;
|
||||
use crate::templates::IndexTemplate;
|
||||
use crate::USER;
|
||||
use tide::Redirect;
|
||||
|
||||
|
@ -19,11 +19,11 @@ pub async fn index(mut request: crate::Request) -> tide::Result {
|
|||
.await
|
||||
.map_err(|err| dbg!(err))?;
|
||||
let messages = Session::from(&mut request).pop_messages();
|
||||
Ok(templates::IndexTemplate::new(my, unassinged, messages).into())
|
||||
Ok(IndexTemplate::new(my, unassinged, messages).into())
|
||||
}
|
||||
|
||||
pub async fn change(mut request: crate::Request) -> tide::Result {
|
||||
let form: forms::ChangeForm = request.body_form().await?;
|
||||
let form: ChangeForm = request.body_form().await?;
|
||||
let message = form.handle(&request).await;
|
||||
Session::from(&mut request).add_message(message);
|
||||
Ok(Redirect::see_other("/").into())
|
||||
|
|
|
@ -2,12 +2,12 @@ use crate::Message;
|
|||
use crate::Request;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub struct AppSession<'r> {
|
||||
pub struct Session<'r> {
|
||||
request: &'r mut Request,
|
||||
session: Inner,
|
||||
}
|
||||
|
||||
impl<'r> AppSession<'r> {
|
||||
impl<'r> Session<'r> {
|
||||
pub fn add_message(&mut self, message: Message) {
|
||||
self.session.add_message(message);
|
||||
}
|
||||
|
@ -17,14 +17,14 @@ impl<'r> AppSession<'r> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'r> From<&'r mut Request> for AppSession<'r> {
|
||||
impl<'r> From<&'r mut Request> for Session<'r> {
|
||||
fn from(request: &'r mut Request) -> Self {
|
||||
let session = Inner::from(&*request);
|
||||
Self { request, session }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Drop for AppSession<'r> {
|
||||
impl<'r> Drop for Session<'r> {
|
||||
fn drop(&mut self) {
|
||||
self.session.commit(self.request);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue