Giter Site home page Giter Site logo

larp's Introduction

Larp

A helper for warp. Provide some macros to make warp easier.

Warning

Larp is far from stable. BE CAREFUL if want to use it on your product.

Install

[dependencies]
warp = "0.1.9"
larp = "0.0.1"

# optional
serde = "1.0.80"
serde_derive = "1.0.80"

Example

#[macro_use]
extern crate warp;

#[macro_use]
extern crate larp;

use warp::Filter;

fn main() {
  let route = get!().map(|| "hello, world");
  warp::serve(route).run(([0; 4], 3000))
}

Now you can see "hello, world" at http://localhost:3000.

API

get!

Create a route only responding to get request.

#[macro_use]
extern crate warp;

#[macro_use]
extern crate larp;

use warp::Filter;

fn main() {
  // handle / and response "hello, world"
  let hello_world = get!().map(|| "hello, world");
  
  // handle /hello/:name and response "hello, $name"
  let hello_name = get!("hello" / String)
    .map(|name| format!("hello, {}", name));
    
  // combine some routes
  let route = route!(/, hello_world, hello_name);
  
  warp::serve(route).run(([0; 4], 3000))
}

post!

Create a route only responding to post request. The Content-Type of request must be application/json and it will be parsed automatically.

#[macro_use]
extern crate warp;

#[macro_use]
extern crate larp;

// for serializing `Person` struct, you should import `serde` and `serde_derive`
#[macro_use]
extern crate serde_derive;
extern crate serde;

use warp::Filter;

#[derive(Debug, Serialize, Deserialize)]
struct Person {
  pub name: String,
}

fn main() {
  // handle / and response "hello, world"
  let hello_world = post!().map(|| "hello, world");
  
  // handle /person
  // request body must be `{ name: 'someone' }`
  // response body is `{ name: 'someone' }` too
  let hello_person = post!("person")
    .map(|p: Person| warp::reply::json(&p));
    
  // combine some routes
  let route = route!(/, hello_world, hello_person);
  
  warp::serve(route).run(([0; 4], 3000))
}

put!

Create a route only responding to put request. Same as post!.

del!

Create a route only responding to delete request. Same as get!.

route!

Make a subroute and combine some routes.

// ...

fn main() {
  let hello_world = get!().map(|| "hello, world");
  
  let hello_name = get!("hello" / String)
      .map(|name| format!("hello, {}", name));
      
  let hello_post_world = post!().map(|| "hello, world");
  
  let hello_person = post!("person")
      .map(|p: Person| warp::reply::json(&p));
      
  // handle /get and /get/hello
  let get = route!("get", hello_world, hello_name);
  
  // handle /post and /post/person
  let post = route!("post", hello_post_world, hello_person);
  
  let route = route!(/, get, post);
  warp::serve(route).run(([0; 4], 3000))
}

larp's People

Stargazers

 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.