Initial commit

This commit is contained in:
Thomas Forgione 2019-03-16 11:04:06 +01:00
commit 2a164d5e72
No known key found for this signature in database
GPG Key ID: BFD17A2D71B3B5E7
8 changed files with 151 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "pytron-web"
version = "0.1.0"
authors = ["Thomas Forgione <thomas@forgione.fr>"]
edition = "2018"
[dependencies]
rocket = "0.4.0"
rocket_contrib = { version = "0.4.0", features = ["tera_templates"] }
serde_derive = "1.0.89"
serde = "1.0.89"
serde_json = "1.0.39"

8
assets/test.json Normal file
View File

@ -0,0 +1,8 @@
{
"ias": ["ia1", "ia2", "ia3"],
"battles": {
"ia1/ia2": 0.3,
"ia1/ia3": 0.7,
"ia2/ia3": 0.5
}
}

44
src/lib.rs Normal file
View File

@ -0,0 +1,44 @@
#[macro_use]
extern crate serde_derive;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
#[derive(Serialize, Deserialize)]
/// The data contained from the json file generated by python.
pub struct Data {
/// The names of the IAs.
pub ias: Vec<String>,
/// The results of the battles.
pub battles: HashMap<String, f64>,
}
impl Data {
/// Creates some test data.
pub fn test() -> Data {
// Open the file
let mut file = File::open("./assets/test.json")
.expect("Couldn't open file");
let mut content = String::new();
file.read_to_string(&mut content)
.expect("Couldn't read data");
serde_json::from_str(&content)
.expect("Couldn't parse json")
}
/// Returns the result for a certain battle.
pub fn get(&self, ia1: &str, ia2: &str) -> Option<f64> {
if let Some(value) = self.battles.get(&format!("{}/{}", ia1, ia2)) {
Some(*value)
} else if let Some(value) = self.battles.get(&format!("{}/{}", ia2, ia1)) {
Some(1.0 - *value)
} else {
None
}
}
}

22
src/main.rs Normal file
View File

@ -0,0 +1,22 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use rocket_contrib::templates::Template;
use rocket_contrib::serve::StaticFiles;
use pytron_web::Data;
#[get("/")]
fn hello() -> Template {
Template::render("index", &Data::test())
}
fn main() {
rocket::ignite()
.mount("/", routes![hello])
.mount("/static", StaticFiles::from("static"))
.attach(Template::fairing())
.launch();
}

1
static/main.css Normal file

File diff suppressed because one or more lines are too long

49
templates/base.html.tera Normal file
View File

@ -0,0 +1,49 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="/static/main.css">
{% block extracss %}{% endblock extracss %}
<title>Pytron</title>
</head>
<body class="layout-documentation page-components">
<nav class="navbar is-link" role="navigation" aria-label="main navigation">
<div class="container">
<div class="navbar-brand">
<a class="navbar-item is-size-4" href="/">
<strong>Pytron</strong>
</a>
<a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-end">
<a class="navbar-item is-size-6" href="/button">
<strong>Button</strong>
</a>
</div>
</div>
</div>
</nav>
{% block content %}{% endblock content %}
<script>
document.addEventListener('DOMContentLoaded', () => {
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
if ($navbarBurgers.length > 0) {
$navbarBurgers.forEach( el => {
el.addEventListener('click', () => {
const target = el.dataset.target;
const $target = document.getElementById(target);
el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
});
}
});
</script>
</body>
</html>

11
templates/index.html.tera Normal file
View File

@ -0,0 +1,11 @@
{% extends "base" %}
{% block content %}
<section class="section">
<div class="container">
<h1 class="title is-1">Welcome to Pytron</h1>
{% for ia in ias %}
<h5>{{ ia }}</h5>
{% endfor %}
</div>
</section>
{% endblock %}