-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
723 additions
and
144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
[package] | ||
name = "realm-api" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1", features = ["full"] } | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
mlua = { version = "0.9", features = ["lua54", "vendored", "serialize"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1" | ||
const_format = "0.2" | ||
clap = { version = "4", features = ["derive"] } | ||
chrono = { version = "0.4", features = ["serde"] } | ||
sonyflake = { version = "0.2.0" } | ||
argon2 = "0.5" | ||
color-eyre = "0.6" | ||
|
||
entity = { path = "../entity" } | ||
migration = { path = "../migration" } | ||
|
||
[dependencies.salvo] | ||
version = "0.68" | ||
features = [ | ||
"oapi", | ||
"eyre", | ||
"websocket", | ||
"logging", | ||
"basic-auth", | ||
"compression", | ||
"affix", | ||
] | ||
|
||
[dependencies.sea-orm] | ||
version = "1.0.0-rc.7" | ||
features = [ | ||
"runtime-tokio", | ||
"sqlx-postgres", | ||
"debug-print", | ||
"macros", | ||
"with-uuid", | ||
"with-json", | ||
"with-chrono", | ||
] | ||
|
||
[dependencies.sea-orm-migration] | ||
version = "1.0.0-rc.7" | ||
features = [ | ||
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. | ||
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. | ||
# e.g. | ||
"runtime-tokio", # `ASYNC_RUNTIME` feature | ||
"sqlx-postgres", # `DATABASE_DRIVER` feature | ||
] |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
pub mod config; | ||
pub mod db; | ||
pub mod error; | ||
pub mod handler; | ||
pub mod middleware; | ||
pub mod models; | ||
pub mod router; | ||
pub mod state; | ||
pub mod utils; | ||
|
||
use crate::router::make_route; | ||
use color_eyre::Result; | ||
use salvo::prelude::*; | ||
use tokio::signal; | ||
use tracing::info; | ||
use tracing_subscriber::EnvFilter; | ||
|
||
#[tokio::main] | ||
pub async fn main() -> Result<()> { | ||
let filter = EnvFilter::from_default_env(); | ||
tracing_subscriber::fmt().with_env_filter(filter).with_test_writer().init(); | ||
color_eyre::install()?; | ||
|
||
|
||
|
||
let config = config::get_config().await.unwrap_or_else(|_| { | ||
info!("failed to read config file, using default instead"); | ||
config::Config::default() | ||
}); | ||
|
||
let acceptor = TcpListener::new(config.host.clone() + ":" + &config.port.to_string()) | ||
.bind() | ||
.await; | ||
|
||
let router = Router::with_path("api").push(make_route(&config).await); | ||
|
||
// TODO: http3 | ||
let server = Server::new(acceptor); | ||
let handle = server.handle(); | ||
|
||
// graceful shutdown | ||
tokio::spawn(async move { | ||
signal::ctrl_c().await.expect("failed to listen for event"); | ||
handle.stop_graceful(None); | ||
}); | ||
|
||
server.serve(router).await; | ||
Ok(()) | ||
} | ||
|
||
// Break this test into smaller routes. | ||
#[cfg(test)] | ||
mod tests { | ||
use salvo::prelude::*; | ||
use salvo::test::{ResponseExt, TestClient}; | ||
|
||
use crate::config::Config; | ||
|
||
#[tokio::test] | ||
async fn test_basic_auth() { | ||
let test_config = Config::default(); | ||
let service = Service::new(super::make_route(&test_config).await); | ||
|
||
let url = format!("http://{}:{}/", test_config.host, test_config.port); | ||
|
||
let content = TestClient::get(url.clone() + "hello_admin") | ||
.basic_auth("admin", Some("admin")) | ||
.send(&service) | ||
.await | ||
.take_string() | ||
.await | ||
.unwrap(); | ||
assert!(content.contains("Admin")); | ||
|
||
let content = TestClient::get(url.clone() + "hello_admin") | ||
.basic_auth("admin", Some("admin2")) | ||
.send(&service) | ||
.await | ||
.take_string() | ||
.await | ||
.unwrap(); | ||
assert!(content.contains("Unauthorized")); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "entity" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sea-orm = { version = "1.0.0-rc.7" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pub fn add(left: u64, right: u64) -> u64 { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "migration" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
name = "migration" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
async-std = { version = "1", features = ["attributes", "tokio1"] } | ||
|
||
[dependencies.sea-orm-migration] | ||
version = "1.0.0-rc.7" | ||
features = [ | ||
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. | ||
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. | ||
# e.g. | ||
"runtime-tokio", # `ASYNC_RUNTIME` feature | ||
"sqlx-postgres", # `DATABASE_DRIVER` feature | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Running Migrator CLI | ||
|
||
- Generate a new migration file | ||
```sh | ||
cargo run -- generate MIGRATION_NAME | ||
``` | ||
- Apply all pending migrations | ||
```sh | ||
cargo run | ||
``` | ||
```sh | ||
cargo run -- up | ||
``` | ||
- Apply first 10 pending migrations | ||
```sh | ||
cargo run -- up -n 10 | ||
``` | ||
- Rollback last applied migrations | ||
```sh | ||
cargo run -- down | ||
``` | ||
- Rollback last 10 applied migrations | ||
```sh | ||
cargo run -- down -n 10 | ||
``` | ||
- Drop all tables from the database, then reapply all migrations | ||
```sh | ||
cargo run -- fresh | ||
``` | ||
- Rollback all applied migrations, then reapply all migrations | ||
```sh | ||
cargo run -- refresh | ||
``` | ||
- Rollback all applied migrations | ||
```sh | ||
cargo run -- reset | ||
``` | ||
- Check the status of all migrations | ||
```sh | ||
cargo run -- status | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pub use sea_orm_migration::prelude::*; | ||
|
||
mod m20220101_000001_create_table; | ||
|
||
pub struct Migrator; | ||
|
||
#[async_trait::async_trait] | ||
impl MigratorTrait for Migrator { | ||
fn migrations() -> Vec<Box<dyn MigrationTrait>> { | ||
vec![Box::new(m20220101_000001_create_table::Migration)] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
// Replace the sample below with your own migration scripts | ||
todo!(); | ||
|
||
manager | ||
.create_table( | ||
Table::create() | ||
.table(Post::Table) | ||
.if_not_exists() | ||
.col( | ||
ColumnDef::new(Post::Id) | ||
.integer() | ||
.not_null() | ||
.auto_increment() | ||
.primary_key(), | ||
) | ||
.col(ColumnDef::new(Post::Title).string().not_null()) | ||
.col(ColumnDef::new(Post::Text).string().not_null()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
// Replace the sample below with your own migration scripts | ||
todo!(); | ||
|
||
manager | ||
.drop_table(Table::drop().table(Post::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Post { | ||
Table, | ||
Id, | ||
Title, | ||
Text, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
cli::run_cli(migration::Migrator).await; | ||
} |
Oops, something went wrong.