This commit is contained in:
Oliver Atkinson 2024-12-12 16:48:07 -07:00
commit 7235326826
11 changed files with 3270 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

45
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'middleman'",
"cargo": {
"args": [
"build",
"--bin=middleman",
"--package=middleman"
],
"filter": {
"name": "middleman",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'middleman'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=middleman",
"--package=middleman"
],
"filter": {
"name": "middleman",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

3064
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "middleman"
version = "0.1.0"
edition = "2021"
[dependencies]
rocket = {path = "../Rocket/core/lib"}
tokio = { version = "1.40.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = "2.5.2"
minio = {git="https://github.com/minio/minio-rs.git", rev = "c28f576"}

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Visable Rocket
Adding visability to rocket.rs
In the project you need to specifiy the url that Loki is at.
Then you need to add this project's url to your prometheus config.
```toml
scrape_configs:
- job_name: rocket
static_configs:
- targets: ['localhost:8000']
```

4
Rocket.toml Normal file
View File

@ -0,0 +1,4 @@
[default]
address = "0.0.0.0"
port = 4433
cli_colors = false

52
src/main.rs Normal file
View File

@ -0,0 +1,52 @@
use rocket::{fs::FileServer, get, routes, State};
use s3::S3;
use tracing::{info, Level};
use url::Url;
mod s3;
struct Config<'a> {
s3_url: &'a str,
s3_bucket: &'a str,
s3_access_key: &'a str,
s3_secret_key: &'a str,
}
#[rocket::main]
async fn main() {
tracing_subscriber::fmt()
.with_line_number(false)
.without_time()
.with_max_level(Level::INFO)
.init();
let config = Config {
s3_bucket: "v1.10",
s3_url: "http://localhost:9000",
s3_access_key: "8UO76z8wCs9DnpxSbQUY",
s3_secret_key: "xwKVMpf2jzgprsdo85Dvo74UmO84y0aRrAUorYY5",
};
let s3 = S3::connect(&config).await.expect("Failed to connect to minio, aborting.");
let _ = rocket::build()
.mount("/", FileServer::new("www/"))
.mount("/", routes![get_s3_content])
.manage(s3)
.launch()
.await;
info!("Bye");
}
#[get("/<path>")]
async fn get_s3_content(path: &str, db: &State<S3>) -> String {
info!(path);
// TODO this is just pseudo-code
let url = "en.wikipedia.org/wiki/CNBC";
if let Some(resp) = db.get(&url).await {
return resp
}
"Hello world.".to_owned()
}

59
src/s3.rs Normal file
View File

@ -0,0 +1,59 @@
use minio::s3::{
args::{BucketExistsArgs, MakeBucketArgs}, client::ClientBuilder, creds::StaticProvider, error::Error, http::BaseUrl, types::S3Api, Client
};
use tracing::{instrument, trace};
use url::Url;
use crate::Config;
pub struct S3 {
bucket_name: String,
client: Client,
}
impl S3 {
#[instrument(skip_all, name = "S3")]
pub async fn connect(config: &Config<'_>) -> Result<Self, Error> {
let base_url = config.s3_url.parse::<BaseUrl>().unwrap();
let static_provider =
StaticProvider::new(&config.s3_access_key, &config.s3_secret_key, None);
let client = ClientBuilder::new(base_url)
.provider(Some(Box::new(static_provider)))
.build()?;
trace!("Checking bucket...");
let exists = client
.bucket_exists(&BucketExistsArgs::new(&config.s3_bucket).unwrap())
.await?;
if !exists {
trace!("Creating bucket...");
client
.make_bucket(&MakeBucketArgs::new(&config.s3_bucket).unwrap())
.await?;
}
trace!("Connection successfull");
Ok(Self {
bucket_name: config.s3_bucket.to_owned(),
client: client,
})
}
pub async fn get(&self, filename: &str) -> Option<String> {
let data = self
.client
.get_object(&self.bucket_name, &filename)
.send()
.await
.unwrap();
if let Ok(segments )= data.content.to_segmented_bytes().await {
return Some(segments.to_bytes().iter().map(|c| *c as char).collect::<String>())
}
None
}
}

16
www/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
Hello world
<script src="main.js" async defer></script>
<div id="frame"></div>
</body>
</html>

0
www/main.js Normal file
View File

4
www/style.css Normal file
View File

@ -0,0 +1,4 @@
html {
background-color: black;
color: white;
}