项目作者: xvxx

项目描述 :
🧪 a micro micro-framework for rust
高级语言: Rust
项目地址: git://github.com/xvxx/vial.git
创建时间: 2020-05-17T06:20:35Z
项目社区:https://github.com/xvxx/vial

开源协议:Other

下载


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:

  1. [dependencies]
  2. 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:

  1. vial::routes! {
  2. GET "/" => |_| "Hello, world!";
  3. }
  4. fn main() {
  5. vial::run!().unwrap();
  6. }

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

  1. use vial::prelude::*;
  2. routes! {
  3. GET "/echo" => echo;
  4. POST "/echo" => post;
  5. }
  6. fn echo(_: Request) -> &'static str {
  7. "<form method='POST'>
  8. <input type='text' name='echo'/>
  9. <input type='submit'/>
  10. </form>"
  11. }
  12. fn post(req: Request) -> String {
  13. format!(
  14. "<h1>You said: {}</h1>",
  15. req.form("echo").unwrap_or("You didn't say anything!")
  16. )
  17. }
  18. fn main() {
  19. vial::run!().unwrap();
  20. }

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

  1. use vial;
  2. mod wiki;
  3. mod blog;
  4. mod index {
  5. use vial::prelude::*;
  6. routes! {
  7. GET "/" => |_| Response::from_file("index.html")
  8. }
  9. }
  10. fn main() {
  11. // The order matters here - if `wiki` and `blog` both define "/",
  12. // the `mod index` version will match first and get run.
  13. vial::run!(index, wiki, blog);
  14. }

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:

  1. use vial::prelude::*;
  2. #[macro_use]
  3. extern crate horrorshow;
  4. routes! {
  5. GET "/" => |_| html! {
  6. p {
  7. : "You're looking for this: ";
  8. a(href="/echo") { : "echo" }
  9. }
  10. };
  11. GET "/echo" => echo;
  12. POST "/echo" => post;
  13. }
  14. fn echo(_: Request) -> impl Responder {
  15. html! {
  16. form(method="POST") {
  17. p {
  18. : "Type something: ";
  19. input(type="text", name="echo");
  20. input(type="submit");
  21. }
  22. }
  23. }
  24. }
  25. fn post(req: Request) -> impl Responder {
  26. owned_html! {
  27. h1: req.form("echo")
  28. .unwrap_or("You didn't say anything!");
  29. }
  30. }
  31. fn main() {
  32. vial::run!().unwrap();
  33. }

~ 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
    1. language.
  • horror: Enable horrorshow: A small & fast macro-based HTML
    1. builder.
  • json_serde: Request::json and Response::with_json powers, via
    1. Serde.
  • json_nano: Request::json and Response::with_json, via
    1. nanoserde.
  • cookies: Request::cookie(), Response::with_cookie, and
    1. friends.
  • sessions: Request::session(), Response::with_session, and
    1. 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:

  1. $ cargo install cargo-watch
  2. $ 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: