When I open the url, it show the raw text like this:
But the html is look like this:
What should I do to render the html file like the second potograph?
Actix is serving only your form, not your scripts and style sheets. You have to serve those as well (probably the whole contents of the codemirror directory). I'd recommend using the actix-files crate for serving static files and directories. I think adding a Files service that serves your codemirror directory should do the trick:
use actix_files::Files;
App::new()
.service(web::resource("/form").route(web::get().to(show_html)))
.service(Files::new("/codemirror", "./codemirror"))
(I'd also use actix-files to serve your html form as well, see i.e. NamedFile or this section of the actix documentation)
You are literally putting the displayed text inside your textarea. The textarea will display whatever you put inside. I have a feeling this has nothing to do with Actix or style sheets.
No worries. I think these are reversed. The first argument is called mount_path and the second is called serve_from. According to the docs:
The first argument (mount_path) is the root URL at which the static files are served. For example, /assets will serve files at example.com/assets/....
The second argument (serve_from) is the location on disk at which files are loaded. This can be a relative path. For example, ./ would serve files from the current working directory.
Try reversing the order of your arguments to File::new("./t", "../templates") and see if it works?
As far as I understood OPs problem, these stylesheets are not loaded when the form (which works fine I assume from the screenshot) is loaded with actix. When the form is opened via file://path/to/form, the style is loaded correctly, resulting in the styled version of the same form that can be seen in the second screenshot.
Hmm, could you try removing the leading dot from the path? Convert the path from the relative ./t to the absolute /t? I'm unsure whether actix serves a relative mounting point as expected or whether it will be served at localhost:8081/./t. Also, what do you see when you open localhost:8081/t in your browser? You should see the contents of your templates directory.
P.S.: Just a sanity check, but you are running your web server from the root directory of your my_crate crate, right? Not something like cargo run --manifest-path my_crate/Cargo.toml from the directory with my_crate and templates in it?
As the screenshot shows, the first noting place must be ./templates, but the second noting place must be ../templates. It seems that async fn main runs in ./my_crate, but async fn show_html runs in ./my_crate/src.
After I changed to that, everything works fine.
Well, no, all your code runs in the same working directory. The include_str! macro works differently. include_str! knows nothing about your working directory, as it is executed at compile time, including the file as an UTF-8 string. It therefore includes a file relative to the file where include_str! is called:
The file is located relative to the current file (similarly to how modules are found).
Since you call include_str! from src/main.rs it will look for the file to include relative from the src/ directory. Unlike Actix which does look up your files at run time relative to your working directory.