First commit

This commit is contained in:
Greg Burri 2019-08-11 22:05:35 +02:00
commit b830d3cf07
12 changed files with 2068 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/target
**/*.rs.bk
key.secret
WebSharper/key

1876
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

14
Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[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"

3
README.md Normal file
View file

@ -0,0 +1,3 @@
== Autoreload
https://actix.rs/docs/autoreload/

1
TODO.md Normal file
View file

@ -0,0 +1 @@
* Enable Logging.

3
conf.ron Normal file
View file

@ -0,0 +1,3 @@
(
port: 8082,
)

29
deploy.ps1 Normal file
View file

@ -0,0 +1,29 @@
if ($args.Count -lt 1) {
$scriptName = [Environment]::GetCommandLineArgs()[1]
echo "Usage: $scriptName <destination>"
exit 1
}
systemctl --user stop www-recipes.service
git pull
cargo build --release
$destination=$args[0]
if (!(Test-Path -Path $destination)) {
New-Item -ItemType directory -Path $destination
}
Copy-Item target/release/rup -Destination $destination
Copy-Item key.secret -Destination $destination
Copy-Item static -Destination $destination -Recurse -Force
# Do not overwrite the configuration.
if (!(Test-Path -Path $destination/conf.ron)) {
Copy-Item conf.ron -Destination $destination
}
systemctl --user start www-recipes.service

2
launch_debug.ps1 Normal file
View file

@ -0,0 +1,2 @@
# To launch RUP and watching source. See https://actix.rs/docs/autoreload/.
systemfd --no-pid -s http::8082 -- cargo watch -x run

1
src/consts.rs Normal file
View file

@ -0,0 +1 @@
pub static FILE_CONF: &str = "conf.ron";

100
src/main.rs Normal file
View 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
static/style.css Normal file
View 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;
}

16
templates/main.html Normal file
View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>rouleunpet</title>
<link href="static/lobster-two-font.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="static/style.css" />
</head>
<body>
<div class="container">
<h1>{{ test }}</h1>
</div>
</body>
</html>