summaryrefslogtreecommitdiffstats
path: root/roseh.moe.go
diff options
context:
space:
mode:
Diffstat (limited to 'roseh.moe.go')
-rw-r--r--roseh.moe.go43
1 files changed, 18 insertions, 25 deletions
diff --git a/roseh.moe.go b/roseh.moe.go
index cf5627c..206679b 100644
--- a/roseh.moe.go
+++ b/roseh.moe.go
@@ -149,12 +149,12 @@ var (
outlineTemplate = template.Must(template.New("outline").Parse(outlineString))
)
-func outlinedTemplate(s string, funcMap template.FuncMap) (*template.Template, error) {
+func outlinedTemplate(s string) (*template.Template, error) {
outline, err := outlineTemplate.Clone()
if err != nil {
return nil, err
}
- t, err := outline.New("body").Funcs(funcMap).Parse(s)
+ t, err := outline.New("body").Parse(s)
if err != nil {
return nil, err
}
@@ -180,7 +180,7 @@ func notFound(w http.ResponseWriter, r *http.Request) {
var (
//go:embed templates/index.html.template
indexString string
- indexTemplate = template.Must(outlinedTemplate(indexString, nil))
+ indexTemplate = template.Must(outlinedTemplate(indexString))
)
func index(w http.ResponseWriter, _ *http.Request) {
@@ -192,7 +192,7 @@ func index(w http.ResponseWriter, _ *http.Request) {
var (
//go:embed templates/games/index.html.template
gamesIndexString string
- gamesIndexTemplate = template.Must(outlinedTemplate(gamesIndexString, nil))
+ gamesIndexTemplate = template.Must(outlinedTemplate(gamesIndexString))
)
func gamesIndex(w http.ResponseWriter, _ *http.Request) {
@@ -216,7 +216,7 @@ func pong(w http.ResponseWriter, _ *http.Request) {
var (
//go:embed templates/games/rms.html.template
stallmanShooterString string
- stallmanShooterTemplate = template.Must(outlinedTemplate(stallmanShooterString, nil))
+ stallmanShooterTemplate = template.Must(outlinedTemplate(stallmanShooterString))
)
func stallmanShooter(w http.ResponseWriter, _ *http.Request) {
@@ -269,7 +269,7 @@ func qrHandler(w http.ResponseWriter, r *http.Request) {
var (
//go:embed templates/wormhole.html.template
wormholeTemplateString string
- wormholeTemplate = template.Must(outlinedTemplate(wormholeTemplateString, nil))
+ wormholeTemplate = template.Must(outlinedTemplate(wormholeTemplateString))
)
type wormholeTemplateArgs struct {
@@ -477,7 +477,7 @@ func uploadWormhole(reader *multipart.Reader) (string, error) {
var (
//go:embed templates/wormhole-success.html.template
wormholeSuccessTemplateString string
- wormholeSuccessTemplate = template.Must(outlinedTemplate(wormholeSuccessTemplateString, nil))
+ wormholeSuccessTemplate = template.Must(outlinedTemplate(wormholeSuccessTemplateString))
)
type wormholeSuccessTemplateArgs struct {
@@ -716,23 +716,16 @@ func checkPassword(password, auth string) bool {
var (
//go:embed templates/login.html.template
loginTemplateString string
- loginTemplate = template.Must(outlinedTemplate(loginTemplateString, template.FuncMap{
- "selfURL": func() string { return *selfURL },
- }))
+ loginTemplate = template.Must(outlinedTemplate(loginTemplateString))
)
type loginTemplateArgs struct {
- Redirect string
- Error bool
+ Error bool
}
-func unauthenticated(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(http.StatusUnauthorized)
- redirect := r.URL.String()
+func redirectLogin(w http.ResponseWriter, r *http.Request, redirect string) {
redirect += base64.RawURLEncoding.EncodeToString(mac([]byte(redirect), "redirect"))
- if err := loginTemplate.Execute(w, loginTemplateArgs{Redirect: redirect}); err != nil {
- log.Printf("Warning: login: %s", err)
- }
+ http.Redirect(w, r, *selfURL+"/login?redirect="+url.QueryEscape(redirect), http.StatusFound)
}
func serveLogin(w http.ResponseWriter, r *http.Request) {
@@ -759,7 +752,7 @@ func verifyRedirect(redirect string) (string, bool) {
func login(w http.ResponseWriter, r *http.Request) {
if !checkPassword(r.FormValue("password"), r.FormValue("auth")) {
- if err := loginTemplate.Execute(w, loginTemplateArgs{Redirect: r.FormValue("redirect"), Error: true}); err != nil {
+ if err := loginTemplate.Execute(w, loginTemplateArgs{Error: true}); err != nil {
log.Printf("Warning: login: %s", err)
}
return
@@ -775,7 +768,7 @@ func login(w http.ResponseWriter, r *http.Request) {
var (
//go:embed templates/note.html.template
notepadString string
- notepadTemplate = template.Must(outlinedTemplate(notepadString, nil))
+ notepadTemplate = template.Must(outlinedTemplate(notepadString))
notepadContentsMu sync.Mutex
notepadContents string
@@ -787,7 +780,7 @@ type notepadTemplateArgs struct {
func notepad(w http.ResponseWriter, r *http.Request) {
if !cookieAuth(w, r) {
- unauthenticated(w, r)
+ redirectLogin(w, r, "/notepad")
return
}
notepadContentsMu.Lock()
@@ -840,7 +833,7 @@ func cmd(w http.ResponseWriter, r *http.Request) {
var (
//go:embed templates/pkg-index.html.template
pkgIndexString string
- pkgIndexTemplate = template.Must(outlinedTemplate(pkgIndexString, nil))
+ pkgIndexTemplate = template.Must(outlinedTemplate(pkgIndexString))
)
type pkgIndexTemplateArgs struct {
@@ -862,7 +855,7 @@ func pkgIndex(w http.ResponseWriter, r *http.Request) {
var (
//go:embed templates/cmd-index.html.template
cmdIndexString string
- cmdIndexTemplate = template.Must(outlinedTemplate(cmdIndexString, nil))
+ cmdIndexTemplate = template.Must(outlinedTemplate(cmdIndexString))
)
type cmdIndexTemplateArgs struct {
@@ -906,7 +899,7 @@ type nezukoHandler struct {
func (h *nezukoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !cookieAuth(w, r) {
- unauthenticated(w, r)
+ redirectLogin(w, r, r.URL.Path)
return
}
h.srv.ServeHTTP(w, r)
@@ -918,7 +911,7 @@ type jellyfinReverseProxy struct {
func (p *jellyfinReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !cookieAuth(w, r) {
- unauthenticated(w, r)
+ redirectLogin(w, r, "https://cinema.roseh.moe")
return
}
p.proxy.ServeHTTP(w, r)