Skip to content

Commit

Permalink
Add second binary configuration for client / server model
Browse files Browse the repository at this point in the history
Now requires Rust Nightly. See rust-lang/rls#1011 (comment) for help with RLS using nightly while it's currently broken
  • Loading branch information
petschekr committed Jan 29, 2019
1 parent 7b7ee2b commit e649919
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"rust.wait_to_build": 500,
"rust-client.disableRustup": true
}
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ version = "0.1.0"
authors = ["Ryan Petschek <petschekr@gmail.com>"]
edition = "2018"

[[bin]]
name = "client"
path = "src/client/main.rs"

[[bin]]
name = "server"
path = "src/server/main.rs"

[dependencies]
pcsc = "2"
url = "1.7.2"
reqwest = "0.9.9"
regex = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.37"
rocket = "0.4.0"

[dependencies.rocket_contrib]
version = "0.4.0"
default-features = false
features = ["json", "serve", "handlebars_templates"]
10 changes: 10 additions & 0 deletions Rocket.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[global]
template_dir = "src/server/ui"

[development]
address = "localhost"
port = 3000

[production]
address = "0.0.0.0"
port = 3000 # Check this @EhsanDB
17 changes: 10 additions & 7 deletions src/api.rs → src/client/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl From<&'static str> for Error {

pub struct CheckinAPI {
client: reqwest::Client,
username: String,
auth_token: String,
}

Expand Down Expand Up @@ -72,14 +71,18 @@ impl CheckinAPI {
token.insert_str(0, "auth=");
Ok(Self {
client,
username: username.to_owned(),
auth_token: token,
})
},
None => Err("No auth token set by server".into())
}
}

pub fn from_token(auth_token: String) -> Self {
let client = reqwest::Client::new();
Self { client, auth_token }
}

fn checkin_action(&self, check_in: bool, uuid: &str, tag: &str) -> Result<String, Error> {
let action = if check_in { "check_in" } else { "check_out" };
let query = format!(
Expand Down Expand Up @@ -121,15 +124,15 @@ impl CheckinAPI {
}

#[cfg(test)]
mod tests {
mod checkin_api_tests {
use super::CheckinAPI;

#[test]
fn login() {
let username = env!("USERNAME");
let password = env!("PASSWORD");
let username = std::env::var("USERNAME").unwrap();
let password = std::env::var("PASSWORD").unwrap();

let instance = CheckinAPI::login(username, password).unwrap();
assert_eq!(instance.username, username);
let instance = CheckinAPI::login(&username, &password).unwrap();
assert!(instance.auth_token.len() == 64);
}
}
File renamed without changes.
11 changes: 8 additions & 3 deletions src/main.rs → src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod api;
use api::CheckinAPI;

fn main() {
let api = CheckinAPI::login("ryan", "test").unwrap();

let ctx = Context::establish(Scope::User).expect("Failed to establish context");

let mut readers_buf = [0; 2048];
Expand Down Expand Up @@ -50,7 +52,7 @@ fn main() {
// Debounce repeated events
if rs.event_state().intersects(State::PRESENT) {
if !readers.get(&name).unwrap_or(&false) {
card_tapped(&ctx, rs.name());
card_tapped(&ctx, rs.name(), &api);
}
readers.insert(name, true);
}
Expand All @@ -61,7 +63,7 @@ fn main() {
}
}

fn card_tapped(ctx: &Context, reader: &std::ffi::CStr) {
fn card_tapped(ctx: &Context, reader: &std::ffi::CStr, api: &CheckinAPI) {
// Connect to the card.
let card = match ctx.connect(reader, ShareMode::Shared, Protocols::ANY) {
Ok(card) => card,
Expand All @@ -77,7 +79,10 @@ fn card_tapped(ctx: &Context, reader: &std::ffi::CStr) {

let badge = badge::NFCBadge::new(&card);
match badge.get_user_id() {
Ok(id) => println!("ID is {}", id),
Ok(id) => {
let name = api.check_in(&id, "123").unwrap();
println!("Checked in {}", name);
},
Err(err) => println!("Error getting user ID: {:?}", err),
}
}
File renamed without changes.
21 changes: 21 additions & 0 deletions src/server/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;

use std::collections::HashMap;

use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;

#[get("/")]
fn index() -> Template {
let context: HashMap<&str, &str> = HashMap::new();
Template::render("index", &context)
}

fn main() {
rocket::ignite()
.attach(Template::fairing())
.mount("/", routes![index])
.mount("/css", StaticFiles::from("/ui/css"))
.launch();
}
10 changes: 10 additions & 0 deletions src/server/ui/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>HackGT Check In Administration</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Hello, world!!</h1>
</body>
</html>

0 comments on commit e649919

Please sign in to comment.