Merge branch 'work' into calculator

This commit is contained in:
Book-reader 2025-09-05 16:17:43 +12:00
commit 7a57c7bf1e
17 changed files with 407 additions and 14 deletions

5
.gitignore vendored
View file

@ -1,2 +1,7 @@
.idea
/target
# /nginx
database
database.bak
.env
proxies.txt

View file

@ -1,4 +1,4 @@
http://127.0.0.1:8001 {
http://127.0.0.1:1235 {
route /static/* {
uri strip_prefix /static
file_server {
@ -6,10 +6,10 @@ http://127.0.0.1:8001 {
}
}
reverse_proxy 127.0.0.1:5843
reverse_proxy http://auth-frontend:5843
}
http://127.0.0.1:8002 {
http://127.0.0.1:1234 {
route /static/* {
uri strip_prefix /static
file_server {
@ -17,5 +17,5 @@ http://127.0.0.1:8002 {
}
}
reverse_proxy 127.0.0.1:5842
reverse_proxy http://frontend:5842
}

19
Dockerfile.auth-frontend Normal file
View file

@ -0,0 +1,19 @@
FROM rust:1.89.0 AS builder
WORKDIR /usr/src/asklyphe/
COPY asklyphe-auth-frontend asklyphe-auth-frontend
COPY asklyphe-common asklyphe-common
COPY lyphedb lyphedb
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/asklyphe-auth-frontend/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path asklyphe-auth-frontend/
FROM debian:trixie-slim
RUN apt-get update && apt-get upgrade -y && apt-get install -y libssl3 && rm -rf /var/lib/apt-get/lists/*
COPY --from=builder /usr/local/cargo/bin/asklyphe-auth-frontend /usr/local/bin/
COPY --from=builder /usr/src/asklyphe/asklyphe-auth-frontend/static /data/static
VOLUME /data
CMD ["asklyphe-auth-frontend"]

24
Dockerfile.authservice Normal file
View file

@ -0,0 +1,24 @@
FROM rust:1.89.0 AS builder
WORKDIR /usr/src/asklyphe/
COPY authservice authservice
COPY asklyphe-common asklyphe-common
COPY lyphedb lyphedb
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/authservice/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path authservice/
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/authservice/migration/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path authservice/migration/
FROM debian:trixie-slim
RUN apt-get update && apt-get upgrade -y && apt-get install -y libssl3 && rm -rf /var/lib/apt-get/lists/*
COPY --from=builder /usr/local/cargo/bin/authservice /usr/local/bin/
COPY --from=builder /usr/local/cargo/bin/migration /usr/local/bin/
CMD ["authservice"]

17
Dockerfile.bingservice Normal file
View file

@ -0,0 +1,17 @@
FROM rust:1.89.0 AS builder
WORKDIR /usr/src/asklyphe/
COPY bingservice bingservice
COPY asklyphe-common asklyphe-common
COPY lyphedb lyphedb
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/bingservice/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path bingservice/
FROM debian:trixie-slim
RUN apt-get update && apt-get upgrade -y && apt-get install -y libssl3 && rm -rf /var/lib/apt-get/lists/*
COPY --from=builder /usr/local/cargo/bin/bingservice /usr/local/bin/
CMD ["bingservice"]

20
Dockerfile.frontend Normal file
View file

@ -0,0 +1,20 @@
FROM rust:1.89.0 AS builder
WORKDIR /usr/src/asklyphe/
COPY asklyphe-frontend asklyphe-frontend
COPY asklyphe-common asklyphe-common
COPY lyphedb lyphedb
COPY unit_converter unit_converter
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/asklyphe-frontend/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path asklyphe-frontend/
FROM debian:trixie-slim
RUN apt-get update && apt-get upgrade -y && apt-get install -y libssl3 && rm -rf /var/lib/apt-get/lists/*
COPY --from=builder /usr/local/cargo/bin/asklyphe-frontend /usr/local/bin/
COPY --from=builder /usr/src/asklyphe/asklyphe-frontend/static /data/static
VOLUME /data
CMD ["asklyphe-frontend"]

17
Dockerfile.googleservice Normal file
View file

@ -0,0 +1,17 @@
FROM rust:1.89.0 AS builder
WORKDIR /usr/src/asklyphe/
COPY googleservice googleservice
COPY asklyphe-common asklyphe-common
COPY lyphedb lyphedb
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/googleservice/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path googleservice/
FROM debian:trixie-slim
RUN apt-get update && apt-get upgrade -y && apt-get install -y libssl3 && rm -rf /var/lib/apt-get/lists/*
COPY --from=builder /usr/local/cargo/bin/googleservice /usr/local/bin/
CMD ["googleservice"]

17
Dockerfile.vorebot Normal file
View file

@ -0,0 +1,17 @@
FROM rust:1.89.0 AS builder
WORKDIR /usr/src/asklyphe/
COPY vorebot vorebot
COPY asklyphe-common asklyphe-common
COPY lyphedb lyphedb
RUN --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \
--mount=type=cache,target=/usr/src/asklyphe/vorebot/target \
--mount=type=cache,target=$CARGO_HOME/git/db,sharing=locked \
cargo install --debug --path vorebot/
FROM debian:trixie-slim
RUN apt-get update && apt-get upgrade -y && apt-get install -y libssl3 && rm -rf /var/lib/apt-get/lists/*
COPY --from=builder /usr/local/cargo/bin/vorebot /usr/local/bin/
CMD ["vorebot"]

View file

@ -17,7 +17,7 @@ mod login;
use std::{env, process};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::net::{SocketAddr, ToSocketAddrs};
use std::ops::Deref;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
@ -66,7 +66,7 @@ async fn main() {
env_logger::init();
let opts = Opts {
bind_addr: env::var("BIND_ADDR").unwrap_or("0.0.0.0:5843".to_string()).parse().expect("Badly formed BIND_ADDR (Needs to be SocketAddr)"),
nats_addr: env::var("NATS_ADDR").unwrap_or("127.0.0.1:4222".to_string()).parse().expect("Badly formed NATS_ADDR (Needs to be SocketAddr)"),
nats_addr: env::var("NATS_ADDR").unwrap_or("127.0.0.1:4222".to_string()).to_socket_addrs().expect("Badly formed NATS_ADDR (Needs to be SocketAddr)").nth(0).unwrap(),
nats_cert: env::var("NATS_CERT").expect("NATS_CERT needs to be set"),
nats_key: env::var("NATS_KEY").expect("NATS_KEY needs to be set"),
asklyphe_url: env::var("ASKLYPHE_URL").unwrap_or("https://asklyphe.com".to_string()),

View file

@ -0,0 +1,106 @@
use tracing::{debug, error};
use once_cell::sync::Lazy;
use std::collections::BTreeMap;
use url_encoded_data;
pub static BANG_PREFIX: &str = "!";
#[derive(Debug)]
struct Bang<'a> {
pub url: &'a str,
pub aliases: &'a [&'a str]
}
impl<'a> Bang<'_> {
fn new(url: &'a str, aliases: &'a [&'a str]) -> Bang<'a> {
Bang {url, aliases}
}
}
static BUILTIN_BANGS: Lazy<BTreeMap<&str, Bang>> = Lazy::new(|| {
let mut bangs = BTreeMap::new();
bangs.insert("Google", Bang::new("https://google.com/search?q={}", &["g", "google"] as &[&str]));
bangs.insert("DuckDuckGo", Bang::new("https://duckduckgo.com/?q={}", &["d", "ddg", "duckduckgo"] as &[&str]));
bangs.insert("Wikipedia", Bang::new("https://wikipedia.org/w/index.php?search={}", &["w", "wiki", "wikipedia"] as &[&str]));
bangs
});
#[derive(Debug, Clone)]
struct BangLoc<'b> {
pub url: &'b str,
pub start_idx: usize,
pub len: usize
}
impl<'b> BangLoc<'_> {
fn new(url: &'b str, start_idx: usize, len: usize) -> BangLoc<'b> {
BangLoc {url, start_idx, len}
}
}
pub fn redirect_bang(query: &String) -> Option<String> {
if !query.contains(BANG_PREFIX) {
return None;
}
let bangs = query.match_indices(BANG_PREFIX).filter(|(bang_start_idx, _)| {
if *bang_start_idx == 0 || query.chars().nth(*bang_start_idx - 1).unwrap().is_whitespace() {
true
} else {
false
}
}).map(|(bang_start_idx, _)| {
let rest = query.get(bang_start_idx + 1..query.len()).unwrap();
BUILTIN_BANGS.iter().map(|(_, bang)| {
let alias = bang.aliases.iter()
.filter(|alias| rest.starts_with(**alias))
.filter(
|alias| rest.chars()
.nth(alias.len())
.unwrap_or(' ')
.is_whitespace())
.max_by(|a, b| a.len().cmp(&b.len()))?;
Some(BangLoc::new(bang.url, bang_start_idx, alias.len()))
}).filter(|bang| bang.is_some()).map(|bang| bang.unwrap()).next()
}).filter(|bang| bang.is_some())
.map(|bang| bang.unwrap())
.collect::<Vec<_>>();
let bang = bangs.first()?;
let end_idx = {
let mut end_idx = bang.start_idx + 1 + bang.len;
if end_idx < query.len() {
end_idx += 1;
}
end_idx
};
let start_idx = if end_idx == query.len() && bang.start_idx > 0 {
bang.start_idx - 1
} else {
bang.start_idx
};
let query_split = query.split_once(query.get(start_idx..end_idx).unwrap()).unwrap();
let query_trimmed = format!("{}{}", query_split.0, query_split.1);
// A hack to get URL escaping without using a proper URL layout, hopefully has no other issues apart from prepending '=' to the string
let query_encoded = url_encoded_data::stringify(&[("", query_trimmed.as_str())]);
let query_encoded = query_encoded.get(1..query_encoded.len()).unwrap().to_owned();
let bang_url_split = bang.url.split_once("{}").unwrap();
let bang_url = format!(
"{}{}{}",
bang_url_split.0,
query_encoded,
bang_url_split.1
);
Some(bang_url)
}

View file

@ -14,12 +14,13 @@
pub mod searchbot;
pub mod wikipedia;
pub mod unit_converter;
pub mod bangs;
pub mod math;
pub mod routes;
use std::{env, process};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::net::{SocketAddr, ToSocketAddrs};
use std::ops::Deref;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
@ -85,7 +86,7 @@ async fn main() {
env_logger::init();
let opts = Opts {
bind_addr: env::var("BIND_ADDR").unwrap_or("0.0.0.0:5842".to_string()).parse().expect("Badly formed BIND_ADDR (Needs to be SocketAddr)"),
nats_addr: env::var("NATS_ADDR").unwrap_or("127.0.0.1:4222".to_string()).parse().expect("Badly formed NATS_ADDR (Needs to be SocketAddr)"),
nats_addr: env::var("NATS_ADDR").unwrap_or("127.0.0.1:4222".to_string()).to_socket_addrs().expect("Badly formed NATS_ADDR (Needs to be SocketAddr)").nth(0).unwrap(),
nats_cert: env::var("NATS_CERT").expect("NATS_CERT needs to be set"),
nats_key: env::var("NATS_KEY").expect("NATS_KEY needs to be set"),
auth_url: env::var("AUTH_URL").unwrap_or("https://auth.asklyphe.com".to_string()),

View file

@ -18,6 +18,7 @@ use crate::unit_converter;
use crate::unit_converter::UnitConversion;
use crate::wikipedia::WikipediaSummary;
use crate::{wikipedia, Opts, ALPHA, BUILT_ON, GIT_COMMIT, VERSION, YEAR};
use crate::bangs;
use crate::math;
use crate::math::Calculation;
use askama::Template;
@ -174,11 +175,15 @@ pub async fn search_js(
let unit_comp = unit_converter::convert_unit(&unit_query);
complications.unit_converter = unit_comp;
let bang_redirect = bangs::redirect_bang(&query);
if let Some(redirect) = bang_redirect {
return Redirect::to(&redirect).into_response();
}
let mut calc_query = query.clone().to_lowercase();
calc_query = calc_query.replace("calculate", "").replace("what is", "");
let math = math::calculate(&calc_query);
complications.math = math;
} else {
complications.disabled = true;
query = query.replace("-complications", "");
@ -291,6 +296,11 @@ pub async fn search_nojs(
unit_query = unit_query.replace("metre", "meter");
let unit_comp = unit_converter::convert_unit(&unit_query);
complications.unit_converter = unit_comp;
let bang_redirect = bangs::redirect_bang(&query);
if let Some(redirect) = bang_redirect {
return Redirect::to(&redirect).into_response();
}
} else {
complications.disabled = true;
query = query.replace("-complications", "");

View file

@ -458,7 +458,7 @@ pub async fn user_count(db: &DatabaseConnection) -> Result<usize, FetchUserError
pub async fn admin_count(db: &DatabaseConnection) -> Result<usize, FetchUserError> {
// dont fucking touch this, i don't know why it works but it does, it's actually evil
// note: doesn't work
Ok(user::Entity::find().filter(user::Column::Flags.into_expr().binary(BinOper::LShift, Expr::value(63 - 2)).lt(1 << (63 - 2)))
Ok(user::Entity::find().filter(user::Column::Flags.into_expr().binary(BinOper::BitAnd, UserFlag::Administrator as i64).binary(BinOper::NotEqual, 0))
.count(db).await.map_err(|e| {
error!("DATABASE ERROR WHILE ADMINCOUNT: {e}");
FetchUserError::DatabaseError

View file

@ -34,7 +34,7 @@ pub static PROXIES: Lazy<Vec<Proxy>> = Lazy::new(|| {
let contents = std::fs::read_to_string(proxy_file);
let mut proxies = vec![];
for line in contents.expect("FAILED TO READ FILE").lines() {
for line in contents.expect("FAILED TO READ FILE").lines().filter(|l| l.len() > 0) {
proxies.push(Proxy::from_str(line).expect("INVALID PROXY"));
}

View file

@ -30,6 +30,7 @@ impl FromStr for Proxy {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.split(':');
let protocol = parts.next().ok_or(ProxyError::InvalidProxyFormat)?;
let host = parts.next().ok_or(ProxyError::InvalidProxyFormat)?;
let port = parts.next().ok_or(ProxyError::InvalidProxyFormat)?;
let auth = if let Some(user) = parts.next() {
@ -39,7 +40,7 @@ impl FromStr for Proxy {
None
};
Ok(Proxy {
address: format!("{}:{}", host, port),
address: format!("{}://{}:{}", protocol, host, port),
credentials: auth,
})
}

129
docker-compose.yml Normal file
View file

@ -0,0 +1,129 @@
services:
auth-frontend:
restart: unless-stopped
networks:
- lyphenet
env_file: ".env"
depends_on:
- nats
- authservice
build:
dockerfile: Dockerfile.auth-frontend
volumes:
- auth_frontend_data:/data
image: asklyphe/auth-frontend
authservice:
restart: unless-stopped
networks:
- lyphenet
env_file: ".env"
depends_on:
- nats
build:
dockerfile: Dockerfile.authservice
image: asklyphe/authservice
frontend:
restart: unless-stopped
networks:
- lyphenet
env_file: ".env"
depends_on:
- nats
build:
dockerfile: Dockerfile.frontend
volumes:
- frontend_data:/data
image: asklyphe/frontend
# vorebot:
# restart: unless-stopped
# networks:
# - lyphenet
# - outer
# env_file: ".env"
# depends_on:
# - nats
# build:
# dockerfile: Dockerfile.vorebot
# image: asklyphe/vorebot
bingservice:
restart: unless-stopped
networks:
- lyphenet
- outer
env_file: ".env"
depends_on:
- nats
build:
dockerfile: Dockerfile.bingservice
volumes:
- ./proxies.txt:/data/proxies.txt
image: asklyphe/bingservice
googleservice:
restart: unless-stopped
networks:
- lyphenet
- outer
env_file: ".env"
depends_on:
- nats
- bingservice
build:
dockerfile: Dockerfile.googleservice
image: asklyphe/googleservice
nats:
restart: unless-stopped
networks:
- lyphenet
depends_on:
- db
env_file: ".env"
image: nats:2.11.8
command: "-js"
db:
image: postgres:17
networks:
- lyphenet
env_file: ".env"
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'postgres']
volumes:
- ./database:/var/lib/postgresql/data
proxy:
image: nginx:latest
networks:
- lyphenet
- outer
depends_on:
- frontend
- auth-frontend
volumes:
- ./nginx:/etc/nginx/conf.d
- frontend_data:/data/frontend
- auth_frontend_data:/data/auth-frontend
ports:
- "1234:80"
- "1235:81"
# caddy:
# image: caddy:latest
# networks:
# - lyphenet
# - outer
# depends_on:
# - frontend
# - auth-frontend
# volumes:
# - ./Caddyfile:/etc/caddy/Caddyfile
# ports:
# - 1234:1234
# - 1235:1235
networks:
outer:
lyphenet:
internal: true
volumes:
frontend_data:
auth_frontend_data:

27
nginx/default.conf Normal file
View file

@ -0,0 +1,27 @@
server {
listen 81;
server_name 0.0.0.0;
access_log off;
location /static/ {
root /data/auth-frontend;
}
location / {
proxy_pass http://auth-frontend:5843;
}
}
server {
listen 80;
server_name 0.0.0.0;
access_log off;
location /static/ {
root /data/frontend;
}
location / {
proxy_pass http://frontend:5842;
}
}