forked from jmug/stoatchat
Initial Commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
.env
|
||||
2004
Cargo.lock
generated
Normal file
2004
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "revolt"
|
||||
version = "0.1.0"
|
||||
authors = ["Paul Makles <paulmakles@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
mongodb = "0.9.0"
|
||||
bson = "0.14.0"
|
||||
rocket = { version = "0.4.2", default-features = false }
|
||||
once_cell = "1.3.1"
|
||||
dotenv = "0.15.0"
|
||||
3
Rocket.toml
Normal file
3
Rocket.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
[development]
|
||||
address = "192.168.0.10"
|
||||
port = 5500
|
||||
17
src/database.rs
Normal file
17
src/database.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use mongodb::Client;
|
||||
use std::env;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
static DBCONN: OnceCell<Client> = OnceCell::new();
|
||||
|
||||
pub fn connect() {
|
||||
let client = Client::with_uri_str(
|
||||
&env::var("DB_URI").expect("DB_URI not in environment variables!"))
|
||||
.expect("Failed to init db connection.");
|
||||
|
||||
DBCONN.set(client).unwrap();
|
||||
}
|
||||
|
||||
pub fn get_connection() -> &'static Client {
|
||||
DBCONN.get().unwrap()
|
||||
}
|
||||
14
src/main.rs
Normal file
14
src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
#[macro_use] extern crate rocket;
|
||||
|
||||
pub mod database;
|
||||
pub mod routes;
|
||||
|
||||
use dotenv;
|
||||
|
||||
fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
database::connect();
|
||||
|
||||
routes::mount(rocket::ignite()).launch();
|
||||
}
|
||||
22
src/routes/account.rs
Normal file
22
src/routes/account.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::database;
|
||||
use bson::{ bson, doc, ordered::OrderedDocument };
|
||||
|
||||
#[get("/")]
|
||||
pub fn root() -> String {
|
||||
let client = database::get_connection();
|
||||
let cursor = client.database("revolt").collection("users").find(None, None).unwrap();
|
||||
|
||||
let results: Vec<Result<OrderedDocument, mongodb::error::Error>> = cursor.collect();
|
||||
|
||||
format!("ok boomer, users: {}", results.len())
|
||||
}
|
||||
|
||||
#[get("/reg")]
|
||||
pub fn reg() -> String {
|
||||
let client = database::get_connection();
|
||||
let col = client.database("revolt").collection("users");
|
||||
|
||||
col.insert_one(doc! { "username": "test" }, None).unwrap();
|
||||
|
||||
format!("inserted")
|
||||
}
|
||||
8
src/routes/mod.rs
Normal file
8
src/routes/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use rocket::Rocket;
|
||||
|
||||
mod account;
|
||||
|
||||
pub fn mount(rocket: Rocket) -> Rocket {
|
||||
rocket
|
||||
.mount("/api/v1", routes![account::root, account::reg])
|
||||
}
|
||||
Reference in New Issue
Block a user