-
Notifications
You must be signed in to change notification settings - Fork 11
/
middleware.go
executable file
·46 lines (38 loc) · 1.12 KB
/
middleware.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
package celeritas
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/justinas/nosurf"
)
func (c *Celeritas) SessionLoad(next http.Handler) http.Handler {
return c.Session.LoadAndSave(next)
}
func (c *Celeritas) NoSurf(next http.Handler) http.Handler {
csrfHandler := nosurf.New(next)
secure, _ := strconv.ParseBool(c.config.cookie.secure)
csrfHandler.ExemptGlob("/api/*")
csrfHandler.SetBaseCookie(http.Cookie{
HttpOnly: true,
Path: "/",
Secure: secure,
SameSite: http.SameSiteStrictMode,
Domain: c.config.cookie.domain,
})
return csrfHandler
}
func (c *Celeritas) CheckForMaintenanceMode(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if maintenanceMode {
if !strings.Contains(r.URL.Path, "/public/maintenance.html") {
w.WriteHeader(http.StatusServiceUnavailable)
w.Header().Set("Retry-After:", "300")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
http.ServeFile(w, r, fmt.Sprintf("%s/public/maintenance.html", c.RootPath))
return
}
}
next.ServeHTTP(w, r)
})
}