Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #27 from obsidian-rs/develop
Browse files Browse the repository at this point in the history
Version 0.1 alpha
  • Loading branch information
jk-gan authored Dec 31, 2019
2 parents 65facc2 + 92f9966 commit 3064a36
Show file tree
Hide file tree
Showing 22 changed files with 3,374 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

.DS_Store
.DS_Store

# vscode local config
/.vscode/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: rust
rust:
- stable
- beta
- nightly
branches:
only:
- master
- develop
matrix:
allow_failures:
- rust: nightly
fast_finish: true
cache: cargo
21 changes: 16 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
cargo-features = ["edition"]

[package]
name = "obsidian"
version = "0.1.0"
version = "0.1.0-alpha.1"
authors = ["Gan Jun Kai <[email protected]>", "Wai Pai Lee <[email protected]>"]
description = "A Rust framework"
repository = "https://github.com/jk-gan/obsidian"
edition = "2018"
description = "A Rust framework"
repository = "https://github.com/obsidian-rs/obsidian"
license = "MIT"

[dependencies]
hyper = "0.12.15"
futures = "0.1"
http = "0.1.14"
serde_json = "1.0.33"
serde_derive = "1.0.80"
serde = "1.0.99"
tokio-fs = "0.1.4"
tokio-io = "0.1.10"
url = "1.7.2"

[[example]]
name = "example"
path = "example/main.rs"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Gan Jun Kai & Wai Pai Lee

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Obsidian

## What's Obsidian?

`Obsidian` is a web-application framework in Rust with a vision to make Rust web development fun.

## Code Status

| **Version** | **Master** |
| :--------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------: |
| [![](http://meritbadge.herokuapp.com/obsidian)](https://crates.io/crates/obsidian) | [![Actions Status](https://github.com/obsidian-rs/obsidian/workflows/Obsidian%20Action/badge.svg)](https://github.com/obsidian-rs/obsidian/actions) |

## Hello World
```rust
use obsidian::App;

fn main() {
let mut app = App::new();
let addr = ([127, 0, 0, 1], 3000).into();

app.get("/", |_ctx| {
"Hello World"
});

app.listen(&addr, || {
println!("server is listening to {}", &addr);
});
}
```

## Hello World (with handler function)
```rust
use obsidian::{App, Responder, context::Context};

fn hello_world(_ctx: Context) -> impl Responder {
"Hello World"
}

fn main() {
let mut app = App::new();
let addr = ([127, 0, 0, 1], 3000).into();

app.get("/", hello_world);

app.listen(&addr, || {
println!("server is listening to {}", &addr);
});
}
```

## Example Files

Example are located in `example/main.rs`.

## Run Example

```
cargo run --example example
```

## Current State

NOT READY FOR PRODUCTION!
179 changes: 179 additions & 0 deletions example/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use serde_derive::*;
use std::{fmt, fmt::Display};

use obsidian::{
context::Context,
middleware::Logger,
router::{response, Responder, Router},
App, StatusCode,
};

// Testing example
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}

#[derive(Serialize, Deserialize, Debug)]
struct JsonTest {
title: String,
content: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct ParamTest {
test: Vec<String>,
test2: String,
}

impl Display for JsonTest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{title: {}, content: {}}}", self.title, self.content)
}
}

fn responder_obsidian_error(mut ctx: Context) -> impl Responder {
let json: JsonTest = ctx.json()?;
println!("{}", json);
Ok(response::json(json, StatusCode::OK))
}

fn main() {
let mut app = App::new();
let addr = ([127, 0, 0, 1], 3000).into();

app.get("/", |_ctx| {
"<!DOCTYPE html><html><head><link rel=\"shotcut icon\" href=\"favicon.ico\" type=\"image/x-icon\" sizes=\"32x32\" /></head> <h1>Hello Obsidian</h1></html>"
});

app.get("/json", |_ctx| {
let point = Point { x: 1, y: 2 };

response::json(point, StatusCode::OK)
// res.header(header::CONTENT_TYPE, "application/json")
// .status(StatusCode::OK)
// .json(point)
});

app.get("/empty-body", |_ctx| StatusCode::OK);

app.get("/vec", |_ctx| vec![1, 2, 3]);

app.get("/String", |_ctx| "<h1>This is a String</h1>".to_string());

app.get("/test/radix", |_ctx| "<h1>Test radix</h1>".to_string());

app.get("/team/radix", |_ctx| "<h1>Team radix</h1>".to_string());

app.get("/test/radix2", |_ctx| "<h1>Test radix2</h1>".to_string());

app.get("/jsontest", |_ctx| response::file("./testjson.html"));

app.get("/jsan", |_ctx: Context| "<h1>jsan</h1>".to_string());

app.post("/jsontestapi", |mut ctx: Context| {
let json: serde_json::Value = ctx.json().unwrap();

println!("{}", json);

response::json(json, StatusCode::OK)
});

app.post("/jsonteststructapi", responder_obsidian_error);

app.get("/test/wildcard/*", |ctx: Context| {
format!(
"{}<br>{}",
"<h1>Test wildcard</h1>".to_string(),
ctx.uri().path()
)
});

app.get("router/test", |ctx: Context| {
format!(
"{}<br>{}",
"<h1>router test get</h1>".to_string(),
ctx.uri().path()
)
});
app.post("router/test", |ctx: Context| {
format!(
"{}<br>{}",
"<h1>router test post</h1>".to_string(),
ctx.uri().path()
)
});
app.put("router/test", |ctx: Context| {
format!(
"{}<br>{}",
"<h1>router test put</h1>".to_string(),
ctx.uri().path()
)
});
app.delete("router/test", |ctx: Context| {
format!(
"{}<br>{}",
"<h1>router test delete</h1>".to_string(),
ctx.uri().path()
)
});

app.get("route/diff_route", |ctx: Context| {
format!(
"{}<br>{}",
"<h1>route diff get</h1>".to_string(),
ctx.uri().path()
)
});

let mut form_router = Router::new();

form_router.get("/formtest", |_ctx| response::file("./test.html"));

form_router.post("/formtest", |mut ctx: Context| {
let param_test: ParamTest = ctx.form().unwrap();

dbg!(&param_test);

response::json(param_test, StatusCode::OK)
});

let mut param_router = Router::new();
let logger = Logger::new();
app.use_service(logger);

param_router.get("/paramtest/:id", |ctx: Context| {
let param_test: i32 = ctx.param("id").unwrap();

dbg!(&param_test);

response::json(param_test, StatusCode::OK)
});
param_router.get("/paramtest/:id/test", |ctx: Context| {
let mut param_test: i32 = ctx.param("id").unwrap();
param_test = param_test * 10;

dbg!(&param_test);

response::json(param_test, StatusCode::OK)
});

param_router.get("/test-next-wild/*", |_ctx| {
"<h1>test next wild</h1>".to_string()
});

param_router.get("/*", |_ctx| {
"<h1>404 Not Found</h1>"
.to_string()
.with_status(StatusCode::NOT_FOUND)
});

app.use_router("/params/", param_router);
app.use_router("/forms/", form_router);
app.use_static_to("/files/", "/assets/");

app.listen(&addr, || {
println!("server is listening to {}", &addr);
});
}
Loading

0 comments on commit 3064a36

Please sign in to comment.