GithubHelp home page GithubHelp logo

tempbottle / rustful Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ogeon/rustful

0.0 1.0 0.0 3.25 MB

A light web micro-framework for Rust

Home Page: http://ogeon.github.io/docs/rustful/master/rustful/index.html

License: MIT License

Rust 99.19% HTML 0.37% CSS 0.27% Shell 0.17%

rustful's Introduction

Rustful

Join the chat at https://gitter.im/Ogeon/rustful

Build Status Windows Build status

A light web micro-framework for Rust with REST-like features. The main purpose of Rustful is to create a simple, modular and non-intrusive foundation for HTTP applications. It has a mainly stateless structure, which naturally allows it to run both as one single server and as multiple instances in a cluster.

Some of the features are:

  • Generic response handlers. Just use a function or implement the Handler trait.
  • Unified log system to make sure everything is working together.
  • Some handy macros reduces the risk for typos and makes life easier.
  • Pluggable request and response filtering.
  • Variables and recursive wildcards in routes.

Online documentation.

#Getting Started

##Cargo.toml Entries

Add the following lines to your Cargo.toml file:

[dependencies]
rustful = "0.3"

###Cargo Features

Some parts of Rustful can be toggled using Cargo features:

  • rustc_json_body - Parse the request body as JSON. Enabled by default.
  • ssl - Enable SSL, and thereby HTTPS. Enabled by default.
  • multipart - Enable parsing of multipart/form-data requests. Enabled by default.

###Using SSL Note that the ssl feature requires OpenSSL to be installed in one way or another. See https://github.com/sfackler/rust-openssl#building for more instructions.

##Write Your Server Here is a simple example of what a simple project could look like. Visit http://localhost:8080 or http://localhost:8080/Olivia (if your name is Olivia) to try it.

//Include macros to be able to use `insert_routes!`.
#[macro_use]
extern crate rustful;

use std::error::Error;

use rustful::{Server, Context, Response, TreeRouter, Handler};

fn say_hello(context: Context, response: Response) {
    //Get the value of the path variable `:person`, from below.
    let person = match context.variables.get("person") {
        Some(name) => &name[..],
        None => "stranger"
    };

    //Use the name from the path variable to say hello.
    response.send(format!("Hello, {}!", person));
}

//Dodge an ICE, related to functions as handlers.
struct HandlerFn(fn(Context, Response));

impl Handler for HandlerFn {
    fn handle_request(&self, context: Context, response: Response) {
        self.0(context, response);
    }
}

fn main() {
    //Build and run the server.
    let server_result = Server {
        //Turn a port number into an IPV4 host address (0.0.0.0:8080 in this case).
        host: 8080.into(),

        //Create a TreeRouter and fill it with handlers.
        handlers: insert_routes!{
            TreeRouter::new() => {
                //Handle requests for root...
                Get: HandlerFn(say_hello),

                //...and one level below.
                //`:person` is a path variable and it will be accessible in the handler.
                ":person" => Get: HandlerFn(say_hello)
            }
        },

        //Use default values for everything else.
        ..Server::default()
    }.run();

    match server_result {
        Ok(_server) => {},
        Err(e) => println!("could not start server: {}", e.description())
    }
}

##Contributing

Contributions are always welcome, even if it's a small typo fix (or maybe I should say "especially typo fixes"). You can fork the project and open a pull request with your changes, or create an issue if you just want to report or request something. Are you not sure about how to implement your change? Is it still a work in progress? Don't worry. You can still open a pull request where we can discuss it and do it step by step.

New features are as welcome as fixes, so pull requests and proposals with enhancements are very much appreciated, but please explain your feature and give a good motivation to why it should be included. It makes things much easier, both for reviewing the feature and for those who are not as familiar with how things work. You can always open an issue where we can discuss the feature and see if it should be included. Asking is better than assuming!

###Testing

Rustful is tested on Linux, using Travis, and on Windows, using AppVeyor and a pull request will not be approved unless it passes these tests. It is therefore a good idea to run tests locally, before pushing your changes, so here is a small list of useful commands:

  • cargo test - Basic unit, documentation and compile tests.
  • cargo build --no-default-features - Check if the most minimal version of Rustful builds.
  • cargo build --no-default-features --features "feature1 feature2" - Check if Rustful with only feature1 and feature2 enabled builds.
  • cargo run --example example_name - check if the example example_name behaves as expected (see the example directory).

rustful's People

Contributors

armed avatar gitter-badger avatar homu avatar mkovalchik avatar ogeon avatar wasi avatar

Watchers

 avatar

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.