Basic router skeleton for freechat's new auth/dispatch model

This commit is contained in:
shockrah 2020-06-01 22:25:01 -07:00
parent c98d6dc503
commit b3289ca9bd

View File

@ -1,66 +1,60 @@
// This client code really just serves as the url router for the main website where we describe what the project is about
#![feature(proc_macro_hygiene, decl_macro, plugin)]
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate diesel;
extern crate chrono;
extern crate dotenv;
extern crate serde;
extern crate serde_json;
extern crate rand;
extern crate getrandom;
extern crate base64;
use rocket_contrib::templates::Template;
use dotenv::dotenv;
use std::net::SocketAddr;
use std::convert::Infallible; // our main dispatcher basically never fails hence why we use this
use std::env::var;
use std::collections::HashMap;
use tokio;
use hyper::{
self,
Server,
Response, Request, Body,
Method,
service::{make_service_fn, service_fn}
};
use dotenv::dotenv;
mod schema;
mod models;
mod invites;
mod payload;
mod users;
mod channels;
mod err;
mod auth;
mod utils;
use invites::*;
use channels::*;
use users::*;
#[database("freechat")]
pub struct DBConn(diesel::MysqlConnection);
pub fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/invite", routes![
generate_invite, use_invite
])
.mount("/channels", routes![
get_voice_channels, insert_new_channel, get_text_chanels
])
.mount("/users", routes![
remove_user, get_user_list
])
.mount("/auth", routes![
auth::leave, auth::login
])
.attach(Template::fairing())
.attach(DBConn::fairing())
fn map_qs(string: Option<&str>) -> HashMap<&str, Option<&str>> {
// if we can't find anything in the map then we no there is nothing
unimplemented!()
}
fn main() -> Result<(), i32> {
match dotenv() {
Ok(_d) => {
println!("Got .env: {:?}", var("DATABASE_URL").unwrap());
rocket().launch();
},
Err(e) => {
panic!("`.env` Could not be loaded err: {:?}", e);
}
async fn route_dispatcher(resp: &Response<Body>, meth: &Method, &path: &str, params: &HashMap<&str, Option<&str>>) {
// we do have to handle some general hyper errors
unimplemented!()
}
async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper::Error>{
let mut response = Response::new(Body::empty());
let method = request.method();
let path = request.uri().path();
let params = map_qs(request.uri().query());
// go through our auth wall first
if auth::wall_entry(&params).await {
route_dispatcher(&response, &method, path, &params);
}
Ok(())
else {
auth::wall_failure(&response);
}
Ok(response)
}
#[tokio::main]
async fn main() {
dotenv().ok();
println!("Servering on localhost:8888");
let addr = SocketAddr::from(([127,0,0,1], 8888));
let service = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(main_responder))
});
let server = Server::bind(&addr).serve(service);
}