Main video pages now slowly coming online from the backend

This commit is contained in:
shockrah 2025-08-15 16:20:56 -07:00
parent ac200c0c0d
commit 61b3a5fe80
8 changed files with 2614 additions and 0 deletions

1
service/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

2487
service/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

9
service/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "backend"
version = "0.1.0"
edition = "2021"
[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
serde = {version = "1.0", features = ["derive"]}
rocket_dyn_templates = {version = "0.2.0", features = ["tera"] }

33
service/src/db.rs Normal file
View File

@ -0,0 +1,33 @@
use std::collections::HashMap;
use std::fs;
use rocket::serde::json;
use serde::Deserialize;
use serde::Serialize;
use crate::CONFIG;
#[derive(Serialize, Deserialize)]
pub struct Row {
pub id: i64,
pub file: String,
pub thumbnail: String
}
pub fn get_video(id: i64) -> Option<Row> {
// Used to fetch video information from the db for client's to request
// video and thumbnail files from us
let db_file = fs::read_to_string(CONFIG.db_file)
.expect("Unable to read DB_FILE");
let db_data: HashMap<i64, Row> = json::from_str(&db_file)
.expect("Unable to parse DB_FILE content");
match db_data.get(&id) {
Some(video) => Some(Row {
id,
file: video.file.clone(),
thumbnail: video.thumbnail.clone()
}),
None => None
}
}

27
service/src/main.rs Normal file
View File

@ -0,0 +1,27 @@
#[macro_use] extern crate rocket;
use rocket_dyn_templates::Template;
use rocket::fs::FileServer;
mod video;
mod db;
pub struct BackendConfig {
pub root: &'static str,
pub db_file: &'static str,
}
const CONFIG: BackendConfig = BackendConfig {
root: "/opt/clippable",
db_file: "/opt/clippable/db.json"
};
#[rocket::main]
async fn main() {
let _ = rocket::build()
.mount("/static", FileServer::from("static"))
.mount("/c", routes![video::index])
.mount("/video", routes![video::file])
.attach(Template::fairing())
.launch().await;
}

33
service/src/video.rs Normal file
View File

@ -0,0 +1,33 @@
use crate::db;
use std::path::PathBuf;
use rocket::fs::NamedFile;
use rocket_dyn_templates::Template;
use rocket_dyn_templates::context;
#[get("/<video_id>")]
pub fn index(video_id: i64) -> Template {
// Read the db file we have to get the ID
// Now we can fetch the row from the DB content
match db::get_video(video_id) {
Some(video) => {
let path = PathBuf::from(&video.file);
Template::render("index", context! {
title: path.file_name(),
kind: path.extension(),
video: video.file
})
},
None => Template::render("index", context! {
title: "Not found"
})
}
}
#[get("/<path>")]
pub async fn file(path: PathBuf) -> Option<NamedFile> {
if path.is_file() {
return NamedFile::open(path).await.ok();
}
None
}

8
service/static/style.css Normal file
View File

@ -0,0 +1,8 @@
.content {
align-items: center;
justify-content: center;
margin: auto;
width: 75%;
text-align: center;
}

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<head lang="en-us">
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<div class="content">
<h1 class="title">{{ title }}</h1>
{% if video %}
<video controls>
<source src="/video/{{ video }}" type="{{ kind }}">
</video>
{% else %}
<p>Nothing to see here</p>
{% endif %}
</div>
</body>