-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
45 lines (38 loc) · 1013 Bytes
/
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
package utils
import (
"github.com/gorilla/sessions"
"log"
"math/rand"
"net/http"
"time"
)
var Store *sessions.FilesystemStore
func CheckErr(e error, msg string) {
if e != nil {
log.Printf("%s %s", msg, e)
}
}
func RandomString(strlen int) string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func InitSessionStore(SESSION_FILESTORE_URL, SESSION_SECRET_KEY string) *sessions.FilesystemStore {
Store = sessions.NewFilesystemStore(SESSION_FILESTORE_URL, []byte(SESSION_SECRET_KEY))
return Store
}
func GetStore() *sessions.FilesystemStore {
return Store
}
func GetSession(name string, w http.ResponseWriter, r *http.Request) (*sessions.Session, error) {
session, err := Store.Get(r, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil, err
}
return session, nil
}