Rust Web API: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Introduction= This page is to point me in the right direction should someone want to do this in rust. I think that Golang, C# or NodeJS (to reduce technology) might be a better choice. This language seems a lot harder to learn. Maybe if you already use it for other stuff in you company. Doing this I was surprise how easy it was. Maybe microcontrollers have clouded my judgement" |
|||
Line 1: | Line 1: | ||
=Introduction= | =Introduction= | ||
This page is to point me in the right direction should someone want to do this in rust. I think that Golang, C# or NodeJS (to reduce technology) might be a better choice. This language seems a lot harder to learn. Maybe if you already use it for other stuff in you company. Doing this I was surprise how easy it was. Maybe microcontrollers have clouded my judgement | This page is to point me in the right direction should someone want to do this in rust. I think that Golang, C# or NodeJS (to reduce technology) might be a better choice. This language seems a lot harder to learn. Maybe if you already use it for other stuff in you company. Doing this I was surprise how easy it was. Maybe microcontrollers have clouded my judgement | ||
=actix_web= | |||
==Server== | |||
I am sure there are many choices knowing rust but this is the first one I came across. May rewrite this as we go but here we are | |||
<syntaxhighlight lang="rs"> | |||
HttpServer::new(move || { | |||
App::new() | |||
.app_data(repository::AppState::new(db.clone())) | |||
.wrap(Logger::default()) | |||
.configure(routes::home_routes::config) // Better Approach | |||
.service(hello) // Sample route 1 | |||
.service(echo) // Sample route 2 | |||
.route("/hey", web::get().to(manual_hello)) | |||
}) | |||
.bind((address, port))? | |||
.run() | |||
.await | |||
</syntaxhighlight> | |||
==Routes== | |||
Here is the example for the routes. We will need to look at middleware no doubt | |||
<syntaxhighlight lang="rs"> | |||
pub fn config(config: &mut web::ServiceConfig) { | |||
config.service( | |||
web::scope("/home") | |||
.service(handlers::home_handlers::greet) | |||
.service(handlers::home_handlers::test), | |||
); | |||
} | |||
</syntaxhighlight> | |||
==Handlers== | |||
And here are the handlers which I will hopefully update to have your 401, 404 etc | |||
<syntaxhighlight lang="rs"> | |||
use actix_web::{get, web, Responder}; | |||
#[get("/greet/{name}")] | |||
pub async fn greet(name: web::Path<String>) -> impl Responder { | |||
format!("Hello {name}!",) | |||
} | |||
#[get("/test")] | |||
pub async fn test() -> impl Responder { | |||
format!("Hello world!") | |||
} | |||
</syntaxhighlight> |
Revision as of 21:35, 9 January 2025
Introduction
This page is to point me in the right direction should someone want to do this in rust. I think that Golang, C# or NodeJS (to reduce technology) might be a better choice. This language seems a lot harder to learn. Maybe if you already use it for other stuff in you company. Doing this I was surprise how easy it was. Maybe microcontrollers have clouded my judgement
actix_web
Server
I am sure there are many choices knowing rust but this is the first one I came across. May rewrite this as we go but here we are
HttpServer::new(move || {
App::new()
.app_data(repository::AppState::new(db.clone()))
.wrap(Logger::default())
.configure(routes::home_routes::config) // Better Approach
.service(hello) // Sample route 1
.service(echo) // Sample route 2
.route("/hey", web::get().to(manual_hello))
})
.bind((address, port))?
.run()
.await
Routes
Here is the example for the routes. We will need to look at middleware no doubt
pub fn config(config: &mut web::ServiceConfig) {
config.service(
web::scope("/home")
.service(handlers::home_handlers::greet)
.service(handlers::home_handlers::test),
);
}
Handlers
And here are the handlers which I will hopefully update to have your 401, 404 etc
use actix_web::{get, web, Responder};
#[get("/greet/{name}")]
pub async fn greet(name: web::Path<String>) -> impl Responder {
format!("Hello {name}!",)
}
#[get("/test")]
pub async fn test() -> impl Responder {
format!("Hello world!")
}