-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter.go
117 lines (100 loc) · 3.42 KB
/
router.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Hockeypuck - OpenPGP key server
Copyright (C) 2012-2014 Casey Marshall
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hockeypuck
import (
"flag"
"go/build"
"net/http"
"os"
"path/filepath"
"code.google.com/p/gorilla/mux"
)
// System installed location for static files.
const INSTALL_WEBROOT = "/var/lib/hockeypuck/www"
// Hockeypuck package, used to locate static files when running from source.
const HOCKEYPUCK_PKG = "launchpad.net/hockeypuck" // Any way to introspect?
// Response for HTTP 500.
const APPLICATION_ERROR = "APPLICATION ERROR"
// Response for HTTP 400.
const BAD_REQUEST = "BAD REQUEST"
// Path to Hockeypuck's installed www directory
func init() {
flag.String("webroot", "",
"Location of static web server files and templates")
}
func (s *Settings) Webroot() string {
webroot := s.GetString("webroot")
if webroot != "" {
return webroot
}
if fi, err := os.Stat(INSTALL_WEBROOT); err == nil && fi.IsDir() {
webroot = INSTALL_WEBROOT
} else if p, err := build.Default.Import(HOCKEYPUCK_PKG, "", build.FindOnly); err == nil {
try_webroot := filepath.Join(p.Dir, "instroot", INSTALL_WEBROOT)
if fi, err := os.Stat(try_webroot); err == nil && fi.IsDir() {
webroot = try_webroot
}
}
s.Set("webroot", webroot)
return webroot
}
// StaticRouter configures HTTP request handlers for static media files.
type StaticRouter struct {
*mux.Router
}
// NewStaticRouter constructs a new static media router and sets up all request handlers.
func NewStaticRouter(r *mux.Router) *StaticRouter {
sr := &StaticRouter{Router: r}
sr.HandleAll()
return sr
}
// HandleAll sets up all request handlers for Hockeypuck static media.
func (sr *StaticRouter) HandleAll() {
sr.HandleMainPage()
sr.HandleFonts()
sr.HandleCss()
}
// HandleMainPage handles the "/" top-level request.
func (sr *StaticRouter) HandleMainPage() {
sr.HandleFunc("/",
func(resp http.ResponseWriter, req *http.Request) {
http.Redirect(resp, req, "/openpgp/lookup", http.StatusMovedPermanently)
})
}
// HandleFonts handles all embedded web font requests.
func (sr *StaticRouter) HandleFonts() {
sr.HandleFunc(`/fonts/{filename:.*\.ttf}`,
func(resp http.ResponseWriter, req *http.Request) {
filename := mux.Vars(req)["filename"]
path := filepath.Join(Config().Webroot(), "fonts", filename)
if stat, err := os.Stat(path); err != nil || stat.IsDir() {
http.NotFound(resp, req)
return
}
http.ServeFile(resp, req, path)
})
}
// HandleCSS handles all embedded cascading style sheet (CSS) requests.
func (sr *StaticRouter) HandleCss() {
sr.HandleFunc(`/css/{filename:.*\.css}`,
func(resp http.ResponseWriter, req *http.Request) {
filename := mux.Vars(req)["filename"]
path := filepath.Join(Config().Webroot(), "css", filename)
if stat, err := os.Stat(path); err != nil || stat.IsDir() {
http.NotFound(resp, req)
return
}
http.ServeFile(resp, req, path)
})
}