work
This commit is contained in:
parent
f76cbd9d46
commit
c5fcafe450
11 changed files with 88053 additions and 97 deletions
11
src/db.rs
Normal file
11
src/db.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
|
||||
#[derive(sqlx::FromRow, Debug)]
|
||||
pub struct Entry {
|
||||
id: i32,
|
||||
macaddr: String,
|
||||
nickname: String,
|
||||
descr: String,
|
||||
privacy: i8,
|
||||
created: DateTime<Utc>,
|
||||
}
|
49
src/main.rs
49
src/main.rs
|
@ -1,23 +1,48 @@
|
|||
use argh::FromArgs;
|
||||
use sqlx::MySqlPool;
|
||||
use std::io;
|
||||
mod db;
|
||||
mod routes;
|
||||
mod templates;
|
||||
|
||||
/// Configuration
|
||||
#[derive(FromArgs, Debug)]
|
||||
struct Config {
|
||||
/// listen address
|
||||
#[argh(option, default = "[::1]:8080")]
|
||||
#[argh(option, default = "\"[::1]:8080\".to_string()")]
|
||||
listen: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let config: Config = argh::from_env();
|
||||
println!("{:?}, config")
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pool: MySqlPool,
|
||||
}
|
||||
|
||||
//#[async_std::main]
|
||||
//async fn main() -> Result<(), std::io::Error> {
|
||||
// tide::log::start();
|
||||
// let config: Config = argh::from_env();
|
||||
// let mut app = tide::new();
|
||||
//
|
||||
// app.listen(config.listen).await
|
||||
//}
|
||||
pub type Request = tide::Request<State>;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> Result<(), io::Error> {
|
||||
let config: Config = argh::from_env();
|
||||
|
||||
let dsn = "mysql://administration:foosinn123@127.0.0.1/administration";
|
||||
let pool = MySqlPool::connect(dsn)
|
||||
.await
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
||||
let mut conn = pool
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
||||
|
||||
let mut app = tide::with_state(State { pool });
|
||||
app.at("/").get(routes::index);
|
||||
app.at("/healthz").get(routes::healthz);
|
||||
app.at("/static").serve_dir("static/")?;
|
||||
|
||||
let entries: Vec<db::Entry> = sqlx::query_as("select * from mac_to_nick")
|
||||
.fetch_all(&mut conn)
|
||||
.await
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
||||
|
||||
entries.into_iter().for_each(|e| println!("{:?}", e));
|
||||
app.listen(config.listen).await
|
||||
}
|
||||
|
|
9
src/routes/mod.rs
Normal file
9
src/routes/mod.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
use crate::templates;
|
||||
|
||||
pub async fn healthz(_request: crate::Request) -> tide::Result {
|
||||
Ok("ok".into())
|
||||
}
|
||||
|
||||
pub async fn index(request: crate::Request) -> tide::Result {
|
||||
Ok(templates::IndexTemplate::new().into())
|
||||
}
|
11
src/templates.rs
Normal file
11
src/templates.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct IndexTemplate {}
|
||||
|
||||
impl IndexTemplate {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue