Initial commit
This commit is contained in:
commit
2422e3108f
37 changed files with 12691 additions and 0 deletions
middleware
26
middleware/panic.go
Normal file
26
middleware/panic.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"git.klink.asia/paul/certman/handlers"
|
||||
)
|
||||
|
||||
// Recoverer Listens for panic() calls and logs the stacktrace.
|
||||
func Recoverer(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rvr := recover(); rvr != nil {
|
||||
log.Println(rvr)
|
||||
log.Println(string(debug.Stack()))
|
||||
handlers.ErrorHandler(w, r)
|
||||
}
|
||||
}()
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
22
middleware/requirelogin.go
Normal file
22
middleware/requirelogin.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.klink.asia/paul/certman/services"
|
||||
)
|
||||
|
||||
// RequireLogin is a middleware that checks for a username in the active
|
||||
// session, and redirects to `/login` if no username was found.
|
||||
func RequireLogin(sessions *services.Sessions) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, req *http.Request) {
|
||||
if username := sessions.GetUsername(req); username == "" {
|
||||
http.Redirect(w, req, "/login", http.StatusFound)
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue