/
routesTool.go
137 lines (111 loc) · 3.98 KB
/
routesTool.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package routes
import (
"html/template"
"net/http"
"github.com/MaksymPrymierov/blog/db/users"
"github.com/MaksymPrymierov/blog/models"
"github.com/MaksymPrymierov/blog/session"
"github.com/MaksymPrymierov/blog/utils"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"gopkg.in/mgo.v2/bson"
"labix.org/v2/mgo"
)
/* Global variables */
var postsCollection *mgo.Collection // Variable for posts
var usersTables *mgo.Collection // Variable for user
var inMemorySession *session.Session // Variable for session
/* Global const */
const (
COOKIE_NAME = "sessionId" // Const for cookie name
)
/* Init default server data */
func Init() *martini.ClassicMartini {
/* Init session */
inMemorySession = session.NewSession()
/* Connect to database */
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
/* Connect collection from database */
postsCollection = session.DB("blog").C("posts")
usersTables = session.DB("blog").C("users")
/* Init martini framework */
m := martini.Classic()
/* Init martini render */
unescapeFuncMap := template.FuncMap{"unescape": unescape, "checkGroup": checkGroup}
m.Use(render.Renderer(render.Options{
Directory: "templates", // Specify what path to load the templates from.
Layout: "layout", // Specify a layout template. Layouts can call {{ yield }} to render the current template.
Extensions: []string{".tmpl", ".html"}, // Specify extensions to load for templates.
Funcs: []template.FuncMap{unescapeFuncMap}, // Specify helper function maps for templates to access.
Charset: "UTF-8", // Sets encoding for json and html content-types. Default is "UTF-8".
IndentJSON: true, // Output human readable JSON
}))
return m
}
/*
func UpdateUserPermission(rnd render.Render) {
thisUser := users.UsersTable{}
usersTables.FindId("connor41").One(&thisUser)
oldUser := thisUser
thisUser.Permission = "admin"
usersTables.Update(oldUser, thisUser)
rnd.Redirect("http://google.com")
}
*/
/* Function return id user of active current session */
func getCurrentUserId(r *http.Request) string {
/* Get user name of current session */
cookie, _ := r.Cookie(COOKIE_NAME)
var username string
if cookie != nil {
username = inMemorySession.Get(cookie.Value)
}
/* Convert username on userId */
userId := utils.GenerateNameId(username)
return userId
}
/* Func return all user data on id and error */
func getPrivateUserData(userId string) (models.Users, error) {
/* Init type Users */
var privateUserData models.Users
/* Get and check user on id */
user := users.UsersTable{}
err := usersTables.FindId(userId).One(&user)
if err != nil {
return privateUserData, err
}
/* Init all user data */
privateUserData = models.Users{userId, user.Username, user.Email, user.Password, user.Permission}
return privateUserData, nil
}
/* Func return public user data on id and error */
func getPublicUserData(userId string) (models.PublicUsersData, error) {
/* Init type PublicUsersData */
var publicUserData models.PublicUsersData
/* Get and check user on id */
user := users.UsersTable{}
err := usersTables.FindId(userId).One(&user)
if err != nil {
return publicUserData, err
}
/* Init public user data */
publicUserData = models.PublicUsersData{userId, user.Username, user.Permission}
return publicUserData, nil
}
/* Function return all information about user current session */
func getPublicCurrentUserData(r *http.Request) (models.PublicUsersData, error) {
return getPublicUserData(getCurrentUserId(r))
}
/* Function return public information about user current session */
func getPrivateCurrentUserData(r *http.Request) (models.Users, error) {
return getPrivateUserData(getCurrentUserId(r))
}
/* Find user of custom data */
func findUserOfData(typeData, data string) (interface{}, error) {
result := users.UsersTable{}
err := usersTables.Find(bson.M{typeData: data}).One(&result)
return result, err
}