token-from-header

This commit is contained in:
Untone 2023-10-11 23:52:33 +03:00
parent ece504bbd1
commit e32e4aade5

View File

@ -1,4 +1,4 @@
use actix_web::{web, App, HttpResponse, HttpServer, web::Bytes};
use actix_web::{HttpRequest, web, App, HttpResponse, HttpServer, web::Bytes};
use actix_web::middleware::Logger;
use redis::{Client, AsyncCommands};
use std::collections::HashMap;
@ -18,9 +18,13 @@ struct AppState {
}
async fn connect_handler(
token: web::Path<String>,
req: HttpRequest,
state: web::Data<AppState>,
) -> Result<HttpResponse, actix_web::Error> {
let token = match req.headers().get("Authorization") {
Some(val) => val.to_str().unwrap_or(""),
None => return Err(ErrorUnauthorized("Unauthorized")),
};
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
eprintln!("TOKEN check failed: {}", e);
ErrorUnauthorized("Unauthorized")
@ -86,9 +90,13 @@ async fn connect_handler(
async fn disconnect_handler(
token: web::Path<String>,
req: HttpRequest,
state: web::Data<AppState>,
) -> Result<HttpResponse, actix_web::Error> {
let token = match req.headers().get("Authorization") {
Some(val) => val.to_str().unwrap_or(""),
None => return Err(ErrorUnauthorized("Unauthorized")),
};
let listener_id = data::get_auth_id(&token).await.map_err(|e| {
eprintln!("TOKEN check failed: {}", e);
ErrorUnauthorized("Unauthorized")
@ -116,9 +124,8 @@ async fn main() -> std::io::Result<()> {
tasks: tasks.clone(),
redis: client.clone(),
};
println!("Redis client initialized");
println!("Starting...");
HttpServer::new(move || {
println!("Webserver initialized");
App::new()
.wrap(Logger::default())
.app_data(web::Data::new(state.clone()))