@@ -23,7 +23,7 @@ Dual-licensed under `MIT` or the [Unlicense](http://unlicense.org/).
2323
2424## <a name =' Features ' ></a >Features
2525
26- - Embed static resources in single self-contained executuble
26+ - Embed static resources in single self-contained executable
2727- Serve static resources in ` actix-web `
2828- Install dependencies with [ npm] ( https://npmjs.org ) package manager
2929- Run custom ` npm ` run commands (such as [ webpack] ( https://webpack.js.org/ ) )
@@ -46,12 +46,16 @@ Add to `Cargo.toml` dependencies related to `actix-web-static-files`:
4646
4747``` toml
4848[dependencies ]
49- actix-web = " 4.0"
50- actix-web-static-files = " 4.0"
51- static-files = " 0.2.1"
49+ actix-web.workspace = true
50+ actix-web-static-files.workspace = true
51+ static-files.workspace = true
52+
53+ [dev-dependencies ]
54+ reqwest.workspace = true
55+ assert_cmd.workspace = true
5256
5357[build-dependencies ]
54- static-files = " 0.2.1 "
58+ static-files.workspace = true
5559```
5660
5761Add ` build.rs ` with call to bundle resources:
@@ -74,13 +78,20 @@ include!(concat!(env!("OUT_DIR"), "/generated.rs"));
7478
7579#[actix_web::main]
7680async fn main() -> std::io::Result<()> {
77- HttpServer::new(move || {
81+ let listen = std::env::var("LISTEN").unwrap_or_else(|_| "127.0.0.1:8081".into());
82+ let server = HttpServer::new(|| {
7883 let generated = generate();
7984 App::new().service(ResourceFiles::new("/", generated))
8085 })
81- .bind("127.0.0.1:8080")?
82- .run()
83- .await
86+ .bind(listen)?;
87+
88+ if let Some(addr) = server.addrs().first() {
89+ println!("{:05}", addr.port());
90+ }
91+
92+ let handle = actix_web::rt::spawn(server.run());
93+
94+ handle.await?
8495}
8596```
8697
@@ -117,8 +128,8 @@ $ curl -v http://localhost:8080/
117128
118129See also:
119130
120- - [Static resources folder with index.html example](https://github.com/kilork/actix-web-static-files-examples/tree/v4.0 /resource-dir)
121- - [Another example with same resources but using own defined function](https://github.com/kilork/actix-web-static-files-examples/tree/v4.0 /generate-resources-mapping)
131+ - [Static resources folder with index.html example](https://github.com/kilork/actix-web-static-files-examples/tree/v4.1 /resource-dir)
132+ - [Another example with same resources but using own defined function](https://github.com/kilork/actix-web-static-files-examples/tree/v4.1 /generate-resources-mapping)
122133
123134
124135# ## <a name='usecase2'></a>Use-case 2: package.json - npm managed folder
@@ -159,7 +170,7 @@ Reference resources in your `HTML` (`static/index.html`):
159170
160171` ` ` html
161172< ! DOCTYPE html>
162- < html lang= " en " >
173+ < html>
163174< head>
164175 < meta charset=" utf-8" >
165176 < meta name=" viewport" content=" width=device-width, initial-scale=1, shrink-to-fit=no" >
@@ -195,21 +206,23 @@ npm install webpack webpack-cli html-webpack-plugin clean-webpack-plugin --save-
195206Add ` web/webpack.config.js` :
196207
197208` ` ` js
198- const path = require(' path' );
199- const { CleanWebpackPlugin } = require(' clean-webpack-plugin' );
200- const HtmlWebpackPlugin = require(' html-webpack-plugin' );
209+ const path = require(" path" );
210+ const { CleanWebpackPlugin } = require(" clean-webpack-plugin" );
211+ const HtmlWebpackPlugin = require(" html-webpack-plugin" );
201212
202213module.exports = {
203- entry: ' ./src/index.js' ,
214+ entry: " ./src/index.js" ,
204215 plugins: [
205216 new CleanWebpackPlugin (),
206217 new HtmlWebpackPlugin({
207- title: ' actix-web-static-files WebPack' ,
218+ title: " actix-web-static-files WebPack" ,
208219 }),
209220 ],
210221 output: {
211- filename: ' main.js' ,
212- path: path.resolve(__dirname, ' dist' , ' bundle' ),
222+ filename: " main.js" ,
223+ path: process.env.OUT_DIR
224+ ? path.resolve(process.env.OUT_DIR, " web" , " dist" , " bundle" )
225+ : path.resolve(__dirname, " dist" , " bundle" ),
213226 },
214227};
215228` ` `
@@ -257,6 +270,9 @@ Add `build.rs` with call to bundle resources:
257270use static_files::NpmBuild;
258271
259272fn main () -> std::io::Result< ()> {
273+ unsafe {
274+ std::env::set_var(" NODE_OPTIONS" , " --openssl-legacy-provider" );
275+ }
260276 NpmBuild::new(" web" )
261277 .install ()?
262278 .run(" build" )?
@@ -271,19 +287,25 @@ Include generated code in `src/main.rs`:
271287
272288` ` ` rust, ignore
273289use actix_web::{App, HttpServer};
274- use actix_web_static_files;
275290
276291include! (concat! (env! (" OUT_DIR" ), " /generated.rs" ));
277292
278293# [actix_web::main]
279294async fn main () -> std::io::Result< ()> {
280- HttpServer::new(move || {
295+ let listen = std::env::var(" LISTEN" ).unwrap_or_else(| _| " 127.0.0.1:8084" .into());
296+ let server = HttpServer::new(|| {
281297 let generated = generate ();
282298 App::new ().service(actix_web_static_files::ResourceFiles::new(" /" , generated))
283299 })
284- .bind(" 127.0.0.1:8080" )?
285- .run ()
286- .await
300+ .bind(listen)? ;
301+
302+ if let Some(addr) = server.addrs().first () {
303+ println! (" {:05}" , addr.port ());
304+ }
305+
306+ let handle = actix_web::rt::spawn(server.run ());
307+
308+ handle.await?
287309}
288310` ` `
289311
@@ -326,7 +348,7 @@ $ curl -v http://localhost:8080
326348
327349See also:
328350
329- - [WebPack Example](https://github.com/kilork/actix-web-static-files-examples/tree/v4.0 /webpack)
351+ - [WebPack Example](https://github.com/kilork/actix-web-static-files-examples/tree/v4.1 /webpack)
330352
331353# ## <a name='usecase4'></a>Use-case 4: yarn package manager
332354
@@ -349,7 +371,7 @@ fn main() -> std::io::Result<()> {
349371
350372See also:
351373
352- - [Yarn WebPack Example](https://github.com/kilork/actix-web-static-files-examples/tree/v4.0 /yarn-webpack)
374+ - [Yarn WebPack Example](https://github.com/kilork/actix-web-static-files-examples/tree/v4.1 /yarn-webpack)
353375
354376# ## <a name='usecase5'></a>Use-case 5: Angular-like applications
355377
@@ -386,4 +408,4 @@ async fn main() -> std::io::Result<()> {
386408
387409Remember to place you static resource route after all other routes in this case.
388410
389- You can check the complete example [Angular Router Sample](https://github.com/kilork/actix-web-static-files-example-angular-router/tree/v4.0 ).
411+ You can check the complete example [Angular Router Sample](https://github.com/kilork/actix-web-static-files-example-angular-router/tree/v4.1 ).
0 commit comments