Skip to content

Instantly share code, notes, and snippets.

@o0Ignition0o
Last active April 24, 2018 09:53
Show Gist options
  • Save o0Ignition0o/e10c64a6ffaf97647a066730775e92a4 to your computer and use it in GitHub Desktop.
Save o0Ignition0o/e10c64a6ffaf97647a066730775e92a4 to your computer and use it in GitHub Desktop.
Add a vector of actix routes
extern crate actix;
extern crate actix_web;
extern crate env_logger;
use actix_web::{server, App, HttpRequest};
struct Route {
path: String,
action: fn(HttpRequest) -> String,
}
fn add_resource(app: App, resource: Route) -> App {
let Route { path, action } = resource;
app.resource(&path, move |r| r.f(action))
}
fn index(_req: HttpRequest) -> String {
String::from("hello world")
}
fn status(_req: HttpRequest) -> String {
String::from("This is the status page")
}
fn main() {
if ::std::env::var("RUST_LOG").is_err() {
::std::env::set_var("RUST_LOG", "actix_web=info");
}
env_logger::init();
server::new(move || {
let mut app = App::new();
let resources = vec![
Route {
path: "/hello".into(),
action: index,
},
Route {
path: "/status".into(),
action: status,
},
];
for route in resources {
app = add_resource(app, route);
}
app
}).bind("127.0.0.1:8088")
.unwrap()
.run();
println!("Started http server: http://127.0.0.1:8443");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment