Skip to content

Commit

Permalink
WIP Containerize
Browse files Browse the repository at this point in the history
Ok shit seems to be working.
TODO: deployment

- use cargo-run-bin to manage installation of some devel tools
- switch to stable toolchain (requires code changes in app)
  • Loading branch information
senekor committed Dec 2, 2024
1 parent 3bfb08c commit ce7ef66
Show file tree
Hide file tree
Showing 29 changed files with 270 additions and 256 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-03-03 # duplicated in rust-toolchain.toml
targets: wasm32-unknown-unknown
components: clippy, rustfmt
- uses: Swatinem/rust-cache@v2
with:
# Additional non workspace directories to be cached, separated by newlines.
cache-directories: ""
- name: rustfmt
run: cargo fmt --check
- name: clippy
# empty directories for rust-embed
run: |
mkdir app/dist
mkdir docs/book
cargo clippy -- -D warnings
run: cargo clippy -- -D warnings
- name: tests
run: cargo test
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
target
**/*.rs.bk

# leptos
app/dist

# mdbook
docs/book
# cargo-run-bin
.bin

# Mac OS shenanigans
.DS_Store
Expand Down
65 changes: 20 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ repository = "https://github.com/senekor/buenzlimarks"
models = { path = "./models", package = "buenzlimarks_models" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"

[workspace.metadata.bin]
cargo-watch = { version = "8.5.3" }
mdbook = { version = "0.4.40" }
trunk = { version = "0.21.4" }
watchexec-cli = { version = "2.2.0", bins = ["watchexec"] }
55 changes: 55 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from rust:bookworm as build-base

run mkdir /work
workdir /work

# install build tools tools
run curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
run cargo binstall -y cargo-run-bin
copy Cargo.toml Cargo.lock .
run cargo bin --install
run rustup target add wasm32-unknown-unknown

from build-base as build-docs

copy docs docs
workdir /work/docs
run cargo bin mdbook build

from build-base as build-deps

copy models models
copy server/Cargo.toml server/

workdir /work/app
copy app/Cargo.toml app/Trunk.toml app/index.html app/tailwind.config.js app/tailwind.css .
copy app/assets assets
run mkdir src && echo "fn main() {}" > src/main.rs
run cargo bin trunk build --release

workdir /work/server
run mkdir src && echo "fn main() {}" > src/main.rs && touch src/lib.rs
run cargo build --release --bin buenzlimarks

from build-deps as build-app

workdir /work/app
copy app/src src
run touch src/main.rs && cargo bin trunk build --release

from build-deps as build-server

workdir /work/server
copy server/src src
run touch src/main.rs src/lib.rs && cargo build --release --bin buenzlimarks

from debian:bookworm-slim

# expected by tokio::signal::ctrl_c
stopsignal SIGINT

copy --from=build-docs /work/target/docs /docs
copy --from=build-app /work/target/app /app
copy --from=build-server /work/target/release/buenzlimarks /usr/local/bin/

cmd [ "/usr/local/bin/buenzlimarks" ]
4 changes: 2 additions & 2 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ gloo = { version = "0.11.0", default-features = false, features = [
"net",
"storage",
] }
leptos = { version = "0.6.0", features = ["csr", "nightly"] }
leptos_router = { version = "0.6.0", features = ["csr", "nightly"] }
leptos = { version = "0.6.0", features = ["csr"] }
leptos_router = { version = "0.6.0", features = ["csr"] }
models = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
7 changes: 6 additions & 1 deletion app/Trunk.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[build]
dist = "../target/app"

[serve]
port = 3000
proxy_backend = "http://localhost:4000/api"

[[proxy]]
backend = "http://localhost:4000/api"
4 changes: 2 additions & 2 deletions app/src/auth/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Auth {

let navigate = use_navigate();
batch(|| {
(self.set_token)(Token(Some(new_token)));
self.set_token.set(Token(Some(new_token)));
navigate("/", Default::default());
});
});
Expand All @@ -71,7 +71,7 @@ impl Auth {

let navigate = use_navigate();
batch(|| {
(self.set_token)(Token(None));
self.set_token.set(Token(None));
navigate("/", Default::default());
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/auth/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::auth::use_token;
pub fn create_auth_guard() {
let token = use_token();
create_effect(move |_| {
if token().into_inner().is_none() {
if token.get().into_inner().is_none() {
let navigate = use_navigate();
// workaround for navigating during initial routing
// https://docs.rs/leptos_router/0.4.2/leptos_router/fn.use_navigate.html#panics
Expand Down
2 changes: 1 addition & 1 deletion app/src/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn DevelLogin() -> impl IntoView {
class="bg-slate-600 p-2 rounded text-white text-center text-3xl"
placeholder="Enter a user name"
prop:value=user_id
on:input=move |ev| set_user_id(event_target_value(&ev))
on:input=move |ev| set_user_id.set(event_target_value(&ev))
on:keydown=move |ev| {
if &ev.key() == "Enter" {
submit()
Expand Down
24 changes: 12 additions & 12 deletions app/src/components/add_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ pub fn AddButton() -> impl IntoView {

let (state, set_state) = create_signal::<State>(State::None);
create_effect(move |prev| {
if !edit_mode() && prev.is_some() {
set_state(State::None);
if !edit_mode.get() && prev.is_some() {
set_state.set(State::None);
}
});

let on_close = move || set_state(State::None);
let on_close = move || set_state.set(State::None);

view! {
<Show
when=edit_mode
when=move || edit_mode.get()
fallback=|| ()
>
<div class="absolute bottom-3 right-3">
Expand All @@ -50,27 +50,27 @@ pub fn AddButton() -> impl IntoView {
</IconButton>

<Show
when=move || state() == State::Picking
when=move || state.get() == State::Picking
fallback=|| ()
>
<div class="absolute bottom-8 right-8
bg-slate-600 rounded p-4 border-2 border-white
flex flex-col gap-2 text-lg">
<button on:click=move |_| set_state(State::Page) >Page</button>
<button on:click=move |_| set_state(State::Widget) >Widget</button>
<button on:click=move |_| set_state(State::Bookmark) >Bookmark</button>
<button on:click=move |_| set_state.set(State::Page) >Page</button>
<button on:click=move |_| set_state.set(State::Widget) >Widget</button>
<button on:click=move |_| set_state.set(State::Bookmark) >Bookmark</button>
</div>
</Show>

<Show when=move || state().is_entity() fallback=|| ()>
<Show when=move || state.get().is_entity() fallback=|| ()>
<Dialog on_close>
<Show when=move || state() == State::Page fallback=|| ()>
<Show when=move || state.get() == State::Page fallback=|| ()>
<PageForm on_close />
</Show>
<Show when=move || state() == State::Widget fallback=|| ()>
<Show when=move || state.get() == State::Widget fallback=|| ()>
<WidgetForm on_close />
</Show>
<Show when=move || state() == State::Bookmark fallback=|| ()>
<Show when=move || state.get() == State::Bookmark fallback=|| ()>
<BookmarkForm on_close />
</Show>
</Dialog>
Expand Down
16 changes: 8 additions & 8 deletions app/src/components/bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ pub fn Bookmark(bookmark: BookmarkType) -> impl IntoView {
let bookmark = store_value(bookmark);

let edit_mode = use_edit_mode().read();
let no_edit_mode = Signal::derive(move || !edit_mode());
let no_edit_mode = Signal::derive(move || !edit_mode.get());

let (form_open, set_form_open) = create_signal(false);
let on_close = move || set_form_open(false);
let on_close = move || set_form_open.set(false);

view! {
<div class="flex w-full gap-1">
<FlexSpace />
<a
class="text-orange-200 hover:text-orange-400 underline"
href=bookmark().url
href=bookmark.get_value().url
>
{ bookmark().name }
{ bookmark.get_value().name }
</a>
<FlexSpace />
<button
hidden=no_edit_mode
class="w-6 ml-2"
on:click=move |_| set_form_open(true)
on:click=move |_| set_form_open.set(true)
>
<PencilSquareIcon />
</button>
<button
hidden=no_edit_mode
class="w-6"
on:click=move |_| store.dispatch(Action::DeleteBookmark(bookmark()))
on:click=move |_| store.dispatch(Action::DeleteBookmark(bookmark.get_value()))
>
<XMarkIcon />
</button>
</div>
<Show when=form_open fallback=|| () >
<Show when=move || form_open.get() fallback=|| () >
<Dialog on_close >
<BookmarkForm on_close prev_bookmark=bookmark() />
<BookmarkForm on_close prev_bookmark=bookmark.get_value() />
</Dialog>
</Show>

Expand Down
Loading

0 comments on commit ce7ef66

Please sign in to comment.