Rust is a programming language known as high performance, memory security without garbage collectors, and its ability to build fast and efficient applications. One of the reasons Rust is increasingly popular is the ease of use to build web applications with various available frameworks, such as ACTIX Web and Rocket.
Rust also has a cargo, build manager and a great package that makes manage dependence and compile easy applications. By using crates.io, Rust has a broad library ecosystem (crates), allowing us to build complex applications with a minimum code.
In this tutorial, we will create a simple web application, compile it, and then use it using Nginx as a inverted proxy.
Install dependencies and rust
Update and install dependencies:
sudo apt update sudo apt install build -isntial -y
|
sudo appropriate renew sudo appropriate install build–important –Y |
Install Rust:
Curl -ssf | SH
|
curly https://sh.Rustup.RS -SSF | SH |
Verification of rust and cargo installation:
Rustup -Rustc Version -Version Cargo -Version
|
Rustup –version Rustc –version load –version |
The resulting response:
RUSTC 1.82.0 (F6E511EEC 2024-10-15) Cargo 1.82.0 (8F40FC59F 2024-08-21)
|
Rustc 1.82.0 (F6E511EEC 2024–10–15) load 1.82.0 (8F40FC59F 2024–08–21) |
Make a rust app
Create a directory for the rust project:
new cargo cd cd cd rustweb
|
load new Rustweb CD Rustweb |
Open SRC/Main.RS submit:
Change content with the following code:
Use std :: io :: prelude ::*; Use std :: net :: {tclistener, tcSptream}; fn handle_connection (mut stream: tcSptream) {let mut buffer = [0; 512]; stream.read (& mut buffer) .unwrap (); Let the response = “http/1.1 200 ok \ r \ ncontent-tipe: text/html; charset = utf-8 \ r \ n‘ r \ n
Hello, World! “; stream.write (response.as_bytes ()). unwrap (); stream.flush (). unwrap ();} fn play () {// bind server to localhost: 8000 let list = tclistener :: Bind (” 127.0.0.1:8000 “). Unwrap (); println! listener.incoming () {let stream = stream.unwrap ();|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
use STD::IO::Prelude::*; use STD::clean::{TCPLPLATIS, TcSptream}; FN handle_connection(Mut small river: TcSptream) { let Mut buffer = [0; 512]; small river.read(&Mut buffer).open(); let response = “Http/1.1 200 ok \ r \ ncontent-type: text/html; charset = utf-8 \ r \ n \ r \ n \ n Hello, World! “; small river.write(response.as_bytes()).open(); small river.water().open(); } FN main() { // bind server to localhost: 8000 let listener = TCPLPLATIS::tie(“127.0.0.1:8000”).open(); PrintlnLai(“The server runs at http://127.0.0.1:8000”); For small river in the listener.enter() { let small river = small river.open(); handle_connection(small river); } } |
Application Compilation:
Binary applications will be placed on Target/release/Rustweb.
Make services
Creating SystemD Service Files:
SUDO NANO/ETC/SYSYSTEMD/SYSYSTEM/RUSTWEB.Service
|
sudo Nano /etc./SystemD/system/Rustweb.serve |
File:
Description = rust web application after = network.target
[Service]
User = youughuser group = Youurizer workingdirectory =/home/youghoser/rustweb execstart =/home/youghoser/rustweb/target/release/rustweb restart = always
[Install]
WANTEDBY = MULTI-USER. TARGET
|
[Unit] Information=Rust Web Application After=network.target [Service] User=Youurizer Group=Youurizer WorkingDirectory=/House/Youurizer/Rustweb Execstart=/House/Youurizer/Rustweb/target/release/Rustweb Restart=always [Install] Searching for=multi–user.target |
Activate Services:
Sudo SystemCTL Daemon-Reload Sudo Systemctl Activating Rustweb Sudo SystemCtl Starting Rustweb Sudo SystemCtl Status Rustweb
|
sudo SystemCTL Daemon–Re -load sudo SystemCTL possible Rustweb sudo SystemCTL beginning Rustweb sudo SystemCTL status Rustweb |
Nginx configuration
Install Nginx:
sudo apt install nginx -y
|
sudo appropriate install Nginx –Y |
Create Nginx Configuration Files:
SUDO NANO/ETC/NGINX/CONF.D/RUSTWEB.CONF
|
sudo Nano /etc./Nginx/conf.D/Rustweb.conf |
File:
server {listen 80; server_name Example.com; Location / {proxy_pass proxy_set_header host $ host; proxy_set_header x-real-ip $ remote_addr; proxy_set_header x-forewarded-for $ proxy_add_x_forwarded_for; }}
|
server { listen 80; server_name example.com; Location / { Proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X–Real–I P $Remote_addr; proxy_set_header X–Continued–For $proxy_add_x_forwarded_for; } } |
Restart nginx:
Sudo systemctl restart nginx
|
sudo SystemCTL Restart Nginx |
Application test
Open your browser and visit the address:
The result is the text “Hello, World!”.
Game Center
Game News
Review Film
Rumus Matematika
Anime Batch
Berita Terkini
Berita Terkini
Berita Terkini
Berita Terkini
review anime
Comments are closed, but trackbacks and pingbacks are open.