-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
67 lines (57 loc) · 1.61 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
61
62
63
64
65
66
67
package utils
import (
"fmt"
"html/template"
"net/http"
"github.com/go-chi/chi"
"github.com/jinzhu/gorm"
"github.com/microcosm-cc/bluemonday"
"github.com/qor/l10n"
"github.com/qor/qor/utils"
"github.com/qor/session"
"github.com/qor/session/manager"
"github.com/requaos/qorfun/config/auth"
"github.com/requaos/qorfun/config/db"
"github.com/requaos/qorfun/models/users"
)
// GetCurrentUser get current user from request
func GetCurrentUser(req *http.Request) *users.User {
if currentUser, ok := auth.Auth.GetCurrentUser(req).(*users.User); ok {
return currentUser
}
return nil
}
// GetCurrentLocale get current locale from request
func GetCurrentLocale(req *http.Request) string {
locale := l10n.Global
if cookie, err := req.Cookie("locale"); err == nil {
locale = cookie.Value
}
return locale
}
// GetDB get DB from request
func GetDB(req *http.Request) *gorm.DB {
if db := utils.GetDBFromRequest(req); db != nil {
return db
}
return db.DB
}
// URLParam get url params from request
func URLParam(name string, req *http.Request) string {
return chi.URLParam(req, name)
}
// AddFlashMessage helper
func AddFlashMessage(w http.ResponseWriter, req *http.Request, message string, mtype string) error {
return manager.SessionManager.Flash(w, req, session.Message{Message: template.HTML(message), Type: mtype})
}
// HTMLSanitizer HTML sanitizer
var HTMLSanitizer = bluemonday.UGCPolicy()
func FormatPrice(price interface{}) string {
switch price.(type) {
case float32, float64:
return fmt.Sprintf("%0.2f", price)
case int, uint, int32, int64, uint32, uint64:
return fmt.Sprintf("%d.00", price)
}
return ""
}