-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.go
More file actions
47 lines (41 loc) · 1.21 KB
/
serve.go
File metadata and controls
47 lines (41 loc) · 1.21 KB
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
package httpstaticd
import (
"fmt"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
)
var (
code int = http.StatusTemporaryRedirect
handler http.Handler
)
func Serve(directory string, listings bool, listenPort int, enableCors bool, accessLog bool) {
log.Info("initialize httpstaticd")
log.WithFields(log.Fields{
"dir": directory,
"port": listenPort,
}).Info("listening for requests")
if listings {
// TODO: support dir listing when index file exists
log.Info("directory listings enabled")
http.Handle("/", handlers.LoggingHandler(os.Stdout, http.FileServer(http.Dir(directory))))
http.HandleFunc("/health", healthCheckHandler)
handler = nil
} else {
fileServer := http.FileServer(neuteredFileSystem{http.Dir(directory)})
mux := http.NewServeMux()
mux.Handle("/", http.StripPrefix("/", handlers.LoggingHandler(os.Stdout, fileServer)))
mux.HandleFunc("/health", healthCheckHandler)
if enableCors {
log.Info("cors support enabled")
handler = cors.Default().Handler(mux)
} else {
handler = mux
}
}
if err := http.ListenAndServe(":"+fmt.Sprintf("%v", listenPort), handler); err != nil {
log.Fatalf("fatality: %v", err)
}
}