Skip to content

Commit c8cbdb0

Browse files
committed
0.3.0
1 parent e3bb523 commit c8cbdb0

File tree

5 files changed

+196
-85
lines changed

5 files changed

+196
-85
lines changed

Cargo.lock

Lines changed: 62 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dlphn"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Todd Treece <[email protected]>"]
55
description = "a humble data logger."
66
homepage = "https://github.com/toddtreece/dlphn-rs"
@@ -9,12 +9,11 @@ keywords = ["data", "logger", "iot"]
99
readme = "README.md"
1010
license = "MIT"
1111
edition = "2018"
12-
include = ["src/**/*", "Cargo.toml", "docs/**/*", "ui/build/**"]
12+
include = ["src/**/*", "Cargo.toml"]
1313

1414
[dependencies]
1515
actix-rt = "1.0.0"
1616
actix-web = "2.0.0"
17-
actix-files = "0.2.1"
1817
failure = "0.1.1"
1918
futures = "0.3.1"
2019
r2d2 = "0.8.2"
@@ -24,6 +23,8 @@ serde_derive = "1.0"
2423
serde_json = "1.0.44"
2524
chrono = { version = "0.4.10", features = ["serde"] }
2625
local-ip = "0.1"
26+
rust-embed="5.2.0"
27+
mime_guess = "2"
2728

2829
[dependencies.rusqlite]
2930
version = "0.21.0"

src/main.rs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
use actix_files as fs;
2-
use actix_web::{web, App, Error as WebError, HttpResponse, HttpServer};
1+
#[macro_use]
2+
extern crate rust_embed;
3+
4+
use actix_web::body::Body;
5+
use actix_web::{web, App, Error as WebError, HttpRequest, HttpResponse, HttpServer};
36
use futures::TryFutureExt;
47
use local_ip;
8+
use mime_guess::from_path;
59
use r2d2_sqlite::{self, SqliteConnectionManager};
610
use serde_json;
11+
use std::borrow::Cow;
712
use std::io;
813

914
mod db;
1015
mod dolphin;
1116
use db::Pool;
1217

18+
#[derive(RustEmbed)]
19+
#[folder = "ui/build/"]
20+
struct UIAsset;
21+
22+
#[derive(RustEmbed)]
23+
#[folder = "docs/"]
24+
struct DocAsset;
25+
1326
async fn list_streams(db: web::Data<Pool>) -> Result<HttpResponse, WebError> {
1427
let pool = db.clone();
1528

@@ -47,38 +60,77 @@ async fn insert_data(
4760
Ok(HttpResponse::Ok().finish())
4861
}
4962

50-
async fn p404() -> Result<fs::NamedFile, WebError> {
51-
Ok(fs::NamedFile::open("ui/build/index.html")?.set_status_code(actix_web::http::StatusCode::OK))
63+
fn handle_ui_file(path: &str) -> HttpResponse {
64+
match UIAsset::get(path) {
65+
Some(content) => {
66+
let body: Body = match content {
67+
Cow::Borrowed(bytes) => bytes.into(),
68+
Cow::Owned(bytes) => bytes.into(),
69+
};
70+
HttpResponse::Ok()
71+
.content_type(from_path(path).first_or_octet_stream().as_ref())
72+
.body(body)
73+
}
74+
None => handle_ui_file("index.html"),
75+
}
76+
}
77+
78+
fn handle_doc_file(path: &str) -> HttpResponse {
79+
match DocAsset::get(path) {
80+
Some(content) => {
81+
let body: Body = match content {
82+
Cow::Borrowed(bytes) => bytes.into(),
83+
Cow::Owned(bytes) => bytes.into(),
84+
};
85+
HttpResponse::Ok()
86+
.content_type(from_path(path).first_or_octet_stream().as_ref())
87+
.body(body)
88+
}
89+
None => HttpResponse::NotFound().body("404 Not Found"),
90+
}
91+
}
92+
93+
fn ui(req: HttpRequest) -> HttpResponse {
94+
let path = &req.path()["/".len()..];
95+
handle_ui_file(path)
96+
}
97+
98+
fn docs_index(_req: HttpRequest) -> HttpResponse {
99+
handle_doc_file("index.html")
100+
}
101+
102+
fn docs(req: HttpRequest) -> HttpResponse {
103+
let path = &req.path()["/api/v1/docs/".len()..];
104+
handle_doc_file(path)
52105
}
53106

54107
#[actix_rt::main]
55108
async fn main() -> io::Result<()> {
56109
let manager = SqliteConnectionManager::file("dlphn.db");
57110
let pool = Pool::new(manager).unwrap();
58111

59-
// create the table if needed
60112
db::create_table(pool.get().unwrap()).unwrap();
61-
62-
// Start http server
63113
dolphin::logo();
114+
64115
let ip = local_ip::get().unwrap();
65116
println!("[dlphn] UI available at http://{}:8080", ip.to_string());
66117
println!(
67118
"[dlphn] API docs available at: http://{}:8080/api/v1/docs",
68119
ip.to_string()
69120
);
121+
70122
HttpServer::new(move || {
71123
App::new()
72124
.data(pool.clone())
73-
.service(fs::Files::new("/api/v1/docs", "docs").index_file("index.html"))
125+
.service(web::resource("/api/v1/docs").route(web::get().to(docs_index)))
126+
.service(web::resource("/api/v1/docs/{_:.*}").route(web::get().to(docs)))
74127
.service(web::resource("/api/v1/streams").route(web::get().to(list_streams)))
75128
.service(
76129
web::resource("/api/v1/streams/{key}/data")
77130
.route(web::get().to(list_data))
78131
.route(web::post().to(insert_data)),
79132
)
80-
.service(fs::Files::new("/", "ui/build").index_file("index.html"))
81-
.default_service(web::resource("").route(web::get().to(p404)))
133+
.service(web::resource("/{_:.*}").route(web::get().to(ui)))
82134
})
83135
.bind("0.0.0.0:8080")?
84136
.run()

0 commit comments

Comments
 (0)