mirror of
https://github.com/Start9Labs/documentation.git
synced 2026-03-26 18:31:53 +00:00
* re-arrange the user docs * Minor description edit * update styles * fix service links Co-authored-by: kn0wmad <kn0wmad@protonmail.com> Co-authored-by: Lucy Cifferello <12953208+elvece@users.noreply.github.com>
72 lines
1.6 KiB
ReStructuredText
72 lines
1.6 KiB
ReStructuredText
.. _packaging-create-service:
|
|
|
|
=======================
|
|
Step 2 - Create Service
|
|
=======================
|
|
|
|
For this example, we are going to create a simple Rust project that serves a static web page.
|
|
|
|
.. code:: bash
|
|
|
|
mkdir hello-world
|
|
cargo init
|
|
touch src/index.html
|
|
|
|
In ``index.html`` add:
|
|
|
|
.. code:: html
|
|
|
|
<html>
|
|
<head>
|
|
<title>Hello World!</title>
|
|
</head>
|
|
<body>
|
|
<h1>Hello World!</h1>
|
|
</body>
|
|
</html>
|
|
|
|
In ``main.rs`` add:
|
|
|
|
.. code:: rust
|
|
|
|
use hyper::service::{make_service_fn, service_fn};
|
|
use hyper::{body::Bytes, Body, Request, Response, Server};
|
|
use std::convert::Infallible;
|
|
use std::net::SocketAddr;
|
|
|
|
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
|
Ok(Response::new(Body::from(Bytes::from_static(
|
|
include_bytes!("index.html"),
|
|
))))
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// Construct our SocketAddr to listen on...
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 80));
|
|
|
|
// And a MakeService to handle each connection...
|
|
let make_service = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });
|
|
|
|
// Then bind and serve...
|
|
let server = Server::bind(&addr).serve(make_service);
|
|
|
|
// And run forever...
|
|
if let Err(e) = server.await {
|
|
eprintln!("server error: {}", e);
|
|
}
|
|
}
|
|
|
|
|
|
**That's it!** We now have the code for our service.
|
|
|
|
Let's build and run it!
|
|
|
|
.. code:: bash
|
|
|
|
# build the project
|
|
cargo build
|
|
# start the executable
|
|
target/debug/hello-world
|
|
|
|
Visit `<http://localhost:80>`_ to see your running web page! |