~ubuntu-branches/ubuntu/utopic/hockeypuck/utopic-proposed

« back to all changes in this revision

Viewing changes to build/src/github.com/hockeypuck/hockeypuck/router.go

  • Committer: Package Import Robot
  • Author(s): Casey Marshall
  • Date: 2014-04-13 20:06:01 UTC
  • Revision ID: package-import@ubuntu.com-20140413200601-oxdlqn1gy0x8m55u
Tags: 1.0~rel20140413+7a1892a~trusty
Hockeypuck 1.0 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Hockeypuck - OpenPGP key server
 
3
   Copyright (C) 2012-2014  Casey Marshall
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
package hockeypuck
 
19
 
 
20
import (
 
21
        "flag"
 
22
        "go/build"
 
23
        "net/http"
 
24
        "os"
 
25
        "path/filepath"
 
26
 
 
27
        "code.google.com/p/gorilla/mux"
 
28
)
 
29
 
 
30
// System installed location for static files.
 
31
const INSTALL_WEBROOT = "/var/lib/hockeypuck/www"
 
32
 
 
33
// Hockeypuck package, used to locate static files when running from source.
 
34
const HOCKEYPUCK_PKG = "github.com/hockeypuck/hockeypuck" // Any way to introspect?
 
35
 
 
36
// Response for HTTP 500.
 
37
const APPLICATION_ERROR = "APPLICATION ERROR"
 
38
 
 
39
// Response for HTTP 400.
 
40
const BAD_REQUEST = "BAD REQUEST"
 
41
 
 
42
// Path to Hockeypuck's installed www directory
 
43
func init() {
 
44
        flag.String("webroot", "",
 
45
                "Location of static web server files and templates")
 
46
}
 
47
func (s *Settings) Webroot() string {
 
48
        webroot := s.GetString("webroot")
 
49
        if webroot != "" {
 
50
                return webroot
 
51
        }
 
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() {
 
57
                        webroot = try_webroot
 
58
                }
 
59
        }
 
60
        s.Set("webroot", webroot)
 
61
        return webroot
 
62
}
 
63
 
 
64
// StaticRouter configures HTTP request handlers for static media files.
 
65
type StaticRouter struct {
 
66
        *mux.Router
 
67
}
 
68
 
 
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}
 
72
        sr.HandleAll()
 
73
        return sr
 
74
}
 
75
 
 
76
// HandleAll sets up all request handlers for Hockeypuck static media.
 
77
func (sr *StaticRouter) HandleAll() {
 
78
        sr.HandleMainPage()
 
79
        sr.HandleFonts()
 
80
        sr.HandleCss()
 
81
}
 
82
 
 
83
// HandleMainPage handles the "/" top-level request.
 
84
func (sr *StaticRouter) HandleMainPage() {
 
85
        sr.HandleFunc("/",
 
86
                func(resp http.ResponseWriter, req *http.Request) {
 
87
                        http.Redirect(resp, req, "/openpgp/lookup", http.StatusMovedPermanently)
 
88
                })
 
89
}
 
90
 
 
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)
 
99
                                return
 
100
                        }
 
101
                        http.ServeFile(resp, req, path)
 
102
                })
 
103
}
 
104
 
 
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)
 
113
                                return
 
114
                        }
 
115
                        http.ServeFile(resp, req, path)
 
116
                })
 
117
}