* Simplifying backend api by... alot

This commit is contained in:
shockrah 2021-10-04 12:19:41 -07:00
parent 87b373bde8
commit 5b3f6efebf
10 changed files with 61 additions and 65 deletions

View File

@ -7,7 +7,6 @@ edition = "2018"
[dependencies]
rocket = "0.4"
dblib = { version = "0.1.0", path = "../dblib" }
serde = { version = "1.0", features = [ "derive" ] }
[dependencies.rocket_contrib]

17
api/src/main.rs Normal file
View File

@ -0,0 +1,17 @@
#![feature(decl_macro)]
#[macro_use] extern crate rocket;
use std::env;
mod video;
fn main() {
// emoji's crash my terminal
env::set_var("ROCKET_CLI_COLORS", "off");
/*
* Some of the target vars that we look for
* CLIP_DIR
*/
let _ = rocket::ignite()
.mount("/api", routes![video::list_categories])
.launch();
}

30
api/src/video.rs Normal file
View File

@ -0,0 +1,30 @@
use rocket_contrib::json;
use rocket_contrib::json::JsonValue;
use std::env;
use std::fs;
#[get("/categories")]
pub fn list_categories() -> JsonValue {
let dir = match env::var("CLIPS_DIR") {
Ok(val) => val,
Err(e) => "/media/clips/".to_string()
};
println!("Directory to search: {}", dir);
// Fucking hell this is going to be dumb
let mut cats: Vec<String> = Vec::new();
for file in fs::read_dir(dir).unwrap() {
match file {
Ok(file) => {
match file.file_name().into_string() {
Ok(s) => cats.push(s),
_ => {}
}
},
Err(_) => continue
}
}
json!({"categories": cats})
}

View File

Before

Width:  |  Height:  |  Size: 160 KiB

After

Width:  |  Height:  |  Size: 160 KiB

11
api/store.json Normal file
View File

@ -0,0 +1,11 @@
{
"keys":[
{
"name": "shockrah",
"key": "asdf"
}
],
"videos": {
"video": { "name": "cool" }
}
}

3
api/target/CACHEDIR.TAG Normal file
View File

@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/

View File

@ -1,36 +0,0 @@
use rocket_contrib::json::Json;
use rocket_contrib::json::JsonValue;
use dblib::{DB, request::ApiKey};
use dblib::KeyStore;
use serde::Serialize;
#[derive(Serialize)]
pub struct GenericResponse<'m> {
msg: Option<&'m str>,
err: bool
}
#[get("/by-id")]
pub fn get_vid_by_id(id: String) -> JsonValue {
todo!()
}
#[get("/by-name")]
pub fn get_vid_by_name(name: String) -> JsonValue {
todo!()
}
#[update("/description?<id>&<new>")]
pub fn update_description<'m>(key: ApiKey, id: String, desc: String) -> Json<GenericResponse<'m>> {
todo!()
}
#[delete("/video?<id>")]
pub fn delete_video(key: ApiKey, id: String) -> Json<GenericResponse<'m>> {
todo!()
}
#[update("/name?<id>&<dest>")]
pub fn update_vid_name<'m>(key: ApiKey, id: String, dest: String) -> Json<GenericResponse<'m>> {
todo!()
}

View File

@ -1,28 +0,0 @@
#![feature(decl_macro)]
#[macro_use] extern crate rocket;
use rocket_contrib::serve::StaticFiles;
use std::env;
mod api;
#[get("/<id>")]
fn video_page(id: u64) -> String {
format!("Video id requested: {}", id)
}
fn main() {
// emoji's crash my terminal
env::set_var("ROCKET_CLI_COLORS", "off");
let _ = rocket::ignite()
.mount("/clip", routes![video_page])
.mount("/video-api", routes![
api::get_vid_by_id,
api::get_vid_by_name,
api::update_description,
api::delete_video,
api::update_vid_name,
])
.mount("/", StaticFiles::from("static"))
.launch();
}