aboutsummaryrefslogtreecommitdiff
path: root/fs_example_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-09 18:52:23 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-12-09 18:52:23 +0200
commitebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6 (patch)
tree5aae67a81adc2a51f77c7cc5b6e23afc8300463e /fs_example_test.go
parentAdded support for If-Modified-Since to SendFile and FS (diff)
downloadfasthttp-ebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6.tar.gz
fasthttp-ebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6.tar.bz2
fasthttp-ebf9968b68ee45cd8af53f3b2c7d141cc8f29cc6.zip
Renamed fshandler to fs
Diffstat (limited to 'fs_example_test.go')
-rw-r--r--fs_example_test.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/fs_example_test.go b/fs_example_test.go
new file mode 100644
index 0000000..dba4670
--- /dev/null
+++ b/fs_example_test.go
@@ -0,0 +1,47 @@
+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)
+ }
+}