aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/middleware.go
blob: 991aa8ca4c9274aa7d079ec0cf21425a261bc69b (plain) (blame)
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
package main

import (
	"net/http"
	"time"

	"riedstra.dev/mitch/go-website/page"
)

func (a *App) Err5xx(w http.ResponseWriter, r *http.Request,
	statusCode int, title, desc string) {
	page.Render5xx(w, r, map[string]interface{}{
		"Error":       title,
		"Description": desc,
	}, statusCode)
}

func (a *App) Err500Default(w http.ResponseWriter, r *http.Request) {
	a.Err5xx(w, r, http.StatusInternalServerError, "Internal server error",
		"Internal server error.")
}

func (a *App) LogoutHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.SetCookie(w, &http.Cookie{
			Name:     "Auth",
			HttpOnly: a.auth.HTTPOnly,
			SameSite: a.auth.SameSiteStrict,
			Secure:   a.auth.Secure,
			Value:    "logout",
			Expires:  time.Now().Add(time.Second),
		})

		http.Redirect(w, r, "/", http.StatusFound)
	})
}