From 1e499087b026d9cbe101a56d48e289077ded0ab6 Mon Sep 17 00:00:00 2001 From: Vinod J M Date: Wed, 31 Aug 2022 11:40:50 +0530 Subject: [PATCH] first commit --- .gitignore | 18 ++++++ backend/Cargo.toml | 31 ++++++++++ backend/entity/Cargo.toml | 15 +++++ backend/entity/src/lib.rs | 1 + backend/entity/src/post.rs | 26 ++++++++ backend/migration/Cargo.toml | 20 ++++++ backend/migration/README.md | 37 +++++++++++ backend/migration/src/lib.rs | 12 ++++ .../src/m20220120_000001_create_post_table.rs | 42 +++++++++++++ backend/migration/src/main.rs | 6 ++ backend/src/main.rs | 61 +++++++++++++++++++ frontend/Cargo.toml | 19 ++++++ frontend/Trunk.toml | 6 ++ frontend/index.html | 10 +++ frontend/src/main.rs | 61 +++++++++++++++++++ 15 files changed, 365 insertions(+) create mode 100644 .gitignore create mode 100644 backend/Cargo.toml create mode 100644 backend/entity/Cargo.toml create mode 100644 backend/entity/src/lib.rs create mode 100644 backend/entity/src/post.rs create mode 100644 backend/migration/Cargo.toml create mode 100644 backend/migration/README.md create mode 100644 backend/migration/src/lib.rs create mode 100644 backend/migration/src/m20220120_000001_create_post_table.rs create mode 100644 backend/migration/src/main.rs create mode 100644 backend/src/main.rs create mode 100644 frontend/Cargo.toml create mode 100644 frontend/Trunk.toml create mode 100644 frontend/index.html create mode 100644 frontend/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7cdae7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Generated by Cargo +# will have compiled files and executables +frontend/debug/ +frontend/target/ +backend/debug/ +backend/target/ +dist/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +# Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + diff --git a/backend/Cargo.toml b/backend/Cargo.toml new file mode 100644 index 0000000..c725953 --- /dev/null +++ b/backend/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "backend" +version = "0.1.0" +edition = "2021" + + +[workspace] +members = [".", "entity", "migration"] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +axum = "^0.5" +axum-extra = { version = "^0.3", features = ["spa"] } +clap = { version = "^3", features = ["derive"] } +log = "^0.4" +serde = { version = "^1.0", features = ["derive"] } +serde_json = "^1.0" +tokio = { version = "^1", features = ["full"] } +tower = "^0.4" +tower-http = { version = "^0.3", features = ["full"] } +tracing = "^0.1" +tracing-subscriber = "^0.3" + +[dependencies.sea-orm] +version = "^0.9.2" # sea-orm version +features = [ + "debug-print", + "runtime-tokio-native-tls", + "sqlx-sqlite", +] + diff --git a/backend/entity/Cargo.toml b/backend/entity/Cargo.toml new file mode 100644 index 0000000..45ca30b --- /dev/null +++ b/backend/entity/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "entity" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +name = "entity" +path = "src/lib.rs" + +[dependencies] +serde = { version = "1", features = ["derive"] } + +[dependencies.sea-orm] +version = "^0.9.2" # sea-orm version diff --git a/backend/entity/src/lib.rs b/backend/entity/src/lib.rs new file mode 100644 index 0000000..e8b6291 --- /dev/null +++ b/backend/entity/src/lib.rs @@ -0,0 +1 @@ +pub mod post; diff --git a/backend/entity/src/post.rs b/backend/entity/src/post.rs new file mode 100644 index 0000000..76d7819 --- /dev/null +++ b/backend/entity/src/post.rs @@ -0,0 +1,26 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.3.2 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "posts")] +pub struct Model { + #[sea_orm(primary_key)] + #[serde(skip_deserializing)] + pub id: i32, + pub title: String, + #[sea_orm(column_type = "Text")] + pub text: String, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation {} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + panic!("No RelationDef") + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/backend/migration/Cargo.toml b/backend/migration/Cargo.toml new file mode 100644 index 0000000..0b07816 --- /dev/null +++ b/backend/migration/Cargo.toml @@ -0,0 +1,20 @@ +[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 = "^0.9.2" # sea-orm-migration version +features = [ + # Enable following runtime and db backend features if you want to run migration via CLI + # "runtime-tokio-native-tls", + # "sqlx-postgres", +] diff --git a/backend/migration/README.md b/backend/migration/README.md new file mode 100644 index 0000000..963caae --- /dev/null +++ b/backend/migration/README.md @@ -0,0 +1,37 @@ +# Running Migrator CLI + +- 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 + ``` diff --git a/backend/migration/src/lib.rs b/backend/migration/src/lib.rs new file mode 100644 index 0000000..af8d9b2 --- /dev/null +++ b/backend/migration/src/lib.rs @@ -0,0 +1,12 @@ +pub use sea_orm_migration::prelude::*; + +mod m20220120_000001_create_post_table; + +pub struct Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![Box::new(m20220120_000001_create_post_table::Migration)] + } +} diff --git a/backend/migration/src/m20220120_000001_create_post_table.rs b/backend/migration/src/m20220120_000001_create_post_table.rs new file mode 100644 index 0000000..a2fa021 --- /dev/null +++ b/backend/migration/src/m20220120_000001_create_post_table.rs @@ -0,0 +1,42 @@ +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> { + manager + .create_table( + Table::create() + .table(Posts::Table) + .if_not_exists() + .col( + ColumnDef::new(Posts::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Posts::Title).string().not_null()) + .col(ColumnDef::new(Posts::Text).string().not_null()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Posts::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Posts { + Table, + Id, + Title, + Text, +} diff --git a/backend/migration/src/main.rs b/backend/migration/src/main.rs new file mode 100644 index 0000000..c6b6e48 --- /dev/null +++ b/backend/migration/src/main.rs @@ -0,0 +1,6 @@ +use sea_orm_migration::prelude::*; + +#[async_std::main] +async fn main() { + cli::run_cli(migration::Migrator).await; +} diff --git a/backend/src/main.rs b/backend/src/main.rs new file mode 100644 index 0000000..b0f6080 --- /dev/null +++ b/backend/src/main.rs @@ -0,0 +1,61 @@ +use axum::{response::IntoResponse, routing::get, Router}; +use axum_extra::routing::SpaRouter; +use clap::Parser; +use std::net::{IpAddr, Ipv6Addr, SocketAddr}; +use std::str::FromStr; +use tower::ServiceBuilder; +use tower_http::trace::TraceLayer; + +// Setup the command line interface with clap. +#[derive(Parser, Debug)] +#[clap(name = "server", about = "A server for our wasm project!")] +struct Opt { + /// set the log level + #[clap(short = 'l', long = "log", default_value = "debug")] + log_level: String, + + /// set the listen addr + #[clap(short = 'a', long = "addr", default_value = "::1")] + addr: String, + + /// set the listen port + #[clap(short = 'p', long = "port", default_value = "8080")] + port: u16, + + /// set the directory where static files are to be found + #[clap(long = "static-dir", default_value = "../dist")] + static_dir: String, +} + +#[tokio::main] +async fn main() { + let opt = Opt::parse(); + + // Setup logging & RUST_LOG from args + if std::env::var("RUST_LOG").is_err() { + std::env::set_var("RUST_LOG", format!("{},hyper=info,mio=info", opt.log_level)) + } + // enable console logging + tracing_subscriber::fmt::init(); + + let app = Router::new() + .route("/api/hello", get(hello)) + .merge(SpaRouter::new("/assets", opt.static_dir)) + .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http())); + + let sock_addr = SocketAddr::from(( + IpAddr::from_str(opt.addr.as_str()).unwrap_or(IpAddr::V6(Ipv6Addr::LOCALHOST)), + opt.port, + )); + + log::info!("listening on http://{}", sock_addr); + + axum::Server::bind(&sock_addr) + .serve(app.into_make_service()) + .await + .expect("Unable to start server"); +} + +async fn hello() -> impl IntoResponse { + "hello from server!" +} diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml new file mode 100644 index 0000000..5044f96 --- /dev/null +++ b/frontend/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "frontend" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +console_error_panic_hook = "0.1.7" +env_logger = "0.9.0" +gloo-net = "^0.2" +log = "0.4" +console_log = { version = "0.2", features = ["color"] } +wasm-bindgen-futures = "^0.4" +wasm-logger = "0.2.0" +sycamore = {version = "0.8.0-beta.7", features = ["suspense"]} +sycamore-router = "0.8.0-beta.7" +#yew = "0.19.3" +#yew-router = "0.16.0" diff --git a/frontend/Trunk.toml b/frontend/Trunk.toml new file mode 100644 index 0000000..02bb8e1 --- /dev/null +++ b/frontend/Trunk.toml @@ -0,0 +1,6 @@ +[build] +target = "index.html" +dist = "../dist" + +[[proxy]] +backend = "http://[::1]:8081/api/" diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 0000000..f30bf99 --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,10 @@ + + + + + + Sycamore App + + + loading... + diff --git a/frontend/src/main.rs b/frontend/src/main.rs new file mode 100644 index 0000000..5e213a3 --- /dev/null +++ b/frontend/src/main.rs @@ -0,0 +1,61 @@ +use gloo_net::http::Request; +use wasm_bindgen_futures::spawn_local; +use sycamore::prelude::*; +use sycamore::suspense::Suspense; +use sycamore_router::{Route, Router, RouterProps}; +use log::Level; + +#[derive(Route)] +enum AppRoutes { + #[to("/")] + Home, + #[to("/hello-server")] + HelloServer, + #[not_found] + NotFound, +} + +#[component] +fn App(cx: Scope) -> View { +view! {cx, + div{"Test"} +/* +Router { + integration: {HistoryIntegration::new()}, + view: |cx, route: &ReadSignal| { + view! { + div(class="app") { + (match route.get().as_ref() { + AppRoutes::Home => view! { cx, + "This is the index page" + }, + AppRoutes::HelloServer => view! { cx, + "About this website" + }, + AppRoutes::NotFound => view! { cx, + "404 Not Found" + }, + }) + } + } + } + } + */ + } +} + +#[component(HelloServer)] +fn HelloServer(cx: Scope) -> View { + + view! {cx, + div{"No server response"} + } +} + +fn main() { + console_error_panic_hook::set_once(); + console_log::init_with_level(log::Level::Debug).unwrap(); + + sycamore::render(|cx| view! { cx, App {} }); + //sycamore::render(|cx| App(cx)); +}