2
Hockeypuck - OpenPGP key server
3
Copyright (C) 2012-2014 Casey Marshall
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Affero General Public License as published by
7
the Free Software Foundation, version 3.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Affero General Public License for more details.
14
You should have received a copy of the GNU Affero General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
27
"code.google.com/p/gorilla/mux"
30
// System installed location for static files.
31
const INSTALL_WEBROOT = "/var/lib/hockeypuck/www"
33
// Hockeypuck package, used to locate static files when running from source.
34
const HOCKEYPUCK_PKG = "github.com/hockeypuck/hockeypuck" // Any way to introspect?
36
// Response for HTTP 500.
37
const APPLICATION_ERROR = "APPLICATION ERROR"
39
// Response for HTTP 400.
40
const BAD_REQUEST = "BAD REQUEST"
42
// Path to Hockeypuck's installed www directory
44
flag.String("webroot", "",
45
"Location of static web server files and templates")
47
func (s *Settings) Webroot() string {
48
webroot := s.GetString("webroot")
52
if fi, err := os.Stat(INSTALL_WEBROOT); err == nil && fi.IsDir() {
53
webroot = INSTALL_WEBROOT
54
} else if p, err := build.Default.Import(HOCKEYPUCK_PKG, "", build.FindOnly); err == nil {
55
try_webroot := filepath.Join(p.Dir, "instroot", INSTALL_WEBROOT)
56
if fi, err := os.Stat(try_webroot); err == nil && fi.IsDir() {
60
s.Set("webroot", webroot)
64
// StaticRouter configures HTTP request handlers for static media files.
65
type StaticRouter struct {
69
// NewStaticRouter constructs a new static media router and sets up all request handlers.
70
func NewStaticRouter(r *mux.Router) *StaticRouter {
71
sr := &StaticRouter{Router: r}
76
// HandleAll sets up all request handlers for Hockeypuck static media.
77
func (sr *StaticRouter) HandleAll() {
83
// HandleMainPage handles the "/" top-level request.
84
func (sr *StaticRouter) HandleMainPage() {
86
func(resp http.ResponseWriter, req *http.Request) {
87
http.Redirect(resp, req, "/openpgp/lookup", http.StatusMovedPermanently)
91
// HandleFonts handles all embedded web font requests.
92
func (sr *StaticRouter) HandleFonts() {
93
sr.HandleFunc(`/fonts/{filename:.*\.ttf}`,
94
func(resp http.ResponseWriter, req *http.Request) {
95
filename := mux.Vars(req)["filename"]
96
path := filepath.Join(Config().Webroot(), "fonts", filename)
97
if stat, err := os.Stat(path); err != nil || stat.IsDir() {
98
http.NotFound(resp, req)
101
http.ServeFile(resp, req, path)
105
// HandleCSS handles all embedded cascading style sheet (CSS) requests.
106
func (sr *StaticRouter) HandleCss() {
107
sr.HandleFunc(`/css/{filename:.*\.css}`,
108
func(resp http.ResponseWriter, req *http.Request) {
109
filename := mux.Vars(req)["filename"]
110
path := filepath.Join(Config().Webroot(), "css", filename)
111
if stat, err := os.Stat(path); err != nil || stat.IsDir() {
112
http.NotFound(resp, req)
115
http.ServeFile(resp, req, path)