Compare commits

...

4 Commits

5 changed files with 22 additions and 5 deletions

View File

@@ -10,5 +10,6 @@ serde_json = "1.0.140"
sqlx = { version = "0.8.3", features = ["sqlite", "sqlx-macros", "runtime-tokio"] }
jsonwebtoken = { version = "9.3.1", features = ["use_pem"] }
uuid = { version = "1.15.1", features = ["v4"] }
argon2 = "0.6.0-pre.1"
rand = "0.9.0"
argon2 = { version="0.6.0-pre.1", features = ["std"] }
rand = "0.9.0"
actix-cors = "0.7.0"

View File

@@ -1,4 +1,4 @@
use actix_web::{get, web, HttpResponse, Responder};
use actix_web::{get, web, HttpResponse, Responder, options};
use actix_web::web::Data;
use serde_json::to_string;
use sqlx::query_as;
@@ -16,7 +16,7 @@ async fn get_item(query: web::Query<DataUuid>, app_state: Data<AppState>) -> imp
if json.is_err() {
return HttpResponse::InternalServerError().finish();
}
HttpResponse::Ok().body(json.unwrap())
HttpResponse::Ok().append_header(("Access-Control-Allow-Origin", "*")).body(json.unwrap())
}
#[get("/items")]

View File

@@ -2,10 +2,12 @@ mod users;
mod cases;
mod items;
mod types;
mod utils;
use users::*;
use cases::*;
use items::*;
use utils::*;
use actix_web::web::Data;
use actix_web::{App, HttpServer};
@@ -17,6 +19,7 @@ struct AppState {
token_expiration: u64,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let pool = SqlitePool::connect("sqlite:database.db")
@@ -37,6 +40,7 @@ async fn main() -> std::io::Result<()> {
.service(get_items)
.service(get_case_items)
.service(get_item_cases)
.service(options)
.app_data(app_state.clone())
})
.bind(("127.0.0.1", 8000))?

View File

@@ -6,7 +6,7 @@ use actix_web::web::{Data, Json};
use actix_web::{HttpRequest, HttpResponse, Responder, post};
use argon2::Argon2;
use argon2::password_hash::{
PasswordHash, PasswordHasher, PasswordVerifier, SaltString, rand_core::OsRng,
PasswordHash, PasswordHasher, PasswordVerifier, SaltString, rand_core::OsRng
};
use jsonwebtoken::{
Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, encode, get_current_timestamp,

12
src/utils.rs Normal file
View File

@@ -0,0 +1,12 @@
use actix_web::{options, HttpResponse, Responder};
// This is needed for the web client.
// This returns the same options for every path of the api
#[options("/{_:.*}")]
async fn options() -> impl Responder {
HttpResponse::Ok()
.append_header(("Access-Control-Allow-Origin", "*"))
.append_header(("Access-Control-Allow-Methods", "GET, OPTIONS"))
.append_header(("Access-Control-Allow-Headers", "Content-Type"))
.finish()
}