Code Monkey home page Code Monkey logo

argos's Introduction

Argos

Argos makes it easy to create a stand-alone web application backend server.

Usage

Simple Example

Step 1: Define your API function

You can define your api function with a macro-attribute:

#[route(GET, path = "/api/hello", formatter = "text")]
pub fn hello(req: HttpRequest) -> Result<String, ReturnError<String>> {
    let url_params = req.url_params();
    let name = url_params.get("name");
    match name {
        Some(name) => Ok(format!("hello {}!", name)),
        None => Err(
            ReturnError::new(
                400,
                "name is required".to_string(),
            )
        ),
    }
}

this function define a http interface with GET method, and url path is /api/hello.

Step 2: Start a server

You can start your server like this:

#[tokio::main]
async fn main() {
    let server = Server::builder(([127, 0, 0, 1], 3000).into())
        .build()
        .await
        .unwrap();
    server.start().await.unwrap();
}

Your server will bind in 127.0.0.1:3000, so you can send a http request to call this API:

curl http://127.0.0.1:3000/api/hello?name=liudao

Filter

You can define a filter to filt the request:

#[filter(path_pattern="/api/hello.*", order=1)]
pub fn path_filter(mut req: HttpRequest) -> Chain {
    let attr = req.attributes_mut();
    attr.insert("kk".to_string(), "value".to_string());
    // let _method = req.method_mut();
    if req.headers().contains_key("token") {
        Chain::Continune(req)
    } else {
        let mut return_err = ReturnError::new(
            401, 
            "not authorized".to_string(),
        );
        return_err.headers.append("filter", HeaderValue::from_str("rejected").unwrap());
        Chain::Reject(return_err)
    }
    
}

Filter now only support path_pattern, using regexp. The attribute order represents the priority of the filter, the smaller the number, the higher the priority.

See more examples in core/examples.

License

Argos is provided under the MIT license. See LICENSE.

argos's People

Contributors

jimmyseraph avatar

Stargazers

 avatar Tommy Skinner avatar  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.