Browse Source

add ticker-serve prototype

master
Astro 2 years ago
parent
commit
760d7b1d36
  1. 700
      Cargo.lock
  2. 6
      Cargo.toml
  3. 14540
      channel-rust-nightly.toml
  4. 25
      default.nix
  5. 10
      libticker/src/config.rs
  6. 10
      shell.nix
  7. 11
      ticker-serve/Cargo.toml
  8. 83
      ticker-serve/src/main.rs
  9. 2
      ticker-update/Cargo.toml
  10. 6
      ticker-update/src/main.rs

700
Cargo.lock

File diff suppressed because it is too large

6
Cargo.toml

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

14540
channel-rust-nightly.toml

File diff suppressed because it is too large

25
default.nix

@ -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;
}

10
libticker/src/config.rs

@ -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")
}
}

10
shell.nix

@ -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

@ -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

@ -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();
}

2
ticker-update/Cargo.toml

@ -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" }

6
ticker-update/src/main.rs

@ -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()

Loading…
Cancel
Save