actix server init

This commit is contained in:
Jovi Hsu 2021-11-02 21:38:59 +08:00
parent abd4299df3
commit 160acb72a2
13 changed files with 69 additions and 12034 deletions

View File

@ -15,6 +15,10 @@ repository = "https://www.github.com/HsuJv/webgateway"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "3.3.2"
actix-files = "0.5.0"
tokio-tar = "0.3.0"
urlencoding = "2.1.0"
[profile.dev]
panic = "unwind"

View File

@ -1,4 +1,5 @@
[tasks.install]
dependencies = ["build"]
script = '''
mkdir -p $INSTALL_PATH
cp $CARGO_MAKE_CRATE_TARGET_DIRECTORY/debug/$CARGO_MAKE_CRATE_NAME $INSTALL_PATH

View File

@ -1,3 +1,38 @@
fn main() {
println!("Hello, world!");
use actix_files as fs;
use actix_web::http::StatusCode;
use actix_web::*;
const STATIC_DIR: &str = "./static/";
// #[get("/{id}/{name}/index.html")]
// async fn index(web::Path((id, name)): web::Path<(u32, String)>) -> impl Responder {
// format!("Hello {}! id:{}", name, id)
// }
async fn index(_: HttpRequest) -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open(format!("{}/index.html", STATIC_DIR))?)
}
async fn p404() -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open(format!("{}/p404.html", STATIC_DIR))?
.set_status_code(StatusCode::NOT_FOUND))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.service(web::resource("/").route(web::get().to(index)))
.service(
fs::Files::new("/static", STATIC_DIR)
.prefer_utf8(true)
.index_file(format!("{}/index.html", STATIC_DIR))
.use_etag(true)
.default_handler(web::route().to(p404)),
)
.default_service(web::route().to(p404))
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}

View File

@ -1,24 +1,22 @@
[tasks.build]
command = "wasm-pack"
args = ["build"]
args = ["build", "--target", "web", "--out-name", "wasm", "--out-dir", "./pkg", "--dev"]
[tasks.install]
dependencies=["install_wasm", "install_static"]
dependencies=["build", "install_wasm", "install_html"]
[tasks.install_wasm]
script = '''
mkdir -p $FE_WASM_INSTALL_PATH
cp -r ./pkg $FE_WASM_INSTALL_PATH
mkdir -p $FE_STATIC_INSTALL_PATH
cp ./pkg/wasm.js $FE_STATIC_INSTALL_PATH
cp ./pkg/wasm_bg.wasm $FE_STATIC_INSTALL_PATH
'''
[tasks.install_static]
[tasks.install_html]
script = '''
mkdir -p $FE_STATIC_INSTALL_PATH
cp www/index.* $FE_STATIC_INSTALL_PATH
cp www/bootstrap.js $FE_STATIC_INSTALL_PATH
cp static/* $FE_STATIC_INSTALL_PATH
'''
[env]
FE_WASM_INSTALL_PATH="${INSTALL_PATH}/html"
FE_STATIC_INSTALL_PATH="${INSTALL_PATH}/html/www"
FE_STATIC_INSTALL_PATH="${INSTALL_PATH}/static"

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<title>Hello, World</title>
<title>Web Gateway</title>
<style type="text/css">
.navbar {
width: 100%;
@ -31,7 +31,12 @@
height: 58px;
}
</style>
<script src="./bootstrap.js" defer></script>
<script type="module" defer>
import init from "/static/wasm.js";
import { run_app } from "/static/wasm.js";
await init();
await run_app();
</script>
</head>
<body>

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<body>
<!-- 404 Not found -->
<h1>404 Not found</h1>
<p>The requested URL was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body>

View File

@ -1,2 +0,0 @@
node_modules
dist

View File

@ -1,5 +0,0 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));

View File

@ -1,5 +0,0 @@
import { run_app } from '../pkg/webgateway_fe_bg.js';
async function main() {
run_app();
}
main()

File diff suppressed because it is too large Load Diff

View File

@ -1,32 +0,0 @@
{
"name": "webgateway",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"dev": "webpack-dev-server"
},
"repository": {
"type": "git",
"url": "git+https://github.com/HsuJv/webgateway.git"
},
"keywords": [
"webassembly",
"wasm",
"rust",
"webpack"
],
"author": "Jovi Hsu <jv.hsu@outlook.com>",
"license": "MIT",
"homepage": "",
"dependencies": {
"webgateway": "file:../pkg"
},
"devDependencies": {
"webpack": "^4.29.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"copy-webpack-plugin": "^5.0.0"
}
}

View File

@ -1,14 +0,0 @@
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');
module.exports = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
mode: "development",
plugins: [
new CopyWebpackPlugin(['index.html'])
],
};

3
run.sh Normal file
View File

@ -0,0 +1,3 @@
set -e
cargo make install
cd build && ./webgateway-be