You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.3 KiB
84 lines
2.3 KiB
2 years ago
|
use std::{any::Any, io};
|
||
|
|
||
|
use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware};
|
||
|
use actix_web::{
|
||
|
get,
|
||
|
http::{header::ContentType, Method},
|
||
|
middleware,
|
||
|
web::{self},
|
||
|
App, HttpRequest, HttpResponse, HttpServer,
|
||
|
};
|
||
|
|
||
|
// Define our model module and use our models
|
||
|
mod model;
|
||
|
use model::user::User;
|
||
|
use model::todo::{Todo, Priority, Status};
|
||
|
mod repo;
|
||
|
|
||
|
async fn index(method: Method) -> HttpResponse {
|
||
|
match method {
|
||
|
Method::GET => HttpResponse::Ok()
|
||
|
.content_type(ContentType::plaintext())
|
||
|
.body(format!("Welcome")),
|
||
|
_ => HttpResponse::Forbidden().finish(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[get("/counter")]
|
||
|
async fn get_counter(req: HttpRequest, session: Session) -> actix_web::Result<HttpResponse> {
|
||
|
// log::info!("Request: {req:?}");
|
||
|
|
||
|
let mut counter = 1;
|
||
|
if let Some(count) = session.get::<i32>("counter")? {
|
||
|
log::info!("Session counter: {count}");
|
||
|
counter = count + 1;
|
||
|
}
|
||
|
|
||
|
session.insert("counter", counter)?;
|
||
|
|
||
|
Ok(HttpResponse::Ok().finish())
|
||
|
}
|
||
|
|
||
|
#[actix_web::main]
|
||
|
async fn main() -> io::Result<()> {
|
||
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||
|
|
||
|
let user = User::new(
|
||
|
"phga".to_string(),
|
||
|
"onetuhoneuth".to_string(),
|
||
|
"salt".to_string(),
|
||
|
);
|
||
|
log::info!("{user:#?}");
|
||
|
let todo = Todo::new(
|
||
|
user.id().clone(),
|
||
|
"Mein todo".to_string(),
|
||
|
"Es hat viele Aufgaben".to_string(),
|
||
|
Priority::Normal,
|
||
|
Status::Todo,
|
||
|
);
|
||
|
log::info!("{todo:#?}");
|
||
|
// Create a better session key for production
|
||
|
// This one is only 0's -> 64 byte "random" string
|
||
|
let key: &[u8] = &[0; 64];
|
||
|
let key = actix_web::cookie::Key::from(key);
|
||
|
|
||
|
log::info!("Starting HTTP server: http://127.0.0.1:6969");
|
||
|
|
||
|
HttpServer::new(move || {
|
||
|
App::new()
|
||
|
.wrap(middleware::Compress::default())
|
||
|
.wrap(
|
||
|
SessionMiddleware::builder(CookieSessionStore::default(), key.clone())
|
||
|
.cookie_secure(false)
|
||
|
.build(),
|
||
|
)
|
||
|
.wrap(middleware::Logger::default())
|
||
|
.service(get_counter)
|
||
|
.default_service(web::to(index))
|
||
|
})
|
||
|
.bind(("127.0.0.1", 6969))?
|
||
|
.workers(2) // number of workers per bind default ist #cpus
|
||
|
.run()
|
||
|
.await
|
||
|
}
|