Compare commits

..

No commits in common. "e7803b98e808293e696052ea2ba15b73c7560c2b" and "1d64c8bb7986b1237e4af6a54dc450df6fd8deba" have entirely different histories.

12 changed files with 24 additions and 144 deletions

2
.gitignore vendored
View File

@ -3,5 +3,3 @@
/venv
.cargo
/.idea
web/static/index.html
web/static/assets

22
Cargo.lock generated
View File

@ -2051,27 +2051,6 @@ dependencies = [
"yansi",
]
[[package]]
name = "prometheus"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c"
dependencies = [
"cfg-if",
"fnv",
"lazy_static",
"memchr",
"parking_lot",
"protobuf",
"thiserror",
]
[[package]]
name = "protobuf"
version = "2.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
[[package]]
name = "quote"
version = "1.0.33"
@ -2232,7 +2211,6 @@ dependencies = [
"lazy_static",
"log",
"oauth2",
"prometheus",
"rand",
"reqwest",
"rocket",

View File

@ -43,7 +43,6 @@ assets = [
["conf/default.env", "etc/reminder-rs/config.env", "600"],
["conf/Rocket.toml", "etc/reminder-rs/Rocket.toml", "600"],
["web/static/**/*", "lib/reminder-rs/static", "644"],
["reminder-dashboard/dist/static/**/*", "lib/reminder-rs/static", "644"],
["web/templates/**/*", "lib/reminder-rs/templates", "644"],
["healthcheck", "lib/reminder-rs/healthcheck", "755"],
["cron.d/reminder_health", "etc/cron.d/reminder_health", "644"],

View File

@ -19,4 +19,3 @@ lazy_static = "1.4.0"
rand = "0.8"
base64 = "0.13"
csv = "1.2"
prometheus = "0.13.3"

View File

@ -6,7 +6,6 @@ mod consts;
mod macros;
mod catchers;
mod guards;
mod metrics;
mod routes;
use std::{env, path::Path};
@ -26,10 +25,7 @@ use serenity::{
};
use sqlx::{MySql, Pool};
use crate::{
consts::{CNC_GUILD, DISCORD_OAUTH_AUTHORIZE, DISCORD_OAUTH_TOKEN, SUBSCRIPTION_ROLES},
metrics::{init_metrics, MetricProducer},
};
use crate::consts::{CNC_GUILD, DISCORD_OAUTH_AUTHORIZE, DISCORD_OAUTH_TOKEN, SUBSCRIPTION_ROLES};
type Database = MySql;
@ -68,10 +64,7 @@ pub async fn initialize(
let static_path =
if Path::new("web/static").exists() { "web/static" } else { "/lib/reminder-rs/static" };
init_metrics();
rocket::build()
.attach(MetricProducer)
.attach(Template::fairing())
.register(
"/",
@ -92,13 +85,12 @@ pub async fn initialize(
.mount(
"/",
routes![
routes::cookies,
routes::index,
routes::metrics::metrics,
routes::cookies,
routes::privacy,
routes::report::report_error,
routes::return_to_same_site,
routes::terms,
routes::return_to_same_site,
routes::report::report_error,
],
)
.mount(

View File

@ -1,43 +0,0 @@
use lazy_static::lazy_static;
use prometheus::{IntCounterVec, Opts, Registry};
use rocket::{
fairing::{Fairing, Info, Kind},
Data, Request, Response,
};
lazy_static! {
pub static ref REGISTRY: Registry = Registry::new();
static ref REQUEST_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("requests", "Requests"), &["method", "route"]).unwrap();
static ref RESPONSE_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("responses", "Responses"), &["status", "route"]).unwrap();
}
pub fn init_metrics() {
REGISTRY.register(Box::new(REQUEST_COUNTER.clone())).unwrap();
}
pub struct MetricProducer;
#[rocket::async_trait]
impl Fairing for MetricProducer {
fn info(&self) -> Info {
Info { name: "Metrics fairing", kind: Kind::Request }
}
async fn on_request(&self, req: &mut Request<'_>, _data: &mut Data<'_>) {
if let Some(route) = req.route() {
REQUEST_COUNTER
.with_label_values(&[req.method().as_str(), &route.uri.to_string()])
.inc();
}
}
async fn on_response<'r>(&self, req: &'r Request<'_>, resp: &mut Response<'r>) {
if let Some(route) = req.route() {
RESPONSE_COUNTER
.with_label_values(&[&resp.status().code.to_string(), &route.uri.to_string()])
.inc();
}
}
}

View File

@ -235,21 +235,6 @@ pub async fn edit_reminder(
]);
}
}
} else {
sqlx::query!(
"
UPDATE reminders
SET interval_seconds = NULL, interval_days = NULL, interval_months = NULL
WHERE uid = ?
",
reminder.uid
)
.execute(transaction.executor())
.await
.map_err(|e| {
warn!("Error updating reminder interval: {:?}", e);
json!({ "reminder": Option::<Reminder>::None, "errors": vec!["Unknown error"] })
})?;
}
if reminder.channel > 0 {

View File

@ -0,0 +1 @@

View File

@ -1,13 +1,9 @@
use std::path::Path;
use std::collections::HashMap;
use chrono::{naive::NaiveDateTime, Utc};
use rand::{rngs::OsRng, seq::IteratorRandom};
use rocket::{
fs::{relative, NamedFile},
http::CookieJar,
response::Redirect,
serde::json::json,
};
use rocket::{http::CookieJar, response::Redirect, serde::json::json};
use rocket_dyn_templates::Template;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serenity::{
client::Context,
@ -31,6 +27,7 @@ use crate::{
pub mod api;
pub mod export;
pub mod guild;
type Unset<T> = Option<T>;
@ -658,26 +655,22 @@ async fn create_database_channel(
}
#[get("/")]
pub async fn dashboard_home(cookies: &CookieJar<'_>) -> Result<NamedFile, Redirect> {
pub async fn dashboard_home(cookies: &CookieJar<'_>) -> Result<Template, Redirect> {
if cookies.get_private("userid").is_some() {
NamedFile::open(Path::new(relative!("static/index.html"))).await.map_err(|e| {
warn!("Couldn't render dashboard: {:?}", e);
Redirect::to("/login/discord")
})
let mut map = HashMap::new();
map.insert("version", env!("CARGO_PKG_VERSION"));
Ok(Template::render("dashboard", &map))
} else {
Err(Redirect::to("/login/discord"))
}
}
#[get("/<_..>")]
pub async fn dashboard(cookies: &CookieJar<'_>) -> Result<NamedFile, Redirect> {
pub async fn dashboard(cookies: &CookieJar<'_>) -> Result<Template, Redirect> {
if cookies.get_private("userid").is_some() {
NamedFile::open(Path::new(relative!("static/index.html"))).await.map_err(|e| {
warn!("Couldn't render dashboard: {:?}", e);
Redirect::to("/login/discord")
})
let mut map = HashMap::new();
map.insert("version", env!("CARGO_PKG_VERSION"));
Ok(Template::render("dashboard", &map))
} else {
Err(Redirect::to("/login/discord"))
}

View File

@ -1,18 +0,0 @@
use prometheus;
use crate::metrics::REGISTRY;
#[get("/metrics")]
pub async fn metrics() -> String {
let encoder = prometheus::TextEncoder::new();
let res_custom = encoder.encode_to_string(&REGISTRY.gather());
match res_custom {
Ok(s) => s,
Err(e) => {
warn!("Error encoding metrics: {:?}", e);
String::new()
}
}
}

View File

@ -1,7 +1,6 @@
pub mod admin;
pub mod dashboard;
pub mod login;
pub mod metrics;
pub mod report;
use std::collections::HashMap;

View File

@ -218,16 +218,17 @@ div.inset-content {
margin-right: 10%;
}
div.flash-container {
position: fixed;
width: 100%;
bottom: 0;
}
div.flash-message {
position: fixed;
width: calc(100% - 32px);
margin: 16px !important;
z-index: 99;
bottom: 0;
display: none;
}
div.flash-message.is-active {
display: block;
}
body {
@ -632,10 +633,6 @@ li.highlight {
display: flex;
}
.button-row-edit > button {
margin-right: 4px;
}
.button-row .button-row-reminder {
flex-grow: 0;
padding: 2px;