Add a common crate
This commit is contained in:
parent
c77e8d09e7
commit
44826a7edd
11 changed files with 34 additions and 15 deletions
1912
backend/Cargo.lock
generated
Normal file
1912
backend/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
15
backend/Cargo.toml
Normal file
15
backend/Cargo.toml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "recipes"
|
||||
version = "1.0.0"
|
||||
authors = ["Grégory Burri <greg.burri@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
actix-files = "0.1"
|
||||
askama = "0.8" # Template system
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
listenfd = "0.3" # To watch file modifications and automatically launch a build process (only used in dev/debug).
|
||||
ron = "0.5" # Rust object notation, to load configuration files.
|
||||
itertools = "0.8"
|
||||
sqlite = "0.25"
|
||||
3
backend/conf.ron
Normal file
3
backend/conf.ron
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(
|
||||
port: 8082,
|
||||
)
|
||||
1
backend/src/consts.rs
Normal file
1
backend/src/consts.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub static FILE_CONF: &str = "conf.ron";
|
||||
100
backend/src/main.rs
Normal file
100
backend/src/main.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
extern crate actix_web;
|
||||
extern crate listenfd;
|
||||
extern crate askama;
|
||||
|
||||
use listenfd::ListenFd;
|
||||
use actix_files as fs;
|
||||
use actix_web::{web, middleware, App, HttpServer, HttpResponse, Result, web::Query};
|
||||
use askama::Template;
|
||||
|
||||
use std::io::prelude::*;
|
||||
use ron::de::from_reader;
|
||||
use serde::Deserialize;
|
||||
use std::{fs::File, env::args};
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
mod consts;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "main.html")]
|
||||
struct MainTemplate<'a> {
|
||||
test: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Request {
|
||||
m: Option<String>
|
||||
}
|
||||
|
||||
fn main_page(query: Query<Request>) -> Result<HttpResponse> {
|
||||
|
||||
let main_template = MainTemplate { test: &"test" };
|
||||
|
||||
let s = main_template.render().unwrap();
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Config {
|
||||
port: u16
|
||||
}
|
||||
|
||||
fn get_exe_name() -> String {
|
||||
let first_arg = std::env::args().nth(0).unwrap();
|
||||
let sep: &[_] = &['\\', '/'];
|
||||
first_arg[first_arg.rfind(sep).unwrap()+1..].to_string()
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
if process_args() { return Ok(()) }
|
||||
|
||||
println!("Starting RUP as web server...");
|
||||
|
||||
let config: Config = {
|
||||
let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
|
||||
match from_reader(f) {
|
||||
Ok(c) => c,
|
||||
Err(e) => panic!("Failed to load config: {}", e)
|
||||
}
|
||||
};
|
||||
|
||||
println!("Configuration: {:?}", config);
|
||||
|
||||
let mut listenfd = ListenFd::from_env();
|
||||
let mut server =
|
||||
HttpServer::new(
|
||||
|| {
|
||||
App::new()
|
||||
.wrap(middleware::Compress::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").to(main_page))
|
||||
.service(fs::Files::new("/static", "static").show_files_listing())
|
||||
}
|
||||
);
|
||||
|
||||
server =
|
||||
if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
|
||||
server.listen(l).unwrap()
|
||||
} else {
|
||||
server.bind(&format!("0.0.0.0:{}", config.port)).unwrap()
|
||||
};
|
||||
|
||||
server.run()
|
||||
}
|
||||
|
||||
fn process_args() -> bool {
|
||||
fn print_usage() {
|
||||
println!("Usage:");
|
||||
println!(" {} [--help]", get_exe_name());
|
||||
}
|
||||
|
||||
let args: Vec<String> = args().collect();
|
||||
|
||||
if args.iter().any(|arg| arg == "--help") {
|
||||
print_usage();
|
||||
return true
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
19
backend/static/style.css
Normal file
19
backend/static/style.css
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
* {
|
||||
margin: 50px;
|
||||
padding: 0px;
|
||||
}
|
||||
|
||||
body {
|
||||
font-size: 18px;
|
||||
text-shadow: 2px 2px 2px #DDD;
|
||||
text-align: center;
|
||||
line-height: 18px;
|
||||
color: #5b5b5b;
|
||||
background-color: #ededed;
|
||||
margin: 0px;
|
||||
}
|
||||
|
||||
img {
|
||||
border: 0px;
|
||||
}
|
||||
|
||||
15
backend/templates/main.html
Normal file
15
backend/templates/main.html
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Recettes de cuisine</title>
|
||||
<link rel="stylesheet" type="text/css" href="static/style.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>{{ test }}</h1>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue