Skip to content

Commit 51f0711

Browse files
feat(front): serving front and ci setup
1 parent 04714c1 commit 51f0711

File tree

10 files changed

+116
-23
lines changed

10 files changed

+116
-23
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PORT=8080
22
DATABASE_URL=postgres://posgres:[email protected]:5432/your-db
33
RUST_LOG=info
4+
STATIC_FOLDER=./front/dist

.github/workflows/shuttle.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ jobs:
99
deploy:
1010
runs-on: ubuntu-latest
1111
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v3
14+
15+
- name: Setup Rust
16+
uses: actions-rs/toolchain@v1
17+
with:
18+
toolchain: stable
19+
target: wasm32-unknown-unknown
20+
override: true
21+
22+
- name: Getting cargo make
23+
uses: davidB/rust-cargo-make@v1
24+
25+
- name: Install Dioxus CLI
26+
run: cargo install dioxus-cli
27+
28+
- name: Build the front-end
29+
run: cargo make front-build
30+
1231
- name: deployment
1332
uses: shuttle-hq/deploy-action@main
1433
with:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Secrets.toml
44
.env
55
node_modules
66
api.local.http
7-
7+
static/

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ shared = { version = "0.1.0", path = "./shared" }
1313
api-lib = { version = "0.1.0", path = "./api/lib" }
1414
# actix and sqlx
1515
actix-web = "4.3.1"
16+
actix-files = "0.6.2"
1617
sqlx = { version = "0.6.3", default-features = false, features = [ "runtime-actix-native-tls", "macros", "postgres", "uuid", "chrono", "json" ] }
1718
# serde
1819
serde = { version = "1.0.164", features = ["derive"] }

Makefile.toml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ install_crate = "dioxus-cli"
1818
command = "dioxus"
1919
args = ["serve"]
2020

21-
# [tasks.front]
22-
# workspace = false
23-
# script_runner = "@shell"
24-
# script = '''
25-
# rm -rf static
26-
# mkdir static
27-
# cd front
28-
# dioxus build --release
29-
# mkdir -p dist/assets/images
30-
# cp -a src/images/* dist/assets/images/
31-
# cp -r dist/* ../static
32-
# rm -rf dist
33-
# '''
21+
[tasks.front-build]
22+
workspace = false
23+
script_runner = "@shell"
24+
script = '''
25+
rm -rf api/shuttle/static
26+
mkdir api/shuttle/static
27+
cd front
28+
dioxus build --release
29+
cp -r dist/* ../api/shuttle/static
30+
'''
3431

3532
# local db
3633
[tasks.db-start]

api/actix/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ api-lib = { workspace = true }
1111
sqlx = { workspace = true }
1212
# actix
1313
actix-web = { workspace = true }
14+
actix-files = { workspace = true }
1415
actix-cors = "0.6.4"
1516
# utils
1617
dotenv = "0.15"

api/actix/src/main.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,28 @@ async fn main() -> std::io::Result<()> {
2828
// starting the server
2929
tracing::info!("🚀🚀🚀 Starting Actix server at {}", address);
3030

31+
// static files
32+
let static_folder = std::env::var("STATIC_FOLDER").unwrap_or("./front/dist".to_string());
33+
3134
HttpServer::new(move || {
3235
// CORS
3336
let cors = Cors::permissive();
3437

35-
App::new().wrap(cors).service(
36-
web::scope("/api")
37-
.app_data(repo.clone())
38-
.configure(api_lib::health::service)
39-
.configure(
40-
api_lib::v1::service::<api_lib::film_repository::PostgresFilmRepository>,
41-
),
42-
)
38+
App::new()
39+
.wrap(cors)
40+
.service(
41+
web::scope("/api")
42+
.app_data(repo.clone())
43+
.configure(api_lib::health::service)
44+
.configure(
45+
api_lib::v1::service::<api_lib::film_repository::PostgresFilmRepository>,
46+
),
47+
)
48+
.service(
49+
actix_files::Files::new("/", &static_folder)
50+
.show_files_listing()
51+
.index_file("index.html"),
52+
)
4353
})
4454
.bind(&address)
4555
.unwrap_or_else(|err| {

api/shuttle/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ shuttle-actix-web = "0.19.0"
1616
# shuttle-aws-rds = { version = "0.18.0", features = ["postgres"] }
1717
shuttle-shared-db = { version = "0.19.0", features = ["postgres"] }
1818
sqlx = { workspace = true }
19-
2019
# static files
2120
shuttle-static-folder = "0.19.0"
2221
# actix
2322
actix-web = { workspace = true }
23+
actix-files = { workspace = true }
2424
tokio = "1.28.2"

api/shuttle/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use actix_web::web::{self, ServiceConfig};
24
use shuttle_actix_web::ShuttleActixWeb;
35
use shuttle_runtime::CustomError;
@@ -6,6 +8,7 @@ use sqlx::Executor;
68
#[shuttle_runtime::main]
79
async fn actix_web(
810
#[shuttle_shared_db::Postgres()] pool: sqlx::PgPool,
11+
#[shuttle_static_folder::StaticFolder(folder = "static")] static_folder: PathBuf,
912
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
1013
// initialize the database if not already initialized
1114
pool.execute(include_str!("../../db/schema.sql"))
@@ -25,6 +28,11 @@ async fn actix_web(
2528
.configure(
2629
api_lib::v1::service::<api_lib::film_repository::PostgresFilmRepository>,
2730
),
31+
)
32+
.service(
33+
actix_files::Files::new("/", static_folder)
34+
.show_files_listing()
35+
.index_file("index.html"),
2836
);
2937
};
3038

0 commit comments

Comments
 (0)