Code Monkey home page Code Monkey logo

rust-canteen's Introduction

Canteen

Build Status Latest Version

Description

Canteen is the first project that I'm implementing in Rust. It's a clone of Flask, my very favorite Python web framework. There is code for an example implementation in the canteen-impl repository.

Usage

It's by no means complete, but I'm working on it, and it's now available on crates.io! To install and check it out, add the following to your Cargo.toml:

[dependencies]
canteen = "0.5"

The principle behind Canteen is simple -- handler functions are defined as simple Rust functions that take a Request and return a Response. Handlers are then attached to one or more routes and HTTP methods/verbs. Routes are specified using a simple syntax that lets you define variables within them; variables that can then be extracted to perform various operations. Currently, the following variable types can be used:

  • <str:name> will match anything inside a path segment, returns a String
  • <int:name> will return a signed integer (i32) from a path segment
    • ex: cnt.add_route("/api/foo/<int:foo_id>", &[Method::Get], my_handler) will match "/api/foo/123" but not "/api/foo/123.34" or "/api/foo/bar"
  • <uint:name> will return an unsigned integer (u32)
  • <float:name> does the same thing as the int parameter definition, but matches numbers with decimal points and returns an f32
  • <path:name> will greedily take all path data contained, returns a String
    • ex: cnt.add_route("/static/<path:name>", &[Method::Get], utils::static_file) will serve anything in the /static/ directory as a file

After the handlers are attached to routes, the next step is to simply start the server. Any time a request is received, it is dispatched with the associated handler to a threadpool worker. The worker notifies the parent process when it's finished, and then the response is transmitted back to the client. Pretty straightforward stuff!

Example

extern crate canteen;

use canteen::*;
use canteen::utils;

fn hello_handler(req: &Request) -> Response {
    let mut res = Response::new();

    res.set_status(200);
    res.set_content_type("text/plain");
    res.append("Hello, world!");

    res
}

fn double_handler(req: &Request) -> Response {
    let to_dbl: i32 = req.get("to_dbl");

    /* simpler response generation syntax */
    utils::make_response(format!("{}", to_dbl * 2), "text/plain", 200)
}

fn main() {
    let cnt = Canteen::new();

    // bind to the listening address
    cnt.bind(("127.0.0.1", 8080));

    // set the default route handler to show a 404 message
    cnt.set_default(utils::err_404);

    // respond to requests to / with "Hello, world!"
    cnt.add_route("/", &[Method::Get], hello_handler);

    // pull a variable from the path and do something with it
    cnt.add_route("/double/<int:to_dbl>", &[Method::Get], double_handler);

    // serve raw files from the /static/ directory
    cnt.add_route("/static/<path:path>", &[Method::Get], utils::static_file);

    cnt.run();
}

rust-canteen's People

Contributors

anharhussainmiah avatar jeffdn avatar tdryer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

fdenzer liamdgray

rust-canteen's Issues

Hello world example in README not compiling

Hi Jeff,

It seems like you have updated canteen to a new minor version (0.4.1 according to my Cargo.lock file) and now the example "Hello world" code in the README does not work.

``error[E0061]: this function takes 1 parameter but 0 parameters were supplied
--> src/main.rs:24:15
|
24 | let cnt = Canteen::new();
| ^^^^^^^^^^^^^^ expected 1 parameter

error[E0599]: no method named bind found for type canteen::Canteen in the current scope
--> src/main.rs:27:9
|
27 | cnt.bind(("127.0.0.1", 8080));
| ^^^^

error: aborting due to 2 previous errors``

My guess is that you've moved the url and the port configuration to the Canteen::new method and removed the bind method.

I can open a small PR just to fix it. Let me know.

Cheers from Brazil :)

PS: Love the idea for Canteen, I'm a big fan of Flask as well. If you would like to take this project forward, and need some help, let me know.

PS 2: Found you on Are We Web Yet?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.