Git Product home page Git Product logo

tela's Introduction

Tela

Version
Maintained Tests

Rust based web design ๐Ÿ˜„

Construct endpoints or error handlers like so.

use tela::prelude::*;
// This is a text/plain response
#[get("/")]
fn home() -> &'static str {
  "Hello, world!"
}
use tela::{prelude::*, response::HTML};
// This is a text/html response that could fail and the error should be either
// given to the appropriate handler or returned as is.
#[post("/login/:username/:age")]
fn data(username: String, age: i32) -> Result<HTML<String>> {
  response!(html!(<h1>"Hello, world!"</h1>))
}
use tela::{prelude::*, response::HTML};
// Catches any error that is 404 comming from another endpoint
// soon this will be for all 404 errors that are thrown
// All returns must be valid data. There can not be custom HTTP codes or results
// returned.
#[catch(404)]
fn not_found(code: u16, message: String, reason: String) -> HTML<String> {
  html!(<h1>{code}" "{message}</h1>)
}
use tela::{prelude::*, response::{JSON, Raw}};
// Endpoint that returns json with a custom HTTP code. This response is not
// caught by any other handlers.
// The `Raw` type can be used inside of a JSON type to represent a shapeless object.
#[get("/get-data")]
fn get_data() -> (u16, JSON<Raw>) {
  (203, JSON(json!({"name": "Tela"}))
}
use tela::{prelude::*, response::{JSON, Raw}, request::{Body, Query}};
use serde::{Serialize, Deserialize};

#[derive(Default, Serialize, Deserialize)]
struct User {
  name: String,
}

// The query and body can automatically be extracted from the request in the parameters.
// Just use `Body` and `Query`. If a extraction or a uri capture could be missing or you don't want
// Tela throwing a 500 error automatically, you can wrap the parameters type in an `Option`.
// Also note that the order of the parameters are not important.
#[get("/api/user/:username")]
fn get_user(query: Option<Query<User>>, username: String, Body(body): Body<i32>) -> Result<JSON<Raw>> {
  let username = match query {
    Some(User{name}) => name,
    None => String::new()
  };

  JSON(json!({"name": username, "age": body}))
}

Run an app like so.

use tela::{prelude::*, Server};

#[tela::main]
asyn fn main() {
  Server::new()
      .routes(group![home, data])
      .catch(not_found)
      .serve(3000)
      .await
}

TODO:

  • Built it timeout, throtteling, etc... with Tower
  • HTTP/1 and HTTP/2 Support (Currently only HTTP/1)

Inspiration

Tools


Made with rust Built with love

tela's People

Contributors

tired-fox 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.