feat(revcord): inital revcord commit

This commit is contained in:
Zomatree
2022-06-02 17:02:14 +01:00
parent 252a116766
commit b0313b8acb
18 changed files with 706 additions and 8 deletions

View File

@@ -0,0 +1,66 @@
[package]
name = "revcord-api"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
# Utility
lru = "0.7.0"
url = "2.2.2"
log = "0.4.11"
dotenv = "0.15.0"
dashmap = "5.2.0"
linkify = "0.6.0"
once_cell = "1.4.1"
env_logger = "0.7.1"
lazy_static = "1.4.0"
ctrlc = { version = "3.0", features = ["termination"] }
# Lang. Utilities
regex = "1"
num_enum = "0.5.1"
impl_ops = "0.1.1"
bitfield = "0.13.2"
# ID / key generation
ulid = "0.4.1"
nanoid = "0.4.0"
# serde
serde_json = "1.0.57"
serde = { version = "1.0.115", features = ["derive"] }
validator = { version = "0.14", features = ["derive"] }
# async
futures = "0.3.8"
chrono = "0.4.15"
async-channel = "1.6.1"
reqwest = { version = "0.11.4", features = ["json"] }
async-std = { version = "1.8.0", features = ["tokio1", "tokio02", "attributes"] }
# internal util
lettre = "0.10.0-alpha.4"
rauth = { git = "https://github.com/insertish/rauth", rev = "001a9698c56cea79e69e4ae71d7bc2cb48aec1a6" }
# redis
redis = { version = "0.21.2", features = ["async-std-comp"] }
mobc = { version = "0.7.3" }
mobc-redis = { version = "0.7.0", default-features = false, features = ["async-std-comp"] }
# web
rocket = { version = "0.5.0-rc.1", default-features = false, features = ["json"] }
mongodb = { version = "1.2.2", features = ["async-std-runtime"], default-features = false }
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", rev = "5843861a88958c16bfaa0b40f0d8910772bcd2f6" }
# spec generation
schemars = "0.8.8"
# rocket_okapi = "0.8.0-rc.1"
rocket_okapi = { git = "https://github.com/insertish/okapi", rev = "dcf0df115596ee07a587a7a543cddf3d7944645b", features = [ "swagger" ] }
# quark
revolt-quark = { path = "../../quark" }
# discord
revcord-models = { path = "../models" }

View File

@@ -0,0 +1,74 @@
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_okapi;
extern crate ctrlc;
pub mod routes;
pub mod version;
use log::info;
use rauth::{
config::Config,
logic::Auth,
};
use revolt_quark::DatabaseInfo;
use rocket_cors::AllowedOrigins;
use std::str::FromStr;
#[async_std::main]
async fn main() {
let _guard = revolt_quark::setup_logging();
info!(
"Starting Revcord server [version {}].",
crate::version::VERSION
);
revolt_quark::variables::delta::preflight_checks();
#[cfg(debug_assertions)]
ctrlc::set_handler(move || {
// Force ungraceful exit to avoid hang.
std::process::exit(0);
})
.expect("Error setting Ctrl-C handler");
let cors = rocket_cors::CorsOptions {
allowed_origins: AllowedOrigins::All,
allowed_methods: [
"Get", "Put", "Post", "Delete", "Options", "Head", "Trace", "Connect", "Patch",
]
.iter()
.map(|s| FromStr::from_str(s).unwrap())
.collect(),
..Default::default()
}
.to_cors()
.expect("Failed to create CORS.");
let db = DatabaseInfo::Auto.connect().await.unwrap();
// This is entirely temporary code until rauth is migrated to quark.
// (and / or otherwise gets updated to MongoDB v2 driver)
let mongo_db = mongodb::Client::with_uri_str(
&std::env::var("MONGODB").unwrap_or_else(|_| "mongodb://localhost".to_string()),
)
.await
.expect("Failed to init db connection.");
// Launch background task workers.
async_std::task::spawn(revolt_quark::tasks::start_workers(db.clone()));
let auth = Auth::new(mongo_db.database("revolt"), Config::default());
let rocket = rocket::build();
routes::mount(rocket)
.mount("/", rocket_cors::catch_all_options_routes())
.manage(auth)
.manage(db)
.manage(cors.clone())
.attach(cors)
.launch()
.await
.unwrap();
}

View File

@@ -0,0 +1,15 @@
use revolt_quark::{perms, Database, Permission, Ref, Result};
use revcord_models::{QuarkConversion, channel::Channel, user::User};
use rocket::{serde::json::Json, State};
#[openapi(tag = "Channel Information")]
#[get("/<target>")]
pub async fn req(db: &State<Database>, user: User, target: Ref) -> Result<Json<Channel>> {
let channel = target.as_channel(db).await?;
perms(&user.to_quark().await)
.channel(&channel)
.throw_permission(db, Permission::ViewChannel)
.await?;
Ok(Json(Channel::from_quark(channel).await))
}

View File

@@ -0,0 +1,10 @@
use rocket::Route;
use rocket_okapi::okapi::openapi3::OpenApi;
mod fetch_channel;
pub fn routes() -> (Vec<Route>, OpenApi) {
openapi_get_routes_spec![
fetch_channel::req,
]
}

View File

@@ -0,0 +1,23 @@
pub use rocket::http::Status;
pub use rocket::response::Redirect;
use rocket::{Build, Rocket};
use rocket_okapi::{settings::OpenApiSettings, okapi::openapi3::OpenApi};
pub mod channels;
pub fn mount(mut rocket: Rocket<Build>) -> Rocket<Build> {
let settings = OpenApiSettings::default();
mount_endpoints_and_merged_docs! {
rocket, "/".to_owned(), settings,
"/" => (vec![], custom_openapi_spec()),
"/channels" => channels::routes(),
};
rocket
}
fn custom_openapi_spec() -> OpenApi {
OpenApi::default()
}

View File

@@ -0,0 +1 @@
pub const VERSION: &str = "0.0.1";