This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
155 lines (126 loc) · 4.06 KB
/
handlers.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SHIM - A web front end for the Hugo site generator
// Copyright (C) 2016 Cameron Conn
// 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, either version 3 of the License, or
// (at your option) any later version.
// 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 main
import (
"fmt"
"github.com/niemal/uman"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
const (
handleFiles = 0
handlePreview = 1
)
func loggingHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] %q\n", r.Method, r.URL.String())
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// Middleware for requiring login on certain pages.
type loginHandler struct {
handler http.Handler
userMan *uman.UserManager
}
// newLoginHandler Creates a new LoginHandler, which requires logins
// from all visiting users or redirects them to /login/
func newLoginHandler(um *uman.UserManager) *loginHandler {
return &loginHandler{
userMan: um,
}
}
// Simple syntax candy method to ensure that users are logged in whenever accessing
// protected Shim pages.
func (l *loginHandler) authHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := l.userMan.GetHTTPSession(w, r)
if session.IsLogged() {
h.ServeHTTP(w, r)
return
}
redirectTarget := fmt.Sprintf("%s/login/?redirect=%s%s&warn=yes",
shimAssets.basepath, shimAssets.basepath, url.QueryEscape(r.URL.String()))
log.Printf("Not logged in! Redirecting to %s\n", redirectTarget)
http.Redirect(w, r, redirectTarget, http.StatusSeeOther)
})
}
func (l *loginHandler) dynamicPreviewHandler(w http.ResponseWriter, r *http.Request, mode int) {
session := l.userMan.GetHTTPSession(w, r)
if !session.IsLogged() {
log.Println("not logged in!")
http.Error(w, "You must log in first!", http.StatusUnauthorized)
return
}
site := findUserSite(w, r)
if site == nil {
http.Error(w, "We don't know what site to show you... Sorry", http.StatusInternalServerError)
return
}
filename := strings.TrimLeft(r.URL.Path, "/")
var location http.Dir
if mode == handlePreview {
location = http.Dir(filepath.Join(site.Location, "preview"))
} else {
location = http.Dir(filepath.Join(site.Location, "static", "files"))
}
file, err := location.Open(filename)
if err != nil {
if file != nil {
file.Close()
}
log.Printf("Couldn't find preview at %s\n", location)
http.Error(w, "File not found! :(", http.StatusNotFound)
return
}
info, err := file.Stat()
if err != nil {
file.Close()
http.Error(w, "File not found", http.StatusNotFound)
} else if info.IsDir() {
file.Close()
file, err = location.Open(filepath.Join(filename, "index.html"))
if err != nil {
if os.IsNotExist(err) {
http.Error(w, "Could not find index file to serve", http.StatusNotFound)
} else {
http.Error(w, "Had an issue serving you the file: "+err.Error(), http.StatusInternalServerError)
}
}
}
if err != nil || file == nil {
return
}
http.ServeContent(w, r, info.Name(), info.ModTime(), file)
file.Close()
}
func panicHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from error: %v\n", r)
tmpWrapper := new(WebWrapper)
tmpWrapper.FailedMessage(fmt.Sprint(r))
tmpWrapper.Base = shimAssets.baseurl
tmpWrapper.URL = req.URL.String()
w.WriteHeader(http.StatusInternalServerError)
renderPage(w, "errorPage", tmpWrapper)
}
}()
h.ServeHTTP(w, req)
})
}