Skip to content

Commit

Permalink
Rewrite most of the application.
Browse files Browse the repository at this point in the history
* Daemonising is no longer supported. Use a proper services supervisor
  (Parsing passed-in sockets is coming)
* syslog logging is gone, use stderr and a proper services supervisor
* New user management system
* Error handling is completely different
* Cleaned up the generation module (but the code is morally equivalent)
* Upgrade axum-login
* Switch to tower-sessions
* Switch to the base64 module
* Change over to using the csrf module and sessions
* New message flashing system using axum-messages
* Much simpler application logic
* src/controllers -> src/web
* Cleaned up a bunch of code

More to come...

Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Mar 12, 2024
1 parent d4852b1 commit b743a50
Show file tree
Hide file tree
Showing 57 changed files with 3,660 additions and 3,785 deletions.
1,953 changes: 831 additions & 1,122 deletions Cargo.lock

Large diffs are not rendered by default.

59 changes: 32 additions & 27 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@

[package]
name = "shadyurl-rust"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
rust-version = "1.65.0"
rust-version = "1.74.1"
authors = ["Elizabeth Myers"]
categories = ["web-programming::http-server"]
keywords = ["web", "service", "axum", "http"]
description = "A webapp that makes URLs shady"
license = "CC0-1.0"
homepage = "https://github.com/Elizafox/ShadyURL-Rust"
repository = "https://github.com/Elizafox/ShadyURL-Rust"
readme = "README.md"

[workspace]
members = [".", "entity", "migration"]
members = [".", "entity", "migration", "service"]

[profile.release]
codegen-units = 1
Expand All @@ -37,47 +39,50 @@ default = ["sqlite"]
mysql = ["sea-orm/sqlx-mysql", "sea-query/backend-postgres", "migration/mysql"]
postgres = ["sea-orm/sqlx-postgres", "sea-query/backend-postgres", "migration/postgres"]
sqlite = ["sea-orm/sqlx-sqlite", "sea-query/backend-sqlite", "migration/sqlite"]
with-tracing = ["axum/tracing", "tokio/tracing", "tower/tracing", "tower-http/tracing"]
tracing = ["tokio/tracing", "tower/tracing", "tower-http/tracing"]

[dependencies]
entity = { path = "entity" }
migration = { path = "migration" }
service = { path = "service" }


######################################
# Package dependencies go below here #
######################################

