Skip to content

Commit

Permalink
Merge pull request #554 from mikotoIO/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
TheCactusBlue authored Sep 3, 2024
2 parents beb68f5 + b67632b commit 48e9d2d
Show file tree
Hide file tree
Showing 80 changed files with 11,225 additions and 570 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docker-superego.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: Build Docker for Superego (Auth)
name: Build Docker for Superego
on:
push:
paths:
- apps/auth/**
- apps/superego/**
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: mikotoIO/superego
DOCKERFILE: ./apps/auth/Dockerfile
DOCKERFILE: ./apps/superego/Dockerfile

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ yarn-error.log*
.env.production.local

# TODO: separate the envfiles
/apps/auth/.env
/apps/superego/.env

# turbo
.turbo
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"apps/content-proxy",
"apps/auth",
"apps/superego",
"packages/muonic",
"packages/muonic/macros",
"packages/muonic/core",
Expand Down
92 changes: 0 additions & 92 deletions apps/auth/src/routes/mod.rs

This file was deleted.

1 change: 1 addition & 0 deletions apps/auth/.env.example → apps/superego/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PORT=9503

# Services
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/mikoto
REDIS_URL=redis://localhost:6379

# JWT
SECRET=biribiribestgirl
Expand Down
10 changes: 6 additions & 4 deletions apps/auth/Cargo.toml → apps/superego/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tokio = { version = "1.35.1", features = ["full"] }
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
async-trait = "0.1.81"
time = { version = "0.3.36", features = ["serde"] }
chrono = "0.4.38"
uuid = { version = "1.8.0", features = ["serde", "v4"] }
nanoid = "0.4.0"
hex = "0.4.3"
Expand All @@ -21,10 +21,10 @@ log = "0.4.21"
pretty_env_logger = "0.5.0"

# web server
axum = { version = "0.7.5", features = ["multipart"] }
axum = { version = "0.7.5", features = ["ws", "macros", "multipart"] }
axum-extra = "0.9.3"
tower-http = { version = "0.5.2", features = ["cors"] }
schemars = { version = "0.8.21", features = ["uuid1"] }
schemars = { version = "0.8.21", features = ["uuid1", "chrono"] }
aide = { version = "0.13.4", features = [
"scalar",
"axum",
Expand Down Expand Up @@ -53,6 +53,8 @@ hcaptcha = { version = "2.4.7", default-features = false, features = [
"ext",
"hex",
] }
futures-util = "0.3.30"
fred = "9.1.2"

# database
[dependencies.sqlx]
Expand All @@ -62,7 +64,7 @@ features = [
"tls-rustls",
"postgres",
"uuid",
"time",
"chrono",
"json",
"migrate",
]
2 changes: 1 addition & 1 deletion apps/auth/Dockerfile → apps/superego/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ COPY --from=planner /mikoto/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .

WORKDIR /mikoto/apps/auth
WORKDIR /mikoto/apps/superego
RUN cargo build --release

FROM alpine:latest
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions apps/auth/src/db.rs → apps/superego/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::time::Duration;

use fred::{
prelude::{ClientLike, RedisClient},
types::{Builder, RedisConfig},
};
use sqlx::PgPool;
use tokio::{sync::OnceCell, time::timeout};

Expand Down Expand Up @@ -30,3 +34,23 @@ pub async fn init() -> Result<&'static PgPool, Error> {
pub fn db() -> &'static PgPool {
DB.get().expect("Database not initialized")
}

static REDIS: OnceCell<RedisClient> = OnceCell::const_new();

pub async fn init_redis() -> Result<&'static RedisClient, Error> {
REDIS
.get_or_try_init(|| async {
let redis = Builder::from_config(
RedisConfig::from_url(&env().redis_url).expect("Invalid Redis URL"),
)
.build()
.unwrap();
redis.init().await.expect("Failed to connect to Redis");
Ok(redis)
})
.await
}

pub fn redis() -> &'static RedisClient {
REDIS.get().expect("Redis not initialized")
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use uuid::Uuid;

use crate::error::Error;

pub mod refresh_token;
pub mod verification;

pub use refresh_token::*;
pub use verification::*;

#[derive(FromRow, Serialize, JsonSchema)]
#[sqlx(rename_all = "camelCase")]
#[schemars(example = "account_example")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
error::Error,
functions::{primitive_now, sha3::sha3},
};
use crate::{error::Error, functions::sha3::sha3};
use chrono::{NaiveDateTime, TimeDelta, Utc};
use nanoid::nanoid;
use sqlx::FromRow;
use uuid::Uuid;
Expand All @@ -11,7 +9,7 @@ use uuid::Uuid;
pub struct RefreshToken {
pub id: Uuid,
pub token: String,
pub expires_at: time::PrimitiveDateTime,
pub expires_at: NaiveDateTime,
pub account_id: Uuid,
}

Expand All @@ -23,7 +21,7 @@ impl RefreshToken {
id: Uuid::new_v4(),
token: sha3(&refresh_token),
account_id,
expires_at: primitive_now() + time::Duration::days(30),
expires_at: Utc::now().naive_utc() + TimeDelta::days(30),
},
refresh_token,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use chrono::{NaiveDateTime, TimeDelta, Utc};
use nanoid::nanoid;
use sqlx::FromRow;
use uuid::Uuid;

use crate::{error::Error, functions::primitive_now};
use crate::error::Error;

#[derive(FromRow, Serialize)]
#[sqlx(rename_all = "camelCase")]
Expand All @@ -11,7 +12,7 @@ pub struct AccountVerification {
pub category: String,
pub token: String,
pub account_id: Uuid,
pub expires_at: time::PrimitiveDateTime,
pub expires_at: NaiveDateTime,
}

impl AccountVerification {
Expand All @@ -36,7 +37,7 @@ impl AccountVerification {
return Err(Error::NotFound);
}

if (verification.expires_at - primitive_now()).whole_seconds() < 0 {
if (verification.expires_at - Utc::now().naive_utc()).num_seconds() < 0 {
return Err(Error::NotFound);
}

Expand All @@ -52,7 +53,7 @@ impl AccountVerification {
category: "PASSWORD_RESET".to_string(),
token: nanoid!(48),
account_id,
expires_at: primitive_now() + time::Duration::hours(1),
expires_at: Utc::now().naive_utc() + TimeDelta::hours(1),
};
sqlx::query(
r#"
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions apps/superego/src/entities/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use chrono::NaiveDateTime;
use schemars::JsonSchema;
use sqlx::prelude::FromRow;
use uuid::Uuid;

#[derive(sqlx::Type, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[sqlx(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ChannelType {
Text,
Voice,
Document,
Application,
Thread,
Category,
}

#[derive(FromRow, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[sqlx(rename_all = "camelCase")]
pub struct Channel {
pub id: Uuid,
pub space_id: Uuid,

pub parent_id: Option<Uuid>,
pub order: i32,

pub name: String,
#[serde(rename = "type")]
#[sqlx(rename = "type")]
pub category: ChannelType,
pub last_updated: NaiveDateTime,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use uuid::Uuid;

mod account;
mod bot;
mod refresh_token;
mod channel;
mod space;
mod user;
mod verification;

pub use account::*;
pub use bot::*;
pub use refresh_token::*;
pub use channel::*;
pub use space::*;
pub use user::*;
pub use verification::*;

pub struct MultiFactor {
pub user_id: Uuid,
Expand Down
Loading

0 comments on commit 48e9d2d

Please sign in to comment.