remove surrealdb

This commit is contained in:
c0repwn3r 2023-03-25 22:32:12 -04:00
commit f442689d03
Signed by: core
GPG Key ID: FDBF740DADDCEECF
11 changed files with 1644 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

11
.idea/hotel.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/hotel.iml" filepath="$PROJECT_DIR$/.idea/hotel.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

1391
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "hotel"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
serde = { version = "1.0.158", features = ["derive"] }
futures = "0.3.27"
once_cell = "1.17.1"
toml = "0.7.3"
log = "0.4.17"
simple_logger = "4.1.0"

31
src/config.rs Normal file
View File

@ -0,0 +1,31 @@
use std::fs;
use log::error;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
pub static CONFIG: Lazy<HotelConfig> = Lazy::new(|| {
let config_str = match fs::read_to_string("/etc/hotel.toml") {
Ok(str) => str,
Err(e) => {
error!("Unable to read config file: {}", e);
std::process::exit(1);
}
};
let config = match toml::from_str(&config_str) {
Ok(cfg) => cfg,
Err(e) => {
error!("Unable to parse config file: {}", e);
std::process::exit(1);
}
};
config
});
#[derive(Serialize, Debug, Deserialize)]
pub struct HotelConfig {
pub db_uri: String,
pub authorized_3fa_tokens: Vec<String>
}

112
src/error.rs Normal file
View File

@ -0,0 +1,112 @@
use actix_web::{error, HttpRequest};
use actix_web::error::{JsonPayloadError, PayloadError};
use actix_web::web::JsonConfig;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct APIErrorResponse {
pub errors: Vec<APIError>
}
#[derive(Serialize, Deserialize)]
pub struct APIError {
pub code: String,
pub message: String
}
impl From<&JsonPayloadError> for APIError {
fn from(value: &JsonPayloadError) -> Self {
match value {
JsonPayloadError::OverflowKnownLength { length, limit } => {
APIError {
code: "ERR_PAYLOAD_OVERFLOW_KNOWN_LENGTH".to_string(),
message: format!("Payload size is bigger than allowed & content length header set. (length: {}, limit: {})", length, limit),
}
},
JsonPayloadError::Overflow { limit } => {
APIError {
code: "ERR_PAYLOAD_OVERFLOW".to_string(),
message: format!("Payload size is bigger than allowed but no content-length header is set. (limit: {})", limit)
}
},
JsonPayloadError::ContentType => {
APIError {
code: "ERR_NOT_JSON".to_string(),
message: "Content-Type header not set to expected application/json".to_string()
}
},
JsonPayloadError::Deserialize(e) => {
APIError {
code: "ERR_JSON_DESERIALIZE".to_string(),
message: format!("Error deserializing JSON: {}", e)
}
},
JsonPayloadError::Serialize(e) => {
APIError {
code: "ERR_JSON_SERIALIZE".to_string(),
message: format!("Error serializing JSON: {}", e)
}
},
JsonPayloadError::Payload(e) => {
e.into()
},
_ => {
APIError {
code: "ERR_UNKNOWN_ERROR".to_string(),
message: "An unknown error has occured".to_string()
}
}
}
}
}
impl From<&PayloadError> for APIError {
fn from(value: &PayloadError) -> Self {
match value {
PayloadError::Incomplete(e) => {
APIError {
code: "ERR_UNEXPECTED_EOF".to_string(),
message: match e {
None => "Payload reached EOF but was incomplete".to_string(),
Some(e) => format!("Payload reached EOF but was incomplete: {}", e)
}
}
}
PayloadError::EncodingCorrupted => {
APIError {
code: "ERR_CORRUPTED_PAYLOAD".to_string(),
message: "Payload content encoding corrupted".to_string()
}
}
PayloadError::Overflow => {
APIError {
code: "ERR_PAYLOAD_OVERFLOW".to_string(),
message: "Payload reached size limit".to_string()
}
}
PayloadError::UnknownLength => {
APIError {
code: "ERR_PAYLOAD_UNKNOWN_LENGTH".to_string(),
message: "Unable to determine payload length".to_string()
}
}
PayloadError::Http2Payload(e) => {
APIError {
code: "ERR_HTTP2_ERROR".to_string(),
message: format!("HTTP/2 error: {}", e)
}
}
PayloadError::Io(e) => {
APIError {
code: "ERR_IO_ERROR".to_string(),
message: format!("I/O error: {}", e)
}
}
_ => {
APIError {
code: "ERR_UNKNOWN_ERROR".to_string(),
message: "An unknown error has occured".to_string()
}
}
}
}
}

55
src/main.rs Normal file
View File

@ -0,0 +1,55 @@
use actix_web::{App, HttpResponse, HttpServer, web, get, post, Responder, HttpRequest};
use actix_web::web::{Data, Json, JsonConfig};
use log::{error, Level};
use serde::{Serialize, Deserialize};
use crate::config::CONFIG;
use crate::error::{APIError, APIErrorResponse};
pub mod config;
pub mod error;
#[derive(Serialize, Deserialize)]
pub struct CodeRequest3FA {
token: String,
user: String
}
#[post("/v1/3fa_code")]
pub async fn get_3fa_code(req: Json<CodeRequest3FA>) -> HttpResponse {
if !CONFIG.authorized_3fa_tokens.contains(&req.token) {
return HttpResponse::Unauthorized().json(APIErrorResponse {
errors: vec![
APIError {
code: "ERR_INVALID_CODEREQ_TOKEN".to_string(),
message: "Invalid codereq token".to_string(),
}
],
})
}
HttpResponse::Ok().body("d")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
simple_logger::init_with_level(Level::Debug).unwrap();
HttpServer::new(move || {
App::new()
.app_data(JsonConfig::default().error_handler(|err, req| {
let err2: APIError = (&err).into();
actix_web::error::InternalError::from_response(
err,
HttpResponse::BadRequest().json(APIErrorResponse {
errors: vec![
err2
],
})
).into()
}))
.service(get_3fa_code)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}