|
| 1 | +extern crate civet; |
| 2 | +extern crate conduit; |
| 3 | +extern crate conduit_router; |
| 4 | + |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::io::{self, Cursor}; |
| 7 | +use std::sync::mpsc::channel; |
| 8 | + |
| 9 | +use civet::{Config, response, Server}; |
| 10 | +use conduit::{Request, Response}; |
| 11 | +use conduit_router::{RouteBuilder, RequestParams}; |
| 12 | + |
| 13 | + |
| 14 | +fn name(req: &mut Request) -> io::Result<Response> { |
| 15 | + let name = req.params().find("name").unwrap(); |
| 16 | + let bytes = format!("Hello {}!", name).into_bytes(); |
| 17 | + |
| 18 | + Ok(response(200, HashMap::new(), Cursor::new(bytes))) |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +fn hello(_req: &mut Request) -> io::Result<Response> { |
| 23 | + let hello = "Hello world!".to_string().into_bytes(); |
| 24 | + Ok(response(200, HashMap::new(), Cursor::new(hello))) |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +fn main() { |
| 29 | + let mut router = RouteBuilder::new(); |
| 30 | + |
| 31 | + router.get("/", hello); |
| 32 | + router.get("/:name", name); |
| 33 | + |
| 34 | + let _server = Server::start(Config { port: 8888, threads: 1 }, router); |
| 35 | + |
| 36 | + // Preventing process exit. |
| 37 | + let (_tx, rx) = channel::<()>(); |
| 38 | + rx.recv().unwrap(); |
| 39 | +} |
0 commit comments