mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
standardize binary names
This commit is contained in:
10
basics/static-files/Cargo.toml
Normal file
10
basics/static-files/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "static_index"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.0.0-rc.3"
|
||||
actix-files = "0.6.0-beta.15"
|
||||
env_logger = "0.9.0"
|
||||
log = "0.4"
|
20
basics/static-files/README.md
Normal file
20
basics/static-files/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# static_index
|
||||
|
||||
Demonstrates how to serve static files. Inside the `./static` folder you will find 2 subfolders:
|
||||
|
||||
* `root`: A tree of files that will be served at the web root `/`. This includes the `css` and `js` folders, each
|
||||
containing an example file.
|
||||
* `images`: A list of images that will be served at `/images` path, with file listing enabled.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
$ cd basics/static_index
|
||||
$ cargo run
|
||||
```
|
||||
|
||||
### Available Routes
|
||||
|
||||
- [GET /](http://localhost:8080/)
|
||||
- [GET /images](http://localhost:8080/images)
|
||||
|
25
basics/static-files/src/main.rs
Normal file
25
basics/static-files/src/main.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use actix_files::Files;
|
||||
use actix_web::{middleware::Logger, App, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// We allow the visitor to see an index of the images at `/images`.
|
||||
.service(Files::new("/images", "static/images/").show_files_listing())
|
||||
// Serve a tree of static files at the web root and specify the index file.
|
||||
// Note that the root path should always be defined as the last item. The paths are
|
||||
// resolved in the order they are defined. If this would be placed before the `/images`
|
||||
// path then the service for the static images would never be reached.
|
||||
.service(Files::new("/", "./static/root/").index_file("index.html"))
|
||||
// Enable the logger.
|
||||
.wrap(Logger::default())
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
BIN
basics/static-files/static/images/logo.png
Normal file
BIN
basics/static-files/static/images/logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.7 KiB |
11
basics/static-files/static/root/css/example.css
Normal file
11
basics/static-files/static/root/css/example.css
Normal file
@ -0,0 +1,11 @@
|
||||
body {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-family: sans-serif;
|
||||
}
|
||||
|
||||
img {
|
||||
transition: .5s ease-in-out;
|
||||
}
|
BIN
basics/static-files/static/root/favicon.ico
Normal file
BIN
basics/static-files/static/root/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
13
basics/static-files/static/root/index.html
Normal file
13
basics/static-files/static/root/index.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8" />
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="css/example.css" />
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<script src="/js/example.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Actix-web</h1>
|
||||
<img src="/images/logo.png" alt="Actix logo" />
|
||||
</body>
|
||||
</html>
|
7
basics/static-files/static/root/js/example.js
Normal file
7
basics/static-files/static/root/js/example.js
Normal file
@ -0,0 +1,7 @@
|
||||
jQuery(document).ready(function () {
|
||||
let rotation = 0;
|
||||
jQuery("img").click(function () {
|
||||
rotation += 360;
|
||||
jQuery("img").css({'transform': 'rotate(' + rotation + 'deg)'});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user