-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 31f844c
Showing
15 changed files
with
764 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[target.x86_64-pc-windows-gnu] | ||
linker = "/usr/bin/x86_64-w64-mingw32-gcc" | ||
|
||
[target.i686-pc-windows-gnu] | ||
linker = "/usr/bin/i686-w64-mingw32-gcc" | ||
rustflags = "-C panic=abort" | ||
|
||
[target.x86_64-apple-darwin] | ||
linker = "x86_64-apple-darwin15-gcc" | ||
ar = "x86_64-apple-darwin15-ar" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
|
||
# Added by cargo | ||
|
||
/target | ||
|
||
/rendered_templates | ||
|
||
release.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "binserve" | ||
version = "0.1.0" | ||
authors = ["mufeedvh <mufeedvh@gmail.com>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
futures = "0.3.5" | ||
env_logger = "0.7.1" | ||
|
||
actix-web = "3" | ||
actix-files = "0.3.0" | ||
actix-rt = "1.1.1" | ||
|
||
serde = { version = "1.0.116", features = ["derive"] } | ||
serde_json = "1.0.58" | ||
|
||
handlebars = "3.5.0" | ||
|
||
[profile.release] | ||
lto = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Mufeed VH | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# `binserve` :zap::crab: | ||
|
||
A blazingly fast static web server in a single binary you can set up with zero code. :fire: | ||
|
||
<p align="left"> | ||
<img src="https://img.shields.io/badge/version-0.1.0-blue.svg" title="version" alt="version"> | ||
<a href="https://github.com/mufeedvh/binserve/blob/master/LICENSE"><img alt="GitHub license" src="https://img.shields.io/github/license/mufeedvh/basecrack.svg"></a> | ||
<a href="https://twitter.com/intent/tweet?text=Check%20this%20out!%20A%20blazingly%20fast%20static%20web%20server%20in%20a%20single%20binary%20you%20can%20set%20up%20with%20zero%20code.:&url=https%3A%2F%2Fgithub.com%2Fmufeedvh%2Fbinserve"><img alt="Twitter" src="https://img.shields.io/twitter/url/https/github.com/mufeedvh/basecrack.svg?style=social"></a> | ||
</p> | ||
|
||
## Features | ||
|
||
- **Single binary with no dependencies and everything built-in.** | ||
- **Blazingly fast!** :zap: - it's built on top of [**Actix**](https://actix.rs/), one of the [**fastest web frameworks**](https://www.techempower.com/benchmarks/) in the world and of course, written in **Rust**. | ||
- **Everything in a single config file** - everything you need to setup is in the `binserve.json`, just change it, run it! | ||
- **Epic Portability** - just carry the binary around and all you have to change is the `host` and `port` in the `binserve.json`. | ||
- **Easiest Routing** - you just have to enter the `route` and the `static file` to respond in the `binserve.json` to add a route entry! | ||
- **[Handlebars](https://github.com/sunng87/handlebars-rust) template engine** - renders every static file with `Handlebars` on the first run improving performance! | ||
- **Template Variables in one place** - that too in the one and only config file! | ||
- **Secure by design** - runs security validation checks on the first run and will only run the server once configuration is confirmed secure! (See [Security](https://github.com/mufeedvh/binserve#security)) | ||
- **Config & Static File Assistance** - just running it will create the configuration file and the static directory boilerplate for you! | ||
- **Supports Any Static Files** - just give any static file of your choice to routes and response will match it's `Content-Type`. | ||
- **Straightforward Directory Structure** - static files falls under the `static` directory, you can even change that! And `images`, `css`, and `js` falls under the `assets` directory which should all be created for you in the first run itself! | ||
- **Custom Error Page Support** - you can design your own fancy error pages! | ||
- **Actix Logging Middleware** - Logging is powered directly from **Actix**. | ||
|
||
## Hello World! | ||
|
||
Download the binary for your OS from [**Releases**](https://github.com/mufeedvh/binserve/releases), then just run it: | ||
|
||
$ binserve | ||
|
||
That's it. Done! You should see the following output: | ||
|
||
``` | ||
_ _ | ||
| |_|_|___ ___ ___ ___ _ _ ___ | ||
| . | | |_ -| -_| _| | | -_| | ||
|___|_|_|_|___|___|_| \_/|___| v0.1.0 | ||
Your server is up and running at http://example.com:80/ | ||
``` | ||
|
||
Here is how the directory structure will look like: | ||
|
||
``` | ||
├── binserve | ||
├── binserve.json | ||
├── rendered_templates | ||
│ ├── 404.html | ||
│ └── index.html | ||
└── static | ||
├── 404.html | ||
├── assets | ||
│ ├── css | ||
│ ├── images | ||
│ └── js | ||
└── index.html | ||
``` | ||
|
||
### ⚙️ Configuration File: | ||
|
||
📄 **File:** `binserve.json` | ||
|
||
```json | ||
{ | ||
"directory_listing": false, | ||
"enable_logging": true, | ||
"error_pages": { | ||
"404": "404.html" | ||
}, | ||
"follow_symlinks": false, | ||
"routes": { | ||
"/": "index.html", | ||
"/example": "example.html" | ||
}, | ||
"server": { | ||
"host": "127.0.0.1", | ||
"port": 1337 | ||
}, | ||
"static_directory": "static", | ||
"template_variables": { | ||
"load_static": "/static/", | ||
"name": "Binserve" | ||
} | ||
} | ||
``` | ||
|
||
The whole thing revolves around this configuration file, whatever changes you want to make, just edit the config and run it! | ||
|
||
### 🎨 Templates: | ||
|
||
`binserve` uses [Handlebars](https://github.com/sunng87/handlebars-rust) as the template engine as it perfectly fits our use case. | ||
|
||
**Here is an example:** | ||
|
||
```html | ||
<html> | ||
<head> | ||
<title>Example</title> | ||
</head> | ||
<body> | ||
<h1>My name is {{name}}</h1> | ||
</body> | ||
</html> | ||
``` | ||
|
||
Now add your name in the config file (`binserve.json`) as a `template variable`: | ||
|
||
```json | ||
"template_variables": { | ||
"load_static": "/static/", | ||
"name": "Keanu Reeves" | ||
} | ||
``` | ||
|
||
Now run the server! | ||
|
||
$ binserve | ||
|
||
This would render down to: | ||
|
||
```html | ||
<html> | ||
<head> | ||
<title>Example</title> | ||
</head> | ||
<body> | ||
<h1>My name is Keanu Reeves</h1> | ||
</body> | ||
</html> | ||
``` | ||
|
||
To load static files such as `images`, `css`, and `javascript`, just use `{{load_static}}`: | ||
|
||
`load_static` is specified in the `binserve.json` itself. | ||
|
||
```html | ||
<img src="{{load_static}}images/rick_roll.gif"> | ||
<link rel="stylesheet" href="{{load_static}}css/main.css"> | ||
<script src="{{load_static}}js/script.js"> | ||
``` | ||
`binserve` renders all your template at once on the first run itself to improve performance as it wouldn't have to render the template on each request. | ||
## Build From Source | ||
$ git clone https://github.com/mufeedvh/binserve.git | ||
$ cd binserve/ | ||
$ cargo build | ||
## Security | ||
Security is one of the most crucial elements in a web server, `binserve` is secure by design. **Here is how it's secure:** | ||
- Routes are specified in the configuration file not directly accepted from the user. | ||
- Runs a check for **Path Traversal** attempts in routes in the configuration file on each run. | ||
- Runs a check for **Symlink Files** which might point to sensitive files on each run. | ||
- Only **Follows Symlinks** when explicitly allowed in the configuration file which is disabled by default. | ||
- Only enables **Directory Listing** when explicitly allowed in the configuration file which is disabled by default. | ||
## Contribution | ||
Ways to contribute | ||
- Suggest a feature | ||
- Report a bug | ||
- Fix something and open a pull request | ||
- Help me document the code | ||
- Spread the word | ||
- Create an example with binserve and you will be featured here! | ||
## License | ||
Licensed under the MIT License, see <a href="https://github.com/mufeedvh/binserve/blob/master/LICENSE">LICENSE</a> for more information. | ||
## Liked the project? | ||
Support the author by buying him a coffee! | ||
<a href="https://www.buymeacoffee.com/mufeedvh" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="51px" width="217px"></a> | ||
------------ | ||
***Support this project by starring ⭐, sharing 📲, and contributing 👩💻! :heart:*** | ||
------------ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"directory_listing": false, | ||
"enable_logging": true, | ||
"error_pages": { | ||
"404": "404.html" | ||
}, | ||
"follow_symlinks": false, | ||
"routes": { | ||
"/": "index.html", | ||
"/example": "example.html" | ||
}, | ||
"server": { | ||
"host": "127.0.0.1", | ||
"port": 1337 | ||
}, | ||
"static_directory": "static", | ||
"template_variables": { | ||
"load_static": "/static/", | ||
"name": "Binserve" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
`config.rs` - Configuration Functions | ||
generate, save, and get config | ||
*/ | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use std::io::{BufReader, Write}; | ||
use std::path::Path; | ||
|
||
// config filename | ||
static CONFIG_FILE: &str = "binserve.json"; | ||
|
||
// save the config to an environment variable | ||
fn save_config() -> std::io::Result<()> { | ||
let config_file = File::open(CONFIG_FILE)?; | ||
let mut buf_reader = BufReader::new(config_file); | ||
let mut json_string = String::new(); | ||
buf_reader.read_to_string(&mut json_string)?; | ||
env::set_var("JSON_CONFIG", json_string); | ||
Ok(()) | ||
} | ||
|
||
// generate the config file for binserve - `binserve.json` | ||
fn generate_config_file() -> std::io::Result<()> { | ||
let config_obj = serde_json::json!({ | ||
"server": { | ||
"host": "127.0.0.1", | ||
"port": 1337 | ||
}, | ||
"static_directory": "static", | ||
"routes": { | ||
"/": "index.html", | ||
"/example": "example.html" | ||
}, | ||
"template_variables": { | ||
"load_static": "/static/", | ||
"name": "Binserve" | ||
}, | ||
"error_pages": { | ||
"404": "404.html" | ||
}, | ||
"enable_logging": true, | ||
"directory_listing": false, | ||
"follow_symlinks": false | ||
}); | ||
|
||
let contents = serde_json::to_string_pretty(&config_obj).unwrap(); | ||
|
||
let mut file = File::create(CONFIG_FILE)?; | ||
file.write_all(contents.as_bytes())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn setup_config() { | ||
// only generate the config file if it doesn't exist already | ||
if !Path::new(CONFIG_FILE).exists() { | ||
generate_config_file().ok(); | ||
} | ||
save_config().ok(); | ||
} | ||
|
||
// this function returns the datastructure of the JSON config | ||
pub fn get_config() -> serde_json::Value { | ||
let bs_config = env::var("JSON_CONFIG").unwrap(); | ||
|
||
let json_config: serde_json::Value = | ||
serde_json::from_str(&bs_config).expect("JSON was not well-formatted"); | ||
|
||
json_config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
`error_page.rs` - Error Page Generation | ||
generate error pages | ||
*/ | ||
use crate::config::get_config; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
// create a default `404 Not Found` error page if it doesn't exist | ||
fn generate_404_page(static_dir: &str) -> std::io::Result<()> { | ||
let contents = b"<html>\ | ||
<title>404 Not Found</title>\ | ||
<body>\ | ||
<h1>404 Not Found</h1>\ | ||
<hr>\ | ||
<p><i>binserve v0.1.0</i></p>\ | ||
</body>\ | ||
</html>"; | ||
|
||
let file_path = format!("{}/404.html", static_dir); | ||
if !Path::new(&file_path).exists() { | ||
let mut file = File::create(file_path)?; | ||
file.write_all(contents)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn generate_error_pages() { | ||
let config = get_config(); | ||
let static_dir = config["static_directory"].to_string().replace("\"", ""); | ||
generate_404_page(&static_dir).ok(); | ||
} |
Oops, something went wrong.