Replace node server with rocket/rust

This commit is contained in:
Sage Vaillancourt 2024-06-24 02:56:11 -04:00
parent 2dd46af5b3
commit 07a6af683d
11 changed files with 1825 additions and 1196 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
*.zip
.DS_Store
node_modules
server/target

5
server/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

8
server/.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/server.iml" filepath="$PROJECT_DIR$/.idea/server.iml" />
</modules>
</component>
</project>

11
server/.idea/server.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_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
server/.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="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

1700
server/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
server/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "pail-server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.38"
rocket = { version = "0.5.1", features = ["json"] }
regex = "1.10.5"
serde = { version = "1.0", features = ["rc", "derive"] }

View File

@ -1,54 +0,0 @@
const express = require('express')
const crypto = require('crypto')
const fs = require('fs/promises')
const app = express()
const buildUpdates = async () => {
const files = await(fs.readdir('./public'))
const r = /[^0-9]+([0-9.]+)\..*/
return files.map(file => ({
version: file.match(r)[1],
'update_link': `https://bb-addon.sagev.space/${file}`
}))
}
/**
* {
* addons: {
* [stringId1]: {
* updates: [
* {
* version: '1.0.9.xpi',
* update_link: `https://bb-addon.sagev.space/${file}`
* },
* ...
* ]
* },
* [stringId2]: ...
* }
* }
*/
const buildJson = async () => {
const uuid1 = '16f4f9af-ad27-41b7-adae-b0b08bc17b43'
const uuid2 = 'svaillancourt@add123.com'
const object = {
addons: {}
}
const updates = await buildUpdates()
object.addons[uuid1] = { updates }
object.addons[uuid2] = { updates }
return JSON.stringify(object, null, 2)
}
app.use(express.static('public'))
app.get('/updates.json', (request, response) => {
buildJson().then(json => response.send(json))
//response.send(JSON.stringify(buildJson()))
})
app.listen(5643, () => {
console.log('Listen on the port 5643...')
})

1127
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +0,0 @@
{
"name": "bb-addon-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}

82
server/src/main.rs Normal file
View File

@ -0,0 +1,82 @@
#[macro_use] extern crate rocket;
extern crate chrono;
use rocket::serde::{Serialize, json::Json};
use std::collections::HashMap;
use chrono::Local;
use std::fs::{OpenOptions, read_dir};
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
use rocket::form::Form;
use rocket::fs::FileServer;
use rocket::response::content::RawHtml;
use regex::Regex;
const UUID: &'static str = "16f4f9af-ad27-41b7-adae-b0b08bc17b43";
const EMAIL: &'static str = "svaillancourt@add123.com";
#[get("/")]
fn index() -> Json<AddonUpdateListing> {
let mut result = AddonUpdateListing::new();
let addon = Addon {
updates: read_dir("public").unwrap()
.map(|v| v.unwrap())
.map(|v| Listing::from_file_name(v.path().to_str().unwrap()))
.collect()
};
let addon = Rc::new(addon);
result.addons.insert(UUID, Rc::clone(&addon));
result.addons.insert(EMAIL, addon);
Json(result)
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct AddonUpdateListing {
addons: HashMap<&'static str, Rc<Addon>>
}
impl AddonUpdateListing {
pub fn new() -> Self {
Self {
addons: HashMap::new()
}
}
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct Addon {
updates: Vec<Listing>
}
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct Listing {
version: String,
update_link: String
}
impl Listing {
pub fn from_file_name(file_name: &str) -> Self {
let r = Regex::new(r#"public/([^0-9]+([0-9.]+)\..*)"#).unwrap();
let name_match = r.captures(file_name).unwrap();
let file_name = name_match.get(1).unwrap().as_str();
Self {
version: name_match.get(2).unwrap().as_str().to_string(),
update_link: format!("https://bb-addon.sagev.space/{file_name}")
}
}
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/updates.json", routes![index])
.mount("/", FileServer::from("public"))
}