forked from Debian/dcs
/
dcs-web.go
74 lines (65 loc) · 1.71 KB
/
dcs-web.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// vim:ts=4:sw=4:noexpandtab
package main
import (
"flag"
"fmt"
"github.com/Debian/dcs/cmd/dcs-web/common"
"github.com/Debian/dcs/cmd/dcs-web/health"
"github.com/Debian/dcs/cmd/dcs-web/index"
"github.com/Debian/dcs/cmd/dcs-web/search"
"github.com/Debian/dcs/cmd/dcs-web/show"
"github.com/Debian/dcs/cmd/dcs-web/varz"
"log"
"net/http"
"os"
"path/filepath"
"runtime/pprof"
)
var (
listenAddress = flag.String("listen_address",
":28080",
"listen address ([host]:port)")
memprofile = flag.String("memprofile", "", "Write memory profile to this file")
staticPath = flag.String("static_path",
"./static/",
"Path to static assets such as *.css")
)
func main() {
flag.Parse()
common.LoadTemplates()
fmt.Println("Debian Code Search webapp")
search.OpenTimingFiles()
health.StartChecking()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Check if a static file was requested with full name
name := filepath.Join(*staticPath, r.URL.Path)
if _, err := os.Stat(name); err == nil {
http.ServeFile(w, r, name)
return
}
// Or maybe /faq, which resolves to /faq.html
name = name + ".html"
if _, err := os.Stat(name); err == nil {
http.ServeFile(w, r, name)
return
}
index.Index(w, r)
})
http.HandleFunc("/favicon.ico", http.NotFound)
http.HandleFunc("/varz", varz.Varz)
http.HandleFunc("/search", search.Search)
http.HandleFunc("/show", show.Show)
http.HandleFunc("/memprof", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("writing memprof")
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}