Skip to content

Commit 7c6cfc7

Browse files
committed
Initial setup
0 parents  commit 7c6cfc7

File tree

11 files changed

+343
-0
lines changed

11 files changed

+343
-0
lines changed

.github/workflows/test.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [master, develop]
6+
pull_request:
7+
branches: [master, develop]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Run tests
16+
run: cargo test --verbose --features="all"

.gitignore

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Created by https://www.gitignore.io/api/rust,macos
2+
# Edit at https://www.gitignore.io/?templates=rust,macos
3+
4+
### macOS ###
5+
# General
6+
.DS_Store
7+
.AppleDouble
8+
.LSOverride
9+
10+
# Icon must end with two \r
11+
Icon
12+
13+
# Thumbnails
14+
._*
15+
16+
# Files that might appear in the root of a volume
17+
.DocumentRevisions-V100
18+
.fseventsd
19+
.Spotlight-V100
20+
.TemporaryItems
21+
.Trashes
22+
.VolumeIcon.icns
23+
.com.apple.timemachine.donotpresent
24+
25+
# Directories potentially created on remote AFP share
26+
.AppleDB
27+
.AppleDesktop
28+
Network Trash Folder
29+
Temporary Items
30+
.apdisk
31+
32+
### Rust ###
33+
# Generated by Cargo
34+
# will have compiled files and executables
35+
/target/
36+
37+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
38+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
39+
Cargo.lock
40+
41+
# These are backup files generated by rustfmt
42+
**/*.rs.bk
43+
44+
### Others ###
45+
.halt.releez.yml
46+
.idea
47+
routerify-websocket.iml
48+
49+
# End of https://www.gitignore.io/api/rust,macos

Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "routerify-websocket"
3+
version = "0.1.0"
4+
description = "An websocket extension for Routerify."
5+
homepage = "https://github.com/routerify/routerify-websocket"
6+
repository = "https://github.com/routerify/routerify-websocket"
7+
keywords = ["routerify", "hyper-rs", "hyper", "websocket", "ws"]
8+
categories = ["asynchronous","web-programming","web-programming::http-server"]
9+
authors = ["Rousan Ali <[email protected]>"]
10+
readme = "README.md"
11+
license = "MIT"
12+
edition = "2018"
13+
14+
[package.metadata.docs.rs]
15+
all-features = true
16+
17+
[package.metadata.playground]
18+
features = ["all"]
19+
20+
[features]
21+
default = []
22+
all = []
23+
24+
[dependencies]
25+
log = "0.4"
26+
derive_more = "0.99"
27+
routerify = "1.1"
28+
hyper = "0.13"
29+
tokio-tungstenite = { version = "0.10", default-features = false, optional = true }
30+
headers = { version = "0.3", optional = true }
31+
futures = { version = "0.3", default-features = false, optional = true }
32+
tokio = { version = "0.2", features = ["rt-core"], optional = true}
33+
34+
[dev-dependencies]
35+
tokio = { version = "0.2", features = ["full"] }

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Rousan Ali
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[![Github Actions Status](https://github.com/routerify/routerify-websocket/workflows/Test/badge.svg)](https://github.com/routerify/routerify-websocket/actions)
2+
[![crates.io](https://img.shields.io/crates/v/routerify-websocket.svg)](https://crates.io/crates/routerify-websocket)
3+
[![Documentation](https://docs.rs/routerify-websocket/badge.svg)](https://docs.rs/routerify-websocket)
4+
[![MIT](https://img.shields.io/crates/l/routerify-websocket.svg)](./LICENSE)
5+
6+
# routerify-websocket
7+
8+
An websocket extension for Routerify.
9+
10+
[Docs](https://docs.rs/routerify-websocket)
11+
12+
## Install
13+
14+
Add this to your `Cargo.toml` file:
15+
16+
```toml
17+
[dependencies]
18+
routerify = "1.1"
19+
routerify-websocket = "0.1.0"
20+
```
21+
22+
## Example
23+
24+
```rust
25+
use routerify_websocket;
26+
27+
fn main() {
28+
println!("{}", routerify_websocket::add(2, 3));
29+
}
30+
```
31+
32+
## Contributing
33+
34+
Your PRs and suggestions are always welcome.

examples/test.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use hyper::{Body, Request, Response, Server};
2+
use routerify::prelude::*;
3+
use routerify::{Middleware, Router, RouterService};
4+
use std::{convert::Infallible, net::SocketAddr};
5+
6+
async fn ws_handler(_: Request<Body>) -> Result<Response<Body>, Infallible> {
7+
Ok(Response::new(Body::from("ws handler")))
8+
}
9+
10+
async fn logger(req: Request<Body>) -> Result<Request<Body>, Infallible> {
11+
println!("{} {} {}", req.remote_addr(), req.method(), req.uri().path());
12+
Ok(req)
13+
}
14+
15+
fn router() -> Router<Body, Infallible> {
16+
Router::builder()
17+
.middleware(Middleware::pre(logger))
18+
.get("/ws/connect", ws_handler)
19+
.build()
20+
.unwrap()
21+
}
22+
23+
#[tokio::main]
24+
async fn main() {
25+
let router = router();
26+
27+
let service = RouterService::new(router).unwrap();
28+
29+
let addr = SocketAddr::from(([127, 0, 0, 1], 3001));
30+
31+
let server = Server::bind(&addr).serve(service);
32+
33+
println!("App is running on: {}", addr);
34+
if let Err(err) = server.await {
35+
eprintln!("Server error: {}", err);
36+
}
37+
}

releez.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
version: 1.0.0
2+
checklist:
3+
- name: Checkout develop and sync with remote
4+
type: auto
5+
run:
6+
- git checkout develop
7+
- git push
8+
- name: Check syntax
9+
type: auto
10+
run:
11+
- cargo check --release --features="all"
12+
- name: Run tests
13+
type: auto
14+
run:
15+
- cargo test --release --features="all"
16+
- name: Start a new release branch
17+
type: auto
18+
run:
19+
- git flow release start "v${VERSION}"
20+
- name: Make sure code is formatted
21+
type: auto
22+
run:
23+
- cargo fmt
24+
- name: Bump version
25+
type: manual
26+
instructions:
27+
- Please update version with ${VERSION} in Cargo.toml file.
28+
- Please update version with ${VERSION} in README.md file if needed.
29+
- name: Commit changes
30+
type: auto
31+
run:
32+
- git add --all && git commit -m "Bump version"
33+
- name: Finish release branch
34+
type: auto
35+
run:
36+
- git flow release finish -s "v${VERSION}"
37+
- name: Push branches and tags to Github
38+
type: auto
39+
run:
40+
- git checkout master
41+
- git push origin master
42+
- git push origin develop
43+
- git push --tags
44+
- name: Edit tag on Github
45+
type: manual
46+
instructions:
47+
- Tag is pushed to Github(https://github.com/routerify/routerify-websocket/releases). Edit it there and make it a release.
48+
- name: Publish to crates.io
49+
type: auto
50+
confirm: Are you sure to publish it to crates.io?
51+
run:
52+
- cargo publish
53+
- git checkout develop

rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
max_width = 120
2+
tab_spaces = 4

src/error.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use derive_more::Display;
2+
use std::fmt::{self, Debug, Display, Formatter};
3+
4+
type BoxError = Box<dyn std::error::Error + Send + Sync>;
5+
6+
#[derive(Display)]
7+
#[display(fmt = "routerify-websocket: {}")]
8+
pub enum Error {
9+
/// Something wrong happened.
10+
#[display(fmt = "Something wrong happened: {}", _0)]
11+
SomethingWrong(BoxError),
12+
13+
/// Something wrong happened again.
14+
#[display(fmt = "Something wrong happened again: {}", data)]
15+
SomethingWrongAgain { data: String },
16+
17+
/// Some other wrong happened.
18+
#[display(fmt = "Some other wrong happened")]
19+
DifferentOne,
20+
21+
#[doc(hidden)]
22+
__Nonexhaustive,
23+
}
24+
25+
impl Debug for Error {
26+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
27+
Display::fmt(self, f)
28+
}
29+
}
30+
31+
impl std::error::Error for Error {}
32+
33+
impl PartialEq for Error {
34+
fn eq(&self, other: &Self) -> bool {
35+
self.to_string().eq(&other.to_string())
36+
}
37+
}
38+
39+
impl Eq for Error {}

src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! An websocket extension for Routerify.
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! use routerify_websocket;
7+
//!
8+
//! # fn run() {
9+
//! println!("{}", routerify_websocket::add(2, 3));
10+
//! # }
11+
//! # run();
12+
//! ```
13+
14+
pub use self::error::Error;
15+
16+
mod error;
17+
18+
/// This function adds two numbers.
19+
pub fn add(a: i32, b: i32) -> i32 {
20+
a + b
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn test_add() {
29+
assert_eq!(add(2, 2), 4);
30+
}
31+
}

0 commit comments

Comments
 (0)