simplify code
This commit is contained in:
parent
fab52f7f80
commit
c962a53bbe
33
src/main.rs
33
src/main.rs
@ -1,6 +1,7 @@
|
|||||||
use std::fs::File;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs::{read_to_string, File};
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
@ -46,14 +47,11 @@ fn init_status() -> Status {
|
|||||||
|
|
||||||
// check given secret
|
// check given secret
|
||||||
// https://www.youtube.com/watch?v=aHKWVLH-ibY
|
// https://www.youtube.com/watch?v=aHKWVLH-ibY
|
||||||
fn check_secret(given_secret: String, given_key: String) -> bool {
|
fn auth(p: &TheDoors) -> bool {
|
||||||
let consumer_secret = env::var("consumer_secret").unwrap();
|
let consumer_secret = env::var("consumer_secret").unwrap();
|
||||||
let consumer_key = env::var("consumer_key").unwrap();
|
let consumer_key = env::var("consumer_key").unwrap();
|
||||||
|
|
||||||
if given_secret == consumer_secret && given_key == consumer_key {
|
p.consumer_secret == consumer_secret && p.consumer_key == consumer_key
|
||||||
return true;
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write status to file and return JSON string.
|
// Write status to file and return JSON string.
|
||||||
@ -68,10 +66,10 @@ fn write_status(s: Status) -> String {
|
|||||||
// It may cease to or not yet exist. Then create an initial status and persist.
|
// It may cease to or not yet exist. Then create an initial status and persist.
|
||||||
async fn root() -> String {
|
async fn root() -> String {
|
||||||
if std::path::Path::new(STATUS_FILE).exists() {
|
if std::path::Path::new(STATUS_FILE).exists() {
|
||||||
return std::fs::read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT"));
|
read_to_string(STATUS_FILE).unwrap_or(String::from("KAPOTT"))
|
||||||
|
} else {
|
||||||
|
write_status(init_status())
|
||||||
}
|
}
|
||||||
let s = init_status();
|
|
||||||
write_status(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input type for the API: Both fields are optional.
|
// Input type for the API: Both fields are optional.
|
||||||
@ -80,21 +78,18 @@ struct TheDoors {
|
|||||||
aerie: Option<bool>,
|
aerie: Option<bool>,
|
||||||
cellar: Option<bool>,
|
cellar: Option<bool>,
|
||||||
consumer_key: String,
|
consumer_key: String,
|
||||||
consumer_secret: String
|
consumer_secret: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// The door can see through your soul.
|
// The door can see through your soul.
|
||||||
// https://www.youtube.com/watch?v=bDQDp00oTP4
|
// https://www.youtube.com/watch?v=bDQDp00oTP4
|
||||||
async fn the_doors(Json(payload): Json<TheDoors>) -> StatusCode {
|
async fn the_doors(Json(payload): Json<TheDoors>) -> StatusCode {
|
||||||
|
if !auth(&payload) {
|
||||||
|
return StatusCode::FORBIDDEN;
|
||||||
|
}
|
||||||
|
|
||||||
let check_secret = check_secret(payload.consumer_secret, payload.consumer_key);
|
let status: Status = if Path::new(STATUS_FILE).exists() {
|
||||||
|
let contents = read_to_string(STATUS_FILE).expect("FCKAFD");
|
||||||
if !check_secret { return StatusCode::FORBIDDEN; }
|
|
||||||
|
|
||||||
|
|
||||||
let status: Status = if std::path::Path::new(STATUS_FILE).exists() {
|
|
||||||
let contents = std::fs::read_to_string(STATUS_FILE).expect("FCKAFD");
|
|
||||||
serde_json::from_str(&contents).unwrap_or_else(|_| init_status())
|
serde_json::from_str(&contents).unwrap_or_else(|_| init_status())
|
||||||
} else {
|
} else {
|
||||||
init_status()
|
init_status()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user