1 Star 0 Fork 0

ap6108/fasthttp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
fs_handler_example_test.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
Aliaksandr Valialkin 提交于 2015-12-29 14:23 . Added an example for FS
package fasthttp_test
import (
"bytes"
"log"
"github.com/valyala/fasthttp"
)
// Setup file handlers (aka 'file server config')
var (
// Handler for serving images from /img/ path,
// i.e. /img/foo/bar.jpg will be served from
// /var/www/images/foo/bar.jpb .
imgPrefix = []byte("/img/")
imgHandler = fasthttp.FSHandler("/var/www/images", 1)
// Handler for serving css from /static/css/ path,
// i.e. /static/css/foo/bar.css will be served from
// /home/dev/css/foo/bar.css .
cssPrefix = []byte("/static/css/")
cssHandler = fasthttp.FSHandler("/home/dev/css", 2)
// Handler for serving the rest of requests,
// i.e. /foo/bar/baz.html will be served from
// /var/www/files/foo/bar/baz.html .
filesHandler = fasthttp.FSHandler("/var/www/files", 0)
)
// Main request handler
func requestHandler(ctx *fasthttp.RequestCtx) {
path := ctx.Path()
switch {
case bytes.HasPrefix(path, imgPrefix):
imgHandler(ctx)
case bytes.HasPrefix(path, cssPrefix):
cssHandler(ctx)
default:
filesHandler(ctx)
}
}
func ExampleFSHandler() {
if err := fasthttp.ListenAndServe(":80", requestHandler); err != nil {
log.Fatalf("Error in server: %s", err)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ap6108/fasthttp.git
git@gitee.com:ap6108/fasthttp.git
ap6108
fasthttp
fasthttp
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385