- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从我的 super 服务器提供一些静态文件(.js、.css、...)。
目前我能想到的唯一方法是将文件内联为字符串/在启动时加载它们。
有没有更好的方法直接提供整个目录或选定文件?
最佳答案
在 crates.io 中输入“hyper static”之后, 第一个结果是 hyper-staticfile .该项目的GitHub repository有一个 examples directory ,举一个这样的例子:
extern crate futures;
extern crate hyper;
extern crate hyper_staticfile;
extern crate tokio_core;
// This example serves the docs from target/doc/hyper_staticfile at /doc/
//
// Run `cargo doc && cargo run --example doc_server`, then
// point your browser to http://localhost:3000/
use futures::{Future, Stream, future};
use hyper::Error;
use hyper::server::{Http, Request, Response, Service};
use hyper_staticfile::Static;
use std::path::Path;
use tokio_core::reactor::{Core, Handle};
use tokio_core::net::TcpListener;
type ResponseFuture = Box<Future<Item=Response, Error=Error>>;
struct MainService {
static_: Static,
}
impl MainService {
fn new(handle: &Handle) -> MainService {
MainService {
static_: Static::new(handle, Path::new("target/doc/")),
}
}
}
impl Service for MainService {
type Request = Request;
type Response = Response;
type Error = Error;
type Future = ResponseFuture;
fn call(&self, req: Request) -> Self::Future {
if req.path() == "/" {
let res = Response::new()
.with_status(hyper::StatusCode::MovedPermanently)
.with_header(hyper::header::Location::new("/hyper_staticfile/"));
Box::new(future::ok(res))
} else {
self.static_.call(req)
}
}
}
fn main() {
let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = "127.0.0.1:3000".parse().unwrap();
let listener = TcpListener::bind(&addr, &handle).unwrap();
let http = Http::new();
let server = listener.incoming().for_each(|(sock, addr)| {
let s = MainService::new(&handle);
http.bind_connection(&handle, sock, addr, s);
Ok(())
});
println!("Doc server running on http://localhost:3000/");
core.run(server).unwrap();
}
关于server - 如何在 hyper 中提供静态文件/目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45713444/
我第一次使用Hyper-V(运行Windows 8)。当我创建一个新的外部虚拟网络交换机时,它将虚拟机的网络适配器与WiFi桥接-出于某种原因,它会中断WiFi的Internet连接。为什么,我可能做
我有一个 vhd 文件,我需要在其上启用网络和远程连接。 vhd上安装了Windows Server 2008 R2和SharePoint开发平台、VS 2010... 我的笔记本电脑运行 Windo
我今天在我的 HP Windows 10 Pro 笔记本电脑上启用了 Hyper-V,它有第二台显示器,配置为扩展显示器(笔记本电脑是 #1,外接显示器是 #2)。重新启动计算机后,我注意到我的显示器
有谁知道为什么我在 hyper-v 管理器中看不到 HoloLens 2 模拟器作为虚拟机?据我所知,它是基于hyper-v的。我这样做是为了将默认虚拟交换机更改为外部虚拟交换机(也是虚拟交换机),以
Windows 手机模拟器需要 Hyper-V 才能运行,但 Android 模拟器又需要 Intel 硬件加速管理器 (HAXM),而后者无法兼容 Hyper-V。 有没有办法保持 Hyper-V
我在 Iron 处理程序中发出客户端请求。如何重用 Tokio 的 Core 和 Hyper 的 Client?我正在使用 hyper 0.11.0 和 tokio-core 0.1。 fn get_
我对此非常非常困惑。我有一台 Sony Vaio I7 笔记本电脑(bios 中有 Hyper-V 设置,所以我很容易启用 Hyper-V) 我想开发 Windows Phone 8。我已阅读 thi
我想使用 Hyper 来制作 HTTP 请求。调用 Client::get 工作正常,但其他方法,如 Client::post 和 Client::head 会导致编译错误。 extern crate
我们有一台大型服务器,上面运行着 Hyper-V 和一些虚拟机。我们的产品在虚拟机中的 Oracle DB 上运行时存在一些奇怪的问题,因此我们希望将 Oracle 直接安装到物理服务器上。我们可以限
如果你安装了win10系统,想做k8s集群,win10自带的hyper-v也是一个很适合的虚拟机环境。在上一节中,我们安装了两台虚拟机,这一节,都安装上docker。 master: 192.16
下面的教程在win2008与win2012 都可以 以上就完成了hyper-v虚拟机的创建工作。
第一步:Hyper-V创建虚拟交换机 第二步:虚拟机添加旧版网络适配器 好了,试下看可以上网了
Windows10安装Hyper-V后没有自动连接到本地计算机,手工连接失败,提示: 引用内容 尝试连接到服务器”DESKTOP-6P9L2HB”时出错。请检查虚拟机管理服务是否正在运行以及是否
我正在用 Hyper 编写一个“hello world”HTTP 服务器,但是我找不到 Server和 rt尝试导入模块时。 调用时cargo run ,然后我看到此错误消息: 26 | le
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
我正在创建一个PowerShell脚本以作为计划任务运行。目的是在每个星期一在我们的每个虚拟机上创建一个快照。我为每个VM创建了一个简短的脚本。与本地Hyper-V服务器建立远程 session 后,
我有一个带有 2 个 NIC 但只有一个 IP 地址的 Windows Server 2008 R2,我已经安装了 Hyper-V。在 Hyper-V 内部,我创建了 2 个虚拟机。 我设置了一个外部
我正在尝试在 Hyper Web 服务器中创建一个计数器来计算它收到的请求数。我正在使用 Arc>坚持下去。但是,我一直无法找出 move 的正确组合。和 .clone()以满足闭包的类型。下面是一些
我遇到了一个编译错误,我对 master 中的示例稍作修改后不太理解Hyper 的分支。给定以下代码: extern crate futures; extern crate hyper; use fu
我在运行添加了对 HTTP/2 协议(protocol)支持的 Python hyper 库时遇到问题。 在我的 Fedora 机器上,我使用 pip 和 pip3 安装它,以便将它与 Python
我是一名优秀的程序员,十分优秀!