Initial commit

This commit is contained in:
paul 2018-04-22 16:55:50 +02:00
commit 2422e3108f
37 changed files with 12691 additions and 0 deletions

26
middleware/panic.go Normal file
View 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)
}

View 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)
}
}