add ticker-serve prototype

This commit is contained in:
Astro 2019-10-26 01:14:49 +02:00
parent db76004e82
commit 760d7b1d36
10 changed files with 15377 additions and 16 deletions

700
Cargo.lock generated

File diff suppressed because it is too large Load Diff

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[workspace]
members = [
"ticker-update",
"ticker-serve",
"libticker",
]

14540
channel-rust-nightly.toml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,38 @@
{ pkgs ? import <nixpkgs> {} }:
{ mozillaOverlay ? import <mozillaOverlay>,
rustManifest ? ./channel-rust-nightly.toml,
}:
let
pkgs = import <nixpkgs> { overlays = [ mozillaOverlay ]; };
in
with pkgs;
let
rustChannelOfTargets = _channel: _date:
(pkgs.lib.rustLib.fromManifestFile rustManifest {
inherit (pkgs) stdenv fetchurl patchelf;
}).rust;
rust =
rustChannelOfTargets "nightly" null;
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
rustc = rust;
cargo = rust;
});
ticker-update = rustPlatform.buildRustPackage {
name = "ticker-update";
src = ./.;
buildInputs = [
pkg-config openssl postgresql.lib
cargo rustc
rust
];
preBuild = "pushd ticker-update";
postBuild = "popd";
cargoSha256 = "0igr8k46yl5a89zjymp6914faawcvqza08h6l9pcplpikhbvls42";
};
in {
inherit ticker-update;
inherit
rustPlatform
ticker-update;
}

View File

@ -1,3 +1,4 @@
use std::fs::read_to_string;
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
@ -12,3 +13,12 @@ pub struct Config {
pub db_url: String,
pub calendars: BTreeMap<String, CalendarOptions>,
}
impl Config {
pub fn read_yaml_file(path: &str) -> Self {
let config_file = read_to_string(path)
.expect(path);
serde_yaml::from_str(&config_file)
.expect("config")
}
}

View File

@ -2,12 +2,14 @@
with pkgs;
let
default = import ./default.nix {};
in
stdenv.mkDerivation {
name = "env";
buildInputs = [
pkg-config openssl postgresql.lib
cargo rustc
];
buildInputs =
default.ticker-update.buildInputs;
shellHook = ''
echo "Run 'cargo build --release'"

11
ticker-serve/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "ticker-serve"
version = "0.1.0"
authors = ["Astro <astro@spaceboyz.net>"]
edition = "2018"
[dependencies]
rocket = "0.4"
typed-html = "0.2"
diesel = { version = "~1", features = ["postgres", "chrono"] }
libticker = { path = "../libticker" }

83
ticker-serve/src/main.rs Normal file
View File

@ -0,0 +1,83 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![recursion_limit="512"]
use std::fs::read_to_string;
use std::sync::Mutex;
#[macro_use] extern crate rocket;
use rocket::{State, response::content};
use typed_html::{html, text, dom::DOMTree};
use diesel::{Connection, pg::PgConnection, prelude::*};
use libticker::{
config::{Config, CalendarOptions},
schema::{self, events::dsl::events},
model::{Calendar, Event},
ics::{Object, Timestamp, GetValue},
};
fn fix_url(s: &str) -> std::borrow::Cow<str> {
if s.starts_with("http:") || s.starts_with("https:") {
s.into()
} else {
format!("http://{}", s).into()
}
}
#[get("/")]
fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
let db = db.lock().unwrap();
let es = events
.order_by(schema::events::dtstart.asc())
.then_order_by(schema::events::dtend.desc())
.load::<Event>(&*db)
.unwrap();
let doc: DOMTree<String> = html!(
<html>
<head>
<title>"Ticker"</title>
</head>
<body>
<h1>"Ticker"</h1>
{ es.iter().map(|e| html!(
<article class="event">
{ match &e.url {
None => html!(
<h2>{ text!("{}", &e.summary) }</h2>
),
Some(url) => html!(
<h2>
<a href={ fix_url(url) }>
{ text!("{}", &e.summary) }
</a>
</h2>
),
} }
<p class="dtstart">{ text!("{}", &e.dtstart) }</p>
{ e.location.as_ref().map(|location| html!(
<p>
{ text!("{}", location) }
</p>
)) }
</article>
)) }
</body>
</html>
);
content::Html(doc.to_string())
}
fn main() {
let config = Config::read_yaml_file("config.yaml");
let db = PgConnection::establish(&config.db_url)
.expect("DB");
rocket::ignite()
.manage(Mutex::new(db))
.mount("/", routes![
index,
])
.launch();
}

View File

@ -8,8 +8,6 @@ edition = "2018"
reqwest = "~0.9"
diesel = { version = "~1", features = ["postgres", "chrono"] }
chrono = "~0.4"
serde = { version = "~1.0", features = ["derive"] }
serde_yaml = "~0.8"
num_cpus = "~1"
crossbeam = "~0.7"
libticker = { path = "../libticker" }

View File

@ -1,6 +1,5 @@
use std::mem::replace;
use std::io::Read;
use std::fs::read_to_string;
use std::sync::RwLock;
use std::collections::{HashMap, VecDeque};
use chrono::offset::Utc;
@ -259,10 +258,7 @@ impl std::fmt::Display for Error {
}
fn main() {
let config_file = read_to_string("config.yaml")
.expect("config.yaml");
let config: Config = serde_yaml::from_str(&config_file)
.expect("config");
let config = Config::read_yaml_file("config.yaml");
let res = Resources::new(
config.db_url,
config.calendars.into_iter()