Have server serve web files

This commit is contained in:
Edward Shen 2021-10-26 23:51:05 -07:00
parent 0d5f9f3288
commit 16bce1d11b
Signed by: edward
GPG key ID: 19182661E818369F
9 changed files with 60 additions and 9 deletions

3
.gitmodules vendored
View file

@ -4,9 +4,6 @@
[submodule "web/vendor/highlight.js"]
path = web/vendor/highlight.js
url = git@github.com:highlightjs/highlight.js.git
[submodule "web/vendor/text-fragments-polyfill"]
path = web/vendor/text-fragments-polyfill
url = git@github.com:GoogleChromeLabs/text-fragments-polyfill.git
[submodule "web/vendor/highlightjs-line-numbers.js"]
path = web/vendor/highlightjs-line-numbers.js
url = git@github.com:wcoder/highlightjs-line-numbers.js.git

15
Cargo.lock generated
View file

@ -797,6 +797,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "minimal-lexical"
version = "0.1.4"
@ -942,6 +952,7 @@ dependencies = [
"rocksdb",
"serde",
"tokio",
"tower-http",
"tracing",
"tracing-subscriber",
]
@ -1609,7 +1620,11 @@ dependencies = [
"futures-util",
"http",
"http-body",
"mime",
"mime_guess",
"pin-project",
"tokio",
"tokio-util",
"tower-layer",
"tower-service",
]

View file

@ -2,6 +2,10 @@
[build]
target = "web/index.html"
release = true
dist = "./dist"
[clean]
dist = "./dist"
[[proxy]]
backend = "http://localhost:8081"

21
build.sh Executable file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -euxo pipefail
# Build frontend assets
yarn
trunk build --release
# Build server
cargo build --release --bin omegaupload-server
# index.html no longer needed, served statically by the upload server
rm dist/index.html
# Prepare assets for upload to webserver
mkdir -p dist/static
# Move everything that's not index.html into a `static` subdir
find dist -type f -exec mv {} dist/static/ ";"
strip target/release/omegaupload-server
cp target/release/omegaupload-server dist/omegaupload-server

View file

@ -20,5 +20,6 @@ rand = "0.8"
rocksdb = { version = "0.17", default_features = false, features = ["zstd"] }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.1", features = ["fs"] }
tracing = { version = "0.1" }
tracing-subscriber = "0.2"

3
server/README.md Normal file
View file

@ -0,0 +1,3 @@
This server responds to four paths:
`GET /`

View file

@ -1,5 +1,6 @@
#![warn(clippy::nursery, clippy::pedantic)]
use std::convert::Infallible;
use std::sync::Arc;
use std::time::Duration;
@ -9,7 +10,7 @@ use axum::extract::{Extension, Path, TypedHeader};
use axum::handler::{get, post};
use axum::http::header::EXPIRES;
use axum::http::StatusCode;
use axum::{AddExtensionLayer, Router};
use axum::{service, AddExtensionLayer, Router};
use chrono::Utc;
use headers::HeaderMap;
use omegaupload_common::Expiration;
@ -18,6 +19,7 @@ use rand::Rng;
use rocksdb::{ColumnFamilyDescriptor, IteratorMode};
use rocksdb::{Options, DB};
use tokio::task;
use tower_http::services::ServeDir;
use tracing::{error, instrument, trace};
use tracing::{info, warn};
@ -50,12 +52,20 @@ async fn main() -> Result<()> {
set_up_expirations(&db);
axum::Server::bind(&"0.0.0.0:8081".parse()?)
let root_service = service::get(ServeDir::new("static"))
.handle_error(|_| Ok::<_, Infallible>(StatusCode::NOT_FOUND));
axum::Server::bind(&"0.0.0.0:8080".parse()?)
.serve(
Router::new()
.route("/", post(upload::<SHORT_CODE_SIZE>))
.route(
"/:code",
get(|| async { include_str!("../../dist/index.html") }),
)
.nest("/static", root_service)
.route(
"/api/:code",
get(paste::<SHORT_CODE_SIZE>).delete(delete::<SHORT_CODE_SIZE>),
)
.layer(AddExtensionLayer::new(db))

View file

@ -19,7 +19,7 @@ http = "0.2"
js-sys = "0.3"
reqwasm = "0.2"
tree_magic_mini = { version = "3", features = ["with-gpl-data"] }
serde = { version = "1.0", featurse = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"]}
wasm-bindgen-futures = "0.4"
zip = { version = "0.5", default-features = false, features = ["deflate"] }

View file

@ -12,9 +12,9 @@
dest="/" />
<link data-trunk rel="scss" href="src/main.scss" />
<script src="main.js" async></script>
<script src="highlight.min.js" defer></script>
<script src="highlightjs-line-numbers.min.js" defer></script>
<script src="static/main.js" async></script>
<script src="static/highlight.min.js" defer></script>
<script src="static/highlightjs-line-numbers.min.js" defer></script>
</head>
</html>