Giter Site home page Giter Site logo

vial's Introduction

Drink Me.

~ vial: a micro micro-framework ~

Vial is a small web "framework" for making small web sites in Rust.

It includes just a few basics:

  • Parsing and routing HTTP requests
  • Parsing POST form data
  • Serving static files (css, js)

Everything else... well, that's up to you.

The goal is an as-few-as-possible-dependencies web library you can use to test out an idea quickly or get a personal project rolling. Single file, server side apps? You bet! Fast compilation? Yes please! ร€ la carte dependencies? Now you're talkin'!

It's sort of like a picnic where the playlist is all 90s music and you have to bring your own beverages. And food.

To learn more, keep reading or visit one of these links:


โš  Status: Vial is still in development and shouldn't be used to build real world apps on untrusted networks. Please proceed with caution and wear a hard hat at all times.


~ getting started ~

To get started, just add vial to your Cargo.toml:

[dependencies]
vial = "0.1"

Now you can use vial::prelude::*; in your application to pull in the common types, or just use the crate like any other.

~ hello world ~

As is tradition:

vial::routes! {
    GET "/" => |_| "Hello, world!";
}

fn main() {
    vial::run!().unwrap();
}

For a bit more sanity, you can route to functions directly:

use vial::prelude::*;

routes! {
    GET "/echo" => echo;
    POST "/echo" => post;
}

fn echo(_: Request) -> &'static str {
    "<form method='POST'>
        <input type='text' name='echo'/>
        <input type='submit'/>
    </form>"
}

fn post(req: Request) -> String {
    format!(
        "<h1>You said: {}</h1>",
        req.form("echo").unwrap_or("You didn't say anything!")
    )
}

fn main() {
    vial::run!().unwrap();
}

To really break the mold, you can split your site into different modules:

use vial;

mod wiki;
mod blog;

mod index {
    use vial::prelude::*;
    routes! {
        GET "/" => |_| Response::from_file("index.html")
    }
}

fn main() {
    // The order matters here - if `wiki` and `blog` both define "/",
    // the `mod index` version will match first and get run.
    vial::run!(index, wiki, blog);
}

But hey, who wants to putz around with HTML when you can be writing Rust? Enable the horror feature and you're on your way:

use vial::prelude::*;

#[macro_use]
extern crate horrorshow;

routes! {
    GET "/" => |_| html! {
        p {
            : "You're looking for this: ";
            a(href="/echo") { : "echo" }
        }
    };
    GET "/echo" => echo;
    POST "/echo" => post;
}

fn echo(_: Request) -> impl Responder {
    html! {
        form(method="POST") {
            p {
            : "Type something: ";
                input(type="text", name="echo");
                input(type="submit");
            }
        }
    }
}

fn post(req: Request) -> impl Responder {
    owned_html! {
        h1: req.form("echo")
            .unwrap_or("You didn't say anything!");
    }
}

fn main() {
    vial::run!().unwrap();
}

~ bonus features ~

vial doesn't come with JSON or a template engine or any of that fancy stuff by default, but there (will be) a few compile-time --features you can activate for enhanced productivity:

Alice

  • hatter: Enable Hatter: A positively mad, HTML templating language.
  • horror: Enable horrorshow: A small & fast macro-based HTML builder.
  • json_serde: Request::json and Response::with_json powers, via Serde.
  • json_nano: Request::json and Response::with_json, via nanoserde.
  • cookies: Request::cookie(), Response::with_cookie, and friends.
  • sessions: Request::session(), Response::with_session, and friends.
  • uploads: Multipart form data (file uploads)
  • log: Access logging

Please note: The list above is a work-in-progress.

~ hot reloading ~

Your assets will automatically get reloaded in debug mode, complete with proper ETag support, but you probably want to refresh your Rust code, too.

Right now the easiest way is to use cargo-watch:

$ cargo install cargo-watch
$ cargo watch -x 'run --example hello_world'

~ testing ~

