Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
aturon committed Nov 8, 2018
1 parent 4085eed commit 815c331
Show file tree
Hide file tree
Showing 20 changed files with 1,262 additions and 91 deletions.
41 changes: 0 additions & 41 deletions .github/ISSUE_TEMPLATE.md

This file was deleted.

21 changes: 0 additions & 21 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

59 changes: 59 additions & 0 deletions .vscode/tasks.json
@@ -0,0 +1,59 @@
{
"version": "2.0.0",
"presentation": {
"reveal": "always",
"panel": "new"
},
"tasks": [
{
"taskName": "cargo build",
"type": "shell",
"command": "cargo",
"args": [
"build"
],
"group": "build",
"problemMatcher": "$rustc"
},
{
"taskName": "run hello",
"type": "shell",
"command": "cargo",
"args": [
"run",
"--example",
"hello"
],
"problemMatcher": "$rustc"
},
{
"taskName": "run messages",
"type": "shell",
"command": "cargo",
"args": [
"run",
"--example",
"messages"
],
"problemMatcher": "$rustc"
},
{
"taskName": "cargo test",
"type": "shell",
"command": "cargo",
"args": [
"test"
],
"group": "test",
"problemMatcher": "$rustc"
},
{
"taskName": "cargo clean",
"type": "shell",
"command": "cargo",
"args": [
"clean"
]
}
]
}
27 changes: 21 additions & 6 deletions Cargo.toml
@@ -1,13 +1,28 @@
[package]
name = "tide"
version = "0.0.0"
license = "MIT OR Apache-2.0"
repository = "https://github.com/yoshuawuyts/tide"
documentation = "https://docs.rs/tide"
authors = [
"Aaron Turon <aturon@mozilla.com>",
"Yoshua Wuyts <yoshuawuyts@gmail.com>",
]
description = "WIP modular web framework"
authors = ["Yoshua Wuyts <yoshuawuyts@gmail.com>"]
documentation = "https://docs.rs/tide"
edition = "2018"
license = "MIT OR Apache-2.0"
name = "tide"
readme = "README.md"
repository = "https://github.com/yoshuawuyts/tide"
version = "0.0.0"

[dependencies]
http = "0.1"
hyper = "0.12.0"
pin-utils = "0.1.0-alpha.3"
serde = "1.0.80"
serde_derive = "1.0.80"
serde_json = "1.0.32"
typemap = "0.3.3"

[dependencies.futures-preview]
features = ["compat"]
version = "0.3.0-alpha.9"

[dev-dependencies]
30 changes: 9 additions & 21 deletions README.md
@@ -1,29 +1,17 @@
# tide
[![crates.io version][1]][2] [![build status][3]][4]
[![downloads][5]][6] [![docs.rs docs][7]][8]
# Tide

WIP modular web framework.
[![build status][1]][2]

- [Documentation][8]
- [Crates.io][2]
An early, experimental web framework. **NOT PRODUCTION-READY**.

## Usage
```txt
```
Read about the design here:

## Installation
```sh
$ cargo add tide
```
- [Rising Tide: building a modular web framework in the open](https://rust-lang-nursery.github.io/wg-net/2018/09/11/tide.html)
- [Routing and extraction in Tide: a first sketch](https://rust-lang-nursery.github.io/wg-net/2018/10/16/tide-routing.html)

## License

[MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE)

[1]: https://img.shields.io/crates/v/tide.svg?style=flat-square
[2]: https://crates.io/crates/tide
[3]: https://img.shields.io/travis/yoshuawuyts/tide.svg?style=flat-square
[4]: https://travis-ci.org/yoshuawuyts/tide
[5]: https://img.shields.io/crates/d/tide.svg?style=flat-square
[6]: https://crates.io/crates/tide
[7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square
[8]: https://docs.rs/tide
[1]: https://img.shields.io/travis/yoshuawuyts/tide.svg?style=flat-square
[2]: https://travis-ci.org/yoshuawuyts/tide
7 changes: 7 additions & 0 deletions examples/hello.rs
@@ -0,0 +1,7 @@
#![feature(async_await)]

fn main() {
let mut app = tide::App::new(());
app.at("/").get(async || "Hello, world!");
app.serve("127.0.0.1:7878")
}
85 changes: 85 additions & 0 deletions examples/messages.rs
@@ -0,0 +1,85 @@
#![feature(async_await, futures_api)]

#[macro_use]
extern crate serde_derive;

use http::status::StatusCode;
use std::sync::{Arc, Mutex};
use tide::{body, head, App, AppData};

#[derive(Clone)]
struct Database {
contents: Arc<Mutex<Vec<Message>>>,
}

#[derive(Serialize, Deserialize, Clone)]
struct Message {
author: Option<String>,
contents: String,
}

impl Database {
fn new() -> Database {
Database {
contents: Arc::new(Mutex::new(Vec::new())),
}
}

fn insert(&mut self, msg: Message) -> usize {
let mut table = self.contents.lock().unwrap();
table.push(msg);
table.len() - 1
}

fn get(&mut self, id: usize) -> Option<Message> {
self.contents.lock().unwrap().get(id).cloned()
}

fn set(&mut self, id: usize, msg: Message) -> bool {
let mut table = self.contents.lock().unwrap();

if let Some(old_msg) = table.get_mut(id) {
*old_msg = msg;
true
} else {
false
}
}
}

async fn new_message(mut db: AppData<Database>, msg: body::Json<Message>) -> String {
db.insert(msg.0).to_string()
}

async fn set_message(
mut db: AppData<Database>,
id: head::Path<usize>,
msg: body::Json<Message>,
) -> Result<(), StatusCode> {
if db.set(id.0, msg.0) {
Ok(())
} else {
Err(StatusCode::NOT_FOUND)
}
}

async fn get_message(
mut db: AppData<Database>,
id: head::Path<usize>,
) -> Result<body::Json<Message>, StatusCode> {
if let Some(msg) = db.get(id.0) {
Ok(body::Json(msg))
} else {
Err(StatusCode::NOT_FOUND)
}
}

fn main() {
let mut app = App::new(Database::new());

app.at("/message").post(new_message);
app.at("/message/{}").get(get_message);
app.at("/message/{}").post(set_message);

app.serve("127.0.0.1:7878");
}
2 changes: 0 additions & 2 deletions rustfmt.toml

This file was deleted.

0 comments on commit 815c331

Please sign in to comment.