Skip to content

Commit 0bb5002

Browse files
committed
add redirect
1 parent 12d0a75 commit 0bb5002

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

adminifier/adminifier.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func Configure() {
4343
*ptr = str
4444
}
4545

46+
if !strings.HasPrefix(root, "/") {
47+
root = "/" + root
48+
}
4649
if !strings.HasSuffix(root, "/") {
4750
root += "/"
4851
}
@@ -69,12 +72,6 @@ func Configure() {
6972
mux.RegisterFunc(host+root, "adminifier root", handleRoot)
7073
log.Println("registered adminifier root: " + host + root)
7174

72-
// template handlers
73-
// FIXME: I think this is unused and can be removed
74-
for _, tmplName := range tmplHandlers {
75-
mux.RegisterFunc(host+root+tmplName, "standalone template", handleTemplate)
76-
}
77-
7875
// admin handlers
7976
setupAdminHandlers()
8077

adminifier/handlers.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/fs"
66
"log"
77
"net/http"
8+
"net/url"
89
"strings"
910

1011
"github.com/cooper/quiki/authenticator"
@@ -196,7 +197,8 @@ func handleLoginPage(w http.ResponseWriter, r *http.Request) {
196197
return
197198
}
198199

199-
handleTemplate(w, r)
200+
r.ParseForm()
201+
handleTemplate(w, r, struct{ Redirect string }{r.Form.Get("redirect")})
200202
}
201203

202204
func handleLogin(w http.ResponseWriter, r *http.Request) {
@@ -219,10 +221,11 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
219221
// start session and remember user info
220222
sessMgr.Put(r.Context(), "user", &user)
221223
sessMgr.Put(r.Context(), "loggedIn", true)
222-
sessMgr.Put(r.Context(), "branch", "master")
224+
sessMgr.Put(r.Context(), "branch", "master") // FIXME: derive default branch
223225

224226
// redirect to dashboard, which is now located at adminifier root
225-
http.Redirect(w, r, "../", http.StatusTemporaryRedirect)
227+
redirect := r.Form.Get("redirect")
228+
http.Redirect(w, r, root+redirect, http.StatusTemporaryRedirect)
226229
}
227230

228231
func handleCreateUserPage(w http.ResponseWriter, r *http.Request) {
@@ -233,7 +236,7 @@ func handleCreateUserPage(w http.ResponseWriter, r *http.Request) {
233236
return
234237
}
235238

236-
handleTemplate(w, r)
239+
handleTemplate(w, r, nil)
237240
}
238241

239242
func handleCreateUser(w http.ResponseWriter, r *http.Request) {
@@ -356,7 +359,11 @@ func parsePost(w http.ResponseWriter, r *http.Request, required ...string) bool
356359
func redirectIfNotLoggedIn(w http.ResponseWriter, r *http.Request) bool {
357360
// if not logged in, temp redirect to login page
358361
if !sessMgr.GetBool(r.Context(), "loggedIn") {
359-
http.Redirect(w, r, root+"login", http.StatusTemporaryRedirect)
362+
redirect := "?redirect=" + strings.TrimPrefix(r.URL.Path, root)
363+
if r.URL.RawQuery != "" {
364+
redirect += url.QueryEscape("?" + r.URL.RawQuery)
365+
}
366+
http.Redirect(w, r, root+"login"+redirect, http.StatusTemporaryRedirect)
360367
return true
361368
}
362369
return false

adminifier/template.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package adminifier
22

33
import (
4+
"log"
45
"net/http"
56
"strings"
6-
)
77

8-
// handlers that go straight to templates
9-
var tmplHandlers = []string{}
8+
"github.com/pkg/errors"
9+
)
1010

11-
func handleTemplate(w http.ResponseWriter, r *http.Request) {
11+
func handleTemplate(w http.ResponseWriter, r *http.Request, dot any) {
1212
relPath := strings.TrimPrefix(r.URL.Path, root)
13-
err := tmpl.ExecuteTemplate(w, relPath+".tpl", nil)
13+
err := tmpl.ExecuteTemplate(w, relPath+".tpl", dot)
1414
if err != nil {
15-
// TODO: internal server error
16-
panic(err)
15+
http.Error(w, err.Error(), http.StatusInternalServerError)
16+
log.Println(errors.Wrap(err, "execute template"))
1717
}
1818
}

build

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ if [ "$1" == "doc" ]; then
1010
done
1111
elif [ "$1" == "help" ]; then
1212
./quiki -wiki=help-wiki -force-gen
13-
cp -r help-wiki resources/adminifier/help
13+
mkdir -p resources/adminifier/help
14+
cp -r help-wiki/* resources/adminifier/help/
1415
elif [ "$1" == "test" ]; then
1516
./quiki -wiki=test -force-gen
1617
else

0 commit comments

Comments
 (0)