forked from ngmoco/falcore
/
server.go
50 lines (41 loc) · 1.1 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"flag"
"fmt"
"github.com/ngmoco/falcore"
"github.com/ngmoco/falcore/compression"
"github.com/ngmoco/falcore/static_file"
"net/http"
)
// Command line options
var (
port = flag.Int("port", 8000, "the port to listen on")
path = flag.String("base", "./test", "the path to serve files from")
)
func main() {
// parse command line options
flag.Parse()
// setup pipeline
pipeline := falcore.NewPipeline()
// upstream filters
// Serve index.html for root requests
pipeline.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
if req.HttpRequest.URL.Path == "/" {
req.HttpRequest.URL.Path = "/index.html"
}
return nil
}))
// Serve files
pipeline.Upstream.PushBack(&static_file.Filter{
BasePath: *path,
})
// downstream
pipeline.Downstream.PushBack(compression.NewFilter(nil))
// setup server
server := falcore.NewServer(*port, pipeline)
// start the server
// this is normally blocking forever unless you send lifecycle commands
if err := server.ListenAndServe(); err != nil {
fmt.Println("Could not start server:", err)
}
}