anyhow = { version = "1.0.80", features = ["backtrace"] }
argon2-kdf = "1.5.2"
askama = { version = "0.12.1", features = ["with-axum"] }
askama_axum = "0.3.0"
async-fred-session = "0.1.5"
axum = { version = "0.6.20", features = ["macros", "http2", "headers"] }
axum-client-ip = "0.4.2"
axum_csrf = { version = "0.7.2", features = ["layer"] }
axum-login = "0.5.0"
base64ct = { version = "1.6.0", features = ["std"] }
askama = { version = "0.12.1", features = ["with-axum", "urlencode", "mime", "mime_guess"], default-features = false }
askama_axum = { version = "0.4.0", default-features = false, features = ["urlencode"] }
async-trait = "0.1.77"
axum = { version = "0.7.4", default-features = false, features = ["form", "http1", "http2", "macros", "tokio", "tower-log"] }
axum-client-ip = "0.5.1"
axum-login = "0.14.0"
axum-messages = "0.5.0"
base64 = "0.22.0"
clap = { version = "4.5.2", features = ["cargo", "derive", "unicode"] }
csrf = "0.4.1"
dotenvy = "0.15.7"
envy = "0.4.2"
heck = { version = "0.4.1", features = ["unicode"] }
itertools = "0.12.1"
lazy_static = "1.4.0"
nix = { version = "0.27.1", default-features = false, features = ["dir", "process", "user"] }
num_cpus = "1.16.0"
once_cell = { version = "1.19.0", features = ["parking_lot"] }
proctitle = "0.1.1"
rand = { version = "0.8.5", features = ["min_const_gen"] }
rand_chacha = { version = "0.3.1", features = ["simd"] }
password-auth = "1.0.0"
#proctitle = "0.1.1"
rand="0.8.5"
rand_distr = "0.4.3"
sea-orm = { version = "0.12.14", default-features = false, features = ["macros", "runtime-tokio-native-tls", "with-chrono"] }
sea-query = { version = "0.30.7", default-features = false, features = ["thread-safe", "with-chrono"] }
rpassword = "7.3.1"
sea-orm = { version = "1.0.0-rc.1", default-features = false, features = ["macros", "runtime-tokio-native-tls", "with-time"] }
sea-query = { version = "0.31.0-rc.4", default-features = false, features = ["thread-safe", "with-time"] }
serde = { version = "1.0.197", features = ["derive"] }
syslog-tracing = "0.3.0"
tokio = { version = "1.36.0", features = ["fs", "macros", "parking_lot", "rt-multi-thread", "signal", "time"] }
tracing = { version = "0.1.40", features = ["async-await", "log"]}
tracing-subscriber = { version = "0.3.18", features = ["local-time", "parking_lot"]}
thiserror = "1.0.57"
time = "0.3.34"
tokio = { version = "1.36.0", default-features = false, features = ["macros", "parking_lot", "rt-multi-thread", "signal", "time"] }
tracing = { version = "0.1.40", features = ["async-await", "log"] }
tracing-subscriber = { version = "0.3.18", features = ["local-time", "parking_lot", "time"] }
tower = { version = "0.4.13", features = ["timeout", "tokio"] }
tower-http = { version = "0.4.4", features = ["fs", "timeout", "tokio", "tower"] }
tower-http = { version = "0.5.2", features = ["fs", "normalize-path", "timeout", "tokio"] }
tower-sessions = { version = "0.11.0", default-features = false, features = ["axum-core"] }
tower-sessions-redis-store = "0.11.0"
url = "2.5.0"
validator = { version = "0.16.1", features = ["derive"] }
validator = { version = "0.17.0", features = ["derive"] }
17 changes: 17 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: CC0-1.0
*
* build.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

fn main() {
println!("cargo:rerun-if-changed=templates");
}
3 changes: 2 additions & 1 deletion entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
name = "entity"
version = "0.1.0"
edition = "2021"
license = "CC0-1.0"
publish = false

[lib]
name = "entity"
path = "src/lib.rs"

[dependencies]
sea-orm = { version = "^0", default-features = false, features = ["with-chrono"] }
sea-orm = { version = "1.0.0-rc.1", default-features = false, features = ["with-time"] }
1 change: 1 addition & 0 deletions entity/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
pub mod prelude;

pub mod url;
pub mod user;
1 change: 1 addition & 0 deletions entity/src/entities/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
pub use super::url::Entity as Url;
pub use super::user::Entity as User;
41 changes: 41 additions & 0 deletions entity/src/entities/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* SPDX-License-Identifier: CC0-1.0
*
* entity/src/entities/user.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
use sea_orm::entity::prelude::*;

#[derive(Clone, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
pub username: String,
pub password_hash: String,
}

impl std::fmt::Debug for Model {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("User")
.field("id", &self.id)
.field("username", &self.username)
.field("password_hash", &"[redacted]")
.finish()
}
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
30 changes: 2 additions & 28 deletions env_example
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,5 @@ DATABASE_URL="sqlite:/tmp/test.db"
# Redis URL, must start with redis:// as below.
REDIS_URL="redis://127.0.0.1"

# This is for cookies, don't disclose it, and you really need to change this.
# This is encoded in base64 now; if you have a previous secret, you can do this:
# printf '%s' '<old_secret_here>' | base64
#
# TIP: for new secrets, generate it with the script provided:
# /path/to/shadyurl-rust/utils/secret_key.sh
SECRET_KEY=ZHVtbXlkdW1teWR1bW15ZHVtbXlkdW1teWR1bW15ZHVtbXlkdW1teWR1bW15ZHVtbXlkdW1teWR1bW15ZHVtbXk=

# Login username, for the /login endpoint for management
USERNAME=Elizafox

