diff --git a/server/src/main.rs b/server/src/main.rs index c2828a7..ef880c4 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -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, 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) -> Result, 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(¶ms).await { + route_dispatcher(&response, &method, path, ¶ms); } - 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); }