first commit
This commit is contained in:
18
.gitignore
vendored
Normal file
18
.gitignore
vendored
Normal file
@@ -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
|
||||
|
||||
31
backend/Cargo.toml
Normal file
31
backend/Cargo.toml
Normal file
@@ -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",
|
||||
]
|
||||
|
||||
15
backend/entity/Cargo.toml
Normal file
15
backend/entity/Cargo.toml
Normal file
@@ -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
|
||||
1
backend/entity/src/lib.rs
Normal file
1
backend/entity/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod post;
|
||||
26
backend/entity/src/post.rs
Normal file
26
backend/entity/src/post.rs
Normal file
@@ -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 {}
|
||||
20
backend/migration/Cargo.toml
Normal file
20
backend/migration/Cargo.toml
Normal file
@@ -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",
|
||||
]
|
||||
37
backend/migration/README.md
Normal file
37
backend/migration/README.md
Normal file
@@ -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
|
||||
```
|
||||
12
backend/migration/src/lib.rs
Normal file
12
backend/migration/src/lib.rs
Normal file
@@ -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<Box<dyn MigrationTrait>> {
|
||||
vec![Box::new(m20220120_000001_create_post_table::Migration)]
|
||||
}
|
||||
}
|
||||
42
backend/migration/src/m20220120_000001_create_post_table.rs
Normal file
42
backend/migration/src/m20220120_000001_create_post_table.rs
Normal file
@@ -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,
|
||||
}
|
||||
6
backend/migration/src/main.rs
Normal file
6
backend/migration/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
||||
61
backend/src/main.rs
Normal file
61
backend/src/main.rs
Normal file
@@ -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!"
|
||||
}
|
||||
19
frontend/Cargo.toml
Normal file
19
frontend/Cargo.toml
Normal file
@@ -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"
|
||||
6
frontend/Trunk.toml
Normal file
6
frontend/Trunk.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[build]
|
||||
target = "index.html"
|
||||
dist = "../dist"
|
||||
|
||||
[[proxy]]
|
||||
backend = "http://[::1]:8081/api/"
|
||||
10
frontend/index.html
Normal file
10
frontend/index.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="shortcut icon"type="image/x-icon" href="data:image/x-icon;,">
|
||||
<title>Sycamore App</title>
|
||||
<base href="/"/>
|
||||
</head>
|
||||
<body>loading...</body>
|
||||
</html>
|
||||
61
frontend/src/main.rs
Normal file
61
frontend/src/main.rs
Normal file
@@ -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<G: Html>(cx: Scope) -> View<G> {
|
||||
view! {cx,
|
||||
div{"Test"}
|
||||
/*
|
||||
Router {
|
||||
integration: {HistoryIntegration::new()},
|
||||
view: |cx, route: &ReadSignal<AppRoutes>| {
|
||||
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<G>)]
|
||||
fn HelloServer<G: Html>(cx: Scope) -> View<G> {
|
||||
|
||||
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));
|
||||
}
|
||||
Reference in New Issue
Block a user