Today we'll be writing a working web API using an extremely simple HTTP library controlable from the console of your computer. Let gonna se the implementation
First, crate a new folder and add k_board & snowboard.
cargo new http-api && cd http-api && cargo add k_board && cargo add snowboard
use k_board::{Keyboard, Keys}; use snowboard::{response, Server}; fn main() { for key in Keyboard::new() { match key { Keys::Enter => router("localhost:3000"), Keys::Letter('q') => break, _ => {} } } } fn router(localhost: &str) { println!("Running in localhost:3000 - `curl localhost:3000`"); Server::new(localhost).run(|req| { println!("Request: {:?}", req); response!(ok, "You are in localhost 3000!\n") }); }
If you press enter you active the server listening all call, then when you make a curl localhost:300
you will receive the answer of the server.
mateo@debian:~/http-api$ curl localhost:3000 You are in localhost 3000!
That a simple example in which it just take the enter key as an activator of the server, if your looking for an specific handling of the keys for server activation , you can look at Keys struct of k_board.
pub enum Keys { Up, Down, Left, Right, Enter, Space, Delete, Escape, Plus, Minus, Equal, Power, ^ Slash, / Backslash, \ Asterisk, * Point, Comma, Hashtag, Pipe, | Percent, % Ampersand, Currency, $ TwoPoints, : Semicolon, ; OpenSquareBracket, [ CloseSquareBracket, ] OpenCurlyBrace, { CloseCurlyBrace, } OpenQuestionMark, ¿ CloseQuestionMark, ? OpenParenthesis, ( CloseParenthesis, ) LessThan, < GreaterThan, > Apostrophe, ' At, @ Home, End, Tab, Backtab, Insert, Letter(char), Number(u8), F(u8), Ctrl(char), Alt(char), Null, }
Top comments (0)