# Password hash to use for the login endpoint; install argon2 and use the shell script provided:
# /path/to/shadyurl-rust/utils/password_hash.sh
PASSWORD_HASH='$argon2id$v=19$m=16,t=10,p=1$ZGFibW9GVU9GYVhQSkpxSw$6IJIHbZZzHyw/qVZK3EjZQ'

# Whether or not to daemonise, set to true or false
# If you are running under a supervisor such as systemd, it's highly recommended to say "false" here.
DAEMON=false

# Path to PID file, defaults to /var/run/shadyurl.pid
PID_FILE="/tmp/shadyurl.pid"

# Set the log level, with 0 being only errors, and 4 being debug
# It's recommended to keep this around 0 or 1
LOG_LEVEL=1

# Whether or not to log to stderr
LOG_STDERR=false
# Redis pool size, defaults to 3, increase if you need more
REDIS_POOL_SIZE=3
4 changes: 2 additions & 2 deletions migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
name = "migration"
version = "0.1.0"
edition = "2021"
license = "CC0-1.0"
publish = false

[lib]
Expand All @@ -28,5 +29,4 @@ postgres = ["sea-orm-migration/sqlx-postgres"]
sqlite = ["sea-orm-migration/sqlx-sqlite"]

[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
sea-orm-migration = { version = "0.12.14", features = ["runtime-tokio-native-tls"] }
sea-orm-migration = { version = "1.0.0-rc.1", default-features = false, features = ["runtime-tokio-native-tls", "with-time"] }
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use sea_orm_migration::prelude::*;

mod m20230702_081718_create_table;
mod m20230713_214521_add_unique_constraint;
mod m20240302_082409_create_user_table;

pub struct Migrator;

Expand All @@ -25,6 +26,7 @@ impl MigratorTrait for Migrator {
vec![
Box::new(m20230702_081718_create_table::Migration),
Box::new(m20230713_214521_add_unique_constraint::Migration),
Box::new(m20240302_082409_create_user_table::Migration),
]
}
}
14 changes: 14 additions & 0 deletions migration/src/m20230713_214521_add_unique_constraint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/* SPDX-License-Identifier: CC0-1.0
*
* migration/src/m20230713_214521_add_unique_constraint.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

use sea_orm_migration::prelude::*;

#[derive(Iden)]
Expand Down
61 changes: 61 additions & 0 deletions migration/src/m20240302_082409_create_user_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* SPDX-License-Identifier: CC0-1.0
*
* migration/src/m20240302_082409_create_user_table.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[allow(clippy::enum_variant_names)]
#[derive(Iden)]
enum User {
Table,
Id,
Username,
PasswordHash,
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(User::Table)
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(User::Username)
.string()
.unique_key()
.not_null(),
)
.col(ColumnDef::new(User::PasswordHash).string().not_null())
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await
}
}
44 changes: 44 additions & 0 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-License-Identifier: CC0-1.0
#
# service/Cargo.toml
#
# This file is a component of ShadyURL by Elizabeth Myers.
#
# To the extent possible under law, the person who associated CC0 with
# ShadyURL has waived all copyright and related or neighboring rights
# to ShadyURL.
#
# You should have received a copy of the CC0 legalcode along with this
# work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.

[package]
name = "service"
version = "0.1.0"
edition = "2021"
license = "CC0-1.0"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "service"
path = "src/lib.rs"

[features]
default = ["sqlite"]
mysql = ["sea-orm/sqlx-mysql"]
postgres = ["sea-orm/sqlx-postgres"]
sqlite = ["sea-orm/sqlx-sqlite"]

[dependencies]
entity = { path = "../entity" }

[dependencies.sea-orm]
version = "1.0.0-rc.1"
features = [
"debug-print",
"runtime-tokio-native-tls",
]

[dev-dependencies]
tokio = { version = "1.36", features = ["macros", "rt"] }
21 changes: 21 additions & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* SPDX-License-Identifier: CC0-1.0
*
* service/src/lib.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

mod mutation;
mod query;

pub use mutation::*;
pub use query::*;

pub use sea_orm;
Loading

0 comments on commit b743a50

Please sign in to comment.