add ticker-serve prototype
This commit is contained in:
parent
db76004e82
commit
760d7b1d36
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,6 @@
|
||||||
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"ticker-update",
|
||||||
|
"ticker-serve",
|
||||||
|
"libticker",
|
||||||
|
]
|
File diff suppressed because it is too large
Load Diff
25
default.nix
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;
|
with pkgs;
|
||||||
|
|
||||||
let
|
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 {
|
ticker-update = rustPlatform.buildRustPackage {
|
||||||
name = "ticker-update";
|
name = "ticker-update";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
pkg-config openssl postgresql.lib
|
pkg-config openssl postgresql.lib
|
||||||
cargo rustc
|
rust
|
||||||
];
|
];
|
||||||
preBuild = "pushd ticker-update";
|
preBuild = "pushd ticker-update";
|
||||||
postBuild = "popd";
|
postBuild = "popd";
|
||||||
cargoSha256 = "0igr8k46yl5a89zjymp6914faawcvqza08h6l9pcplpikhbvls42";
|
cargoSha256 = "0igr8k46yl5a89zjymp6914faawcvqza08h6l9pcplpikhbvls42";
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
inherit ticker-update;
|
inherit
|
||||||
|
rustPlatform
|
||||||
|
ticker-update;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::fs::read_to_string;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
@ -12,3 +13,12 @@ pub struct Config {
|
||||||
pub db_url: String,
|
pub db_url: String,
|
||||||
pub calendars: BTreeMap<String, CalendarOptions>,
|
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
10
shell.nix
|
@ -2,12 +2,14 @@
|
||||||
|
|
||||||
with pkgs;
|
with pkgs;
|
||||||
|
|
||||||
|
let
|
||||||
|
default = import ./default.nix {};
|
||||||
|
in
|
||||||
|
|
||||||
stdenv.mkDerivation {
|
stdenv.mkDerivation {
|
||||||
name = "env";
|
name = "env";
|
||||||
buildInputs = [
|
buildInputs =
|
||||||
pkg-config openssl postgresql.lib
|
default.ticker-update.buildInputs;
|
||||||
cargo rustc
|
|
||||||
];
|
|
||||||
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
echo "Run 'cargo build --release'"
|
echo "Run 'cargo build --release'"
|
||||||
|
|
|
@ -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" }
|
|
@ -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();
|
||||||
|
}
|
|
@ -8,8 +8,6 @@ edition = "2018"
|
||||||
reqwest = "~0.9"
|
reqwest = "~0.9"
|
||||||
diesel = { version = "~1", features = ["postgres", "chrono"] }
|
diesel = { version = "~1", features = ["postgres", "chrono"] }
|
||||||
chrono = "~0.4"
|
chrono = "~0.4"
|
||||||
serde = { version = "~1.0", features = ["derive"] }
|
|
||||||
serde_yaml = "~0.8"
|
|
||||||
num_cpus = "~1"
|
num_cpus = "~1"
|
||||||
crossbeam = "~0.7"
|
crossbeam = "~0.7"
|
||||||
libticker = { path = "../libticker" }
|
libticker = { path = "../libticker" }
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::mem::replace;
|
use std::mem::replace;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::fs::read_to_string;
|
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use chrono::offset::Utc;
|
use chrono::offset::Utc;
|
||||||
|
@ -259,10 +258,7 @@ impl std::fmt::Display for Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let config_file = read_to_string("config.yaml")
|
let config = Config::read_yaml_file("config.yaml");
|
||||||
.expect("config.yaml");
|
|
||||||
let config: Config = serde_yaml::from_str(&config_file)
|
|
||||||
.expect("config");
|
|
||||||
let res = Resources::new(
|
let res = Resources::new(
|
||||||
config.db_url,
|
config.db_url,
|
||||||
config.calendars.into_iter()
|
config.calendars.into_iter()
|
||||||
|
|
Loading…
Reference in New Issue