-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.go
137 lines (107 loc) · 2.91 KB
/
admin.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 main
import (
"encoding/json"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"html/template"
"net/http"
)
type User struct {
Name string `bson:"name"`
Password string `bson:"password"`
}
type UserRole struct {
Rolename string `bson:"rolename"`
Approle string `bson:"approle"`
}
var testuri = "mongodb://pitsch:[email protected]:29051/pitsch_test"
var templates = template.Must(template.ParseFiles("login.html", "view.html"))
//Setting up the database variables
var (
mgoSession *mgo.Session
databaseName = "pitsch_test"
)
//Only get the copy of the session
func getSession() *mgo.Session {
if mgoSession == nil {
var err error
mgoSession, err = mgo.Dial(testuri)
if err != nil {
fmt.Println("Error connecting to Mongo: ", err)
}
}
return mgoSession.Copy()
}
//var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
func loadUser(user string) User {
session := getSession()
defer session.Close()
users := session.DB("pitsch_test").C("users")
userresult := User{}
err := users.Find(bson.M{"name": user}).One(&userresult)
if err != nil {
fmt.Println("No such user: ")
}
return userresult
}
//Example of passing reference of the user type
func saveUser(u User) {
session := getSession()
defer session.Close()
c := session.DB("pitsch_test").C("users")
err := c.Insert(&User{u.Name, u.Password})
if err != nil {
fmt.Println("Error while trying to save to Mongo.")
}
}
func createUserApi(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
u := User{}
err := decoder.Decode(&u)
if err != nil {
fmt.Println("Error trying to decode JSON request for create user.")
}
saveUser(u)
}
func getUserApi(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
u := User{}
err := decoder.Decode(&u)
if err != nil {
fmt.Println("Error trying to decode JSON request for get user.")
}
u = loadUser(u.Name)
res, err := json.Marshal(u)
if err != nil {
fmt.Println("Error trying to create JSON response")
}
w.Header().Set("Content-Type", "application/json")
w.Write(res)
}
func saveUserHandler(w http.ResponseWriter, r *http.Request) {
uname := r.FormValue("user")
upass := r.FormValue("password")
u := User{Name: uname, Password: upass}
saveUser(u)
http.Redirect(w, r, "/view/"+uname, http.StatusFound)
}
func editUserHandler(w http.ResponseWriter, r *http.Request) {
u := User{}
t, _ := template.ParseFiles("create.html")
t.Execute(w, u)
}
func viewUserHandler(w http.ResponseWriter, r *http.Request) {
username := r.URL.Path[len("/view/"):]
u := loadUser(username)
t, _ := template.ParseFiles("view.html")
t.Execute(w, u)
}
func main() {
http.HandleFunc("/rest/create/", createUserApi)
http.HandleFunc("/rest/getuser/", getUserApi)
http.HandleFunc("/view/", viewUserHandler)
http.HandleFunc("/edit/", editUserHandler)
http.HandleFunc("/save/", saveUserHandler)
http.ListenAndServe(":8080", nil)
}