-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
60 lines (52 loc) · 1.28 KB
/
utils.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
54
55
56
57
58
59
60
package main
import (
"errors"
"fmt"
"github.com/anuragdhingra/lets-chat/data"
"golang.org/x/crypto/bcrypt"
"html/template"
"log"
"net/http"
)
func generateHTML(writer http.ResponseWriter, data interface{}, filenames ...string) {
var files []string
for _, file := range filenames {
files = append(files, fmt.Sprintf("templates/%s.html", file))
}
templates := template.Must(template.ParseFiles(files...))
templates.ExecuteTemplate(writer,"layout", data)
}
func throwError(err error) {
if err != nil {
log.Print(err)
return
}
}
func encryptPassword(password string) (encryptedPass string) {
bytePass := []byte(password)
encryptedPassword, err := bcrypt.GenerateFromPassword(bytePass, bcrypt.MinCost)
throwError(err)
encryptedPass = string(encryptedPassword)
return string(encryptedPass)
}
func session(w http.ResponseWriter, r *http.Request) (session data.Session, err error) {
cookie, err := r.Cookie("_cookie")
if err != nil {
log.Print(err)
return
} else {
session = data.Session{Uuid:cookie.Value}
ok,_ := session.Check()
if !ok {
err = errors.New("Invalid session")
}
}
return
}
func checkInvalidRequests(w http.ResponseWriter, r *http.Request) {
_, err := session(w, r)
if err == nil {
http.Redirect(w, r, `/`, http.StatusFound)
return
}
}