Tests can be run on a recent version of stable Rust with make test. We also run tests on commits with GitHub Actions.

Vial prefers to put everything in tests/ rather than include tests directly in src/*.rs files. To access private APIs in tests, we make them pub and use #[doc(hidden)]. Your cooperation is appreciated.

~ license ~

Vial is licensed under either of the following, at your option:

vial's People

Contributors

alexwennerberg avatar codef0x avatar curiouslycurious avatar ggsvr avatar jebailey avatar moalyousef avatar sigaloid avatar tdryer avatar undefineddarkness avatar xvxx 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

vial's Issues

tests fail in windows

Two tests fail when running in a windows environment.
The first is util > file_size. It appears that git will change the line endings of certain files automatically between unix and windows. So instead of finding 1052 and 25161 for the files in the test, it finds 1072 and 25835 respectively.

Additionally, the file_walker test fails. This is actually a problem with the PathBuf object which allows you to construct a path that ends up being a mix of UNIX and Windows-based file path separators. So rather than "tests/assets/rfcs/rfc1288.txt" you get
"tests/assets/rfcs\rfc1288.txt" as the comparison.

`Response::with_*` functions should either panic or state the error

Currently the with_*() functions for Response all have different error-behavior:

  • with_json() panics on serialization failure.
  • with_asset() will instead not panic, continue to execute and return a 404 upon attempting to retrieve the asset. This is the same behavior as if the route isn't even defined. Very confusing.
  • with_file() is the same as with_asset() except that if there is an error opening the file a 500 code with the actual error embedded is returned instead of a 404.

I think it should either be all panic, all returning a 500 with the included error text, a result be propagated somehow or the error message be printed in the "log". 404s are very confusing when the file you are trying to include doesn't exist as it looks like the route doesn't exist instead of the file not existing.

Minimum confusing example:

use vial::{routes, run};

routes! {
    GET "/file" => |_| Response::from_file("I DO NOT EXIST");
}

fn main() {
    _ = run!();
}

Output after two curls:

~ vial running at http://0.0.0.0:7667
GET 404 /file
GET 404 /route_not_existo

Possible Windows support?

Hi! So far, I like vial a lot!

However I was wondering if support for Windows is planned? I think that vial would benefit a lot from being cross-platform!

That's all I want to know for now, thanks :).

bundle_files! macro in build.rs causes panic stating "Not a directory"

i have my folder layed out as

.
โ”œโ”€โ”€ assets
โ”‚ย ย  โ”œโ”€โ”€ html
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ index.html
โ”‚ย ย  โ””โ”€โ”€ img
โ”‚ย ย      โ””โ”€โ”€ icon.ico
โ”œโ”€โ”€ build.rs
โ”œโ”€โ”€ Cargo.lock
โ”œโ”€โ”€ Cargo.toml
โ””โ”€โ”€ src
    โ””โ”€โ”€ main.rs

and my build.rs

use vial::bundle_assets;

fn main() {
	bundle_assets!("assets/").expect("unable to bundle assets!");
}

Unmaintained (?) + fork

While anxiously awaiting the return of @xvxx from a fishing expedition [1], I forked the repo and made a few improvements. Glad to contribute upstream when the maintainer returns, but until then, I won't make a bunch of PR's for each one.
I accepted #10 #11 #7 #6 + a few others. a list:

  • added proper cookie support, fixed cookies (which were previously broken, only parsing the first cookie)
  • added cookie tests
  • clippy formatting
  • resize threadpool when more than minimum_threads are being used at once (dynamically scale with demand)
  • thread timeout set to 1s (slow loris attack prevention)
  • remote_addr field from #6
  • changed cookie crate to one that is (sort of) compliant
  • added more github actions
  • fixed arithmetic overflow sigaloid#5
  • fixed OOM that would flood memory upon carefully-crafted header sigaloid#7
  • anything you want to contribute personally :D

Link: https://github.com/sigaloid/vial

1: image

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.