forked from SimePel/asu-billing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
53 lines (45 loc) · 1.38 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
47
48
49
50
51
52
53
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/julienschmidt/httprouter"
)
func userAuthCheck(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
session, _ := store.Get(r, "user")
if session.Values["user_logged"] == "false" || session.Values["user_logged"] == nil {
http.Redirect(w, r, "/user-login", http.StatusFound)
return
}
h(w, r, ps)
}
}
func adminAuthCheck(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
session, _ := store.Get(r, "admin")
if session.Values["admin_logged"] == "false" || session.Values["admin_logged"] == nil {
http.Redirect(w, r, "/admin-login", http.StatusFound)
return
}
h(w, r, ps)
}
}
func accessLog(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
since := time.Now()
h(w, r, ps)
var f *os.File
f, err := os.OpenFile("logs/access.log", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
f, err = os.Create("logs/access.log")
if err != nil {
log.Printf("could not create file. skipping: %v", err)
return
}
}
fmt.Fprintf(f, "%v. Host: %v. Request: %v. Method: %v. Lead time: %v\n", time.Now().Format(time.UnixDate), r.RemoteAddr, r.RequestURI, r.Method, time.Now().Sub(since))
}
}