forked from tchen0123/supergiant
/
sessions_controller.go
87 lines (79 loc) · 2.04 KB
/
sessions_controller.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
package ui
import (
"net/http"
"github.com/gorilla/mux"
"github.com/supergiant/supergiant/pkg/client"
"github.com/supergiant/supergiant/pkg/core"
"github.com/supergiant/supergiant/pkg/model"
)
func NewSession(sg *client.Client, w http.ResponseWriter, r *http.Request) error {
return renderTemplate(sg, w, "login", map[string]interface{}{
"title": "Sessions",
"formAction": "/ui/sessions",
})
}
func CreateSession(sg *client.Client, w http.ResponseWriter, r *http.Request) error {
if err := r.ParseForm(); err != nil {
return err
}
m := &model.Session{
User: &model.User{
Username: r.PostForm.Get("username"),
Password: r.PostForm.Get("password"),
},
}
if err := sg.Sessions.Create(m); err != nil {
return renderTemplate(sg, w, "login", map[string]interface{}{
"title": "Sessions",
"formAction": "/ui/sessions",
"error": err.Error(),
})
}
// Store Session ID in Cookie
cookie := &http.Cookie{
Name: core.SessionCookieName,
Value: m.ID,
Path: "/",
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/ui/sessions", http.StatusFound)
return nil
}
func ListSessions(sg *client.Client, w http.ResponseWriter, r *http.Request) error {
fields := []map[string]interface{}{
{
"title": "User ID",
"type": "field_value",
"field": "user_id",
},
{
"title": "Created at",
"type": "field_value",
"field": "created_at",
},
}
return renderTemplate(sg, w, "index", map[string]interface{}{
"title": "Sessions",
"uiBasePath": "/ui/sessions",
"apiBasePath": "/api/v0/sessions",
"fields": fields,
"showNewLink": false,
"batchActionPaths": map[string]map[string]string{
"Delete": map[string]string{
"method": "DELETE",
"relativePath": "",
},
},
})
}
func GetSession(sg *client.Client, w http.ResponseWriter, r *http.Request) error {
id := mux.Vars(r)["id"]
item := new(model.Session)
if err := sg.Sessions.Get(id, item); err != nil {
return err
}
return renderTemplate(sg, w, "show", map[string]interface{}{
"title": "Sessions",
"model": item,
})
}