forked from kataras/iris
/
controller.go
189 lines (165 loc) · 5.47 KB
/
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package user
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
var (
// About Code: iris.StatusSeeOther ->
// When redirecting from POST to GET request you -should- use this HTTP status code,
// however there're some (complicated) alternatives if you
// search online or even the HTTP RFC.
// "See Other" RFC 7231
pathMyProfile = mvc.Response{Path: "/user/me", Code: iris.StatusSeeOther}
pathRegister = mvc.Response{Path: "/user/register"}
)
// Controller is responsible to handle the following requests:
// GET /user/register
// POST /user/register
// GET /user/login
// POST /user/login
// GET /user/me
// GET /user/{id:long} | long is a new param type, it's the int64.
// All HTTP Methods /user/logout
type Controller struct {
AuthController
}
type formValue func(string) string
// BeforeActivation called once before the server start
// and before the controller's registration, here you can add
// dependencies, to this controller and only, that the main caller may skip.
func (c *Controller) BeforeActivation(b mvc.BeforeActivation) {
// bind the context's `FormValue` as well in order to be
// acceptable on the controller or its methods' input arguments (NEW feature as well).
b.Dependencies().Add(func(ctx iris.Context) formValue { return ctx.FormValue })
}
type page struct {
Title string
}
// GetRegister handles GET:/user/register.
// mvc.Result can accept any struct which contains a `Dispatch(ctx iris.Context)` method.
// Both mvc.Response and mvc.View are mvc.Result.
func (c *Controller) GetRegister() mvc.Result {
if c.isLoggedIn() {
return c.logout()
}
// You could just use it as a variable to win some time in serve-time,
// this is an exersise for you :)
return mvc.View{
Name: pathRegister.Path + ".html",
Data: page{"User Registration"},
}
}
// PostRegister handles POST:/user/register.
func (c *Controller) PostRegister(form formValue) mvc.Result {
// we can either use the `c.Ctx.ReadForm` or read values one by one.
var (
firstname = form("firstname")
username = form("username")
password = form("password")
)
user, err := c.createOrUpdate(firstname, username, password)
if err != nil {
return c.fireError(err)
}
// setting a session value was never easier.
c.Session.Set(sessionIDKey, user.ID)
// succeed, nothing more to do here, just redirect to the /user/me.
return pathMyProfile
}
// with these static views,
// you can use variables-- that are initialized before server start
// so you can win some time on serving.
// You can do it else where as well but I let them as pracise for you,
// essentially you can understand by just looking below.
var userLoginView = mvc.View{
Name: PathLogin.Path + ".html",
Data: page{"User Login"},
}
// GetLogin handles GET:/user/login.
func (c *Controller) GetLogin() mvc.Result {
if c.isLoggedIn() {
return c.logout()
}
return userLoginView
}
// PostLogin handles POST:/user/login.
func (c *Controller) PostLogin(form formValue) mvc.Result {
var (
username = form("username")
password = form("password")
)
user, err := c.verify(username, password)
if err != nil {
return c.fireError(err)
}
c.Session.Set(sessionIDKey, user.ID)
return pathMyProfile
}
// AnyLogout handles any method on path /user/logout.
func (c *Controller) AnyLogout() {
c.logout()
}
// GetMe handles GET:/user/me.
func (c *Controller) GetMe() mvc.Result {
id, err := c.Session.GetInt64(sessionIDKey)
if err != nil || id <= 0 {
// when not already logged in, redirect to login.
return PathLogin
}
u, found := c.Source.GetByID(id)
if !found {
// if the session exists but for some reason the user doesn't exist in the "database"
// then logout him and redirect to the register page.
return c.logout()
}
// set the model and render the view template.
return mvc.View{
Name: pathMyProfile.Path + ".html",
Data: iris.Map{
"Title": "Profile of " + u.Username,
"User": u,
},
}
}
func (c *Controller) renderNotFound(id int64) mvc.View {
return mvc.View{
Code: iris.StatusNotFound,
Name: "user/notfound.html",
Data: iris.Map{
"Title": "User Not Found",
"ID": id,
},
}
}
// Dispatch completes the `mvc.Result` interface
// in order to be able to return a type of `Model`
// as mvc.Result.
// If this function didn't exist then
// we should explicit set the output result to that Model or to an interface{}.
func (u Model) Dispatch(ctx iris.Context) {
ctx.JSON(u)
}
// GetBy handles GET:/user/{id:long},
// i.e http://localhost:8080/user/1
func (c *Controller) GetBy(userID int64) mvc.Result {
// we have /user/{id}
// fetch and render user json.
user, found := c.Source.GetByID(userID)
if !found {
// not user found with that ID.
return c.renderNotFound(userID)
}
// Q: how the hell Model can be return as mvc.Result?
// A: I told you before on some comments and the docs,
// any struct that has a `Dispatch(ctx iris.Context)`
// can be returned as an mvc.Result(see ~20 lines above),
// therefore we are able to combine many type of results in the same method.
// For example, here, we return either an mvc.View to render a not found custom template
// either a user which returns the Model as JSON via its Dispatch.
//
// We could also return just a struct value that is not an mvc.Result,
// if the output result of the `GetBy` was that struct's type or an interface{}
// and iris would render that with JSON as well, but here we can't do that without complete the `Dispatch`
// function, because we may return an mvc.View which is an mvc.Result.
return user
}