@@ -31,12 +31,12 @@ To delete a cookie, set its `expires` parameter to an instant in the past, for e
31
31
import java .util .UUID
32
32
import java .util .concurrent .ConcurrentHashMap
33
33
34
- object MyApp extends cask.MainRoutes {
34
+ object Example extends cask.MainRoutes {
35
35
36
36
val sessionIds = ConcurrentHashMap .newKeySet[String ]()
37
37
38
38
@ cask.get(" /login" )
39
- def getLogin () = {
39
+ def getLogin (): cask. Response [ String ] = {
40
40
val html =
41
41
""" <!doctype html>
42
42
|<html>
@@ -55,7 +55,7 @@ object MyApp extends cask.MainRoutes {
55
55
}
56
56
57
57
@ cask.postForm(" /login" )
58
- def postLogin (name : String , password : String ) = {
58
+ def postLogin (name : String , password : String ): cask. Response [ String ] = {
59
59
if (name == " user" && password == " password" ) {
60
60
val sessionId = UUID .randomUUID().toString
61
61
sessionIds.add(sessionId)
@@ -66,7 +66,7 @@ object MyApp extends cask.MainRoutes {
66
66
}
67
67
68
68
@ cask.get(" /check" )
69
- def checkLogin (request : cask.Request ) = {
69
+ def checkLogin (request : cask.Request ): String = {
70
70
val sessionId = request.cookies.get(" sessionId" )
71
71
if (sessionId.exists(cookie => sessionIds.contains(cookie.value))) {
72
72
" You are logged in"
@@ -90,12 +90,12 @@ object MyApp extends cask.MainRoutes {
90
90
import java .util .UUID
91
91
import java .util .concurrent .ConcurrentHashMap
92
92
93
- object MyApp extends cask.MainRoutes :
93
+ object Example extends cask.MainRoutes :
94
94
95
95
val sessionIds = ConcurrentHashMap .newKeySet[String ]()
96
96
97
97
@ cask.get(" /login" )
98
- def getLogin () =
98
+ def getLogin (): cask. Response [ String ] =
99
99
val html =
100
100
""" <!doctype html>
101
101
|<html>
@@ -113,24 +113,24 @@ object MyApp extends cask.MainRoutes:
113
113
cask.Response (data = html, headers = Seq (" Content-Type" -> " text/html" ))
114
114
115
115
@ cask.postForm(" /login" )
116
- def postLogin (name : String , password : String ) =
117
- if name == " user" && password == " password" :
116
+ def postLogin (name : String , password : String ): cask. Response [ String ] =
117
+ if name == " user" && password == " password" then
118
118
val sessionId = UUID .randomUUID().toString
119
119
sessionIds.add(sessionId)
120
120
cask.Response (data = " Success!" , cookies = Seq (cask.Cookie (" sessionId" , sessionId)))
121
121
else
122
122
cask.Response (data = " Authentication failed" , statusCode = 401 )
123
123
124
- @ cask.get(" /check" )
125
- def checkLogin (request : cask.Request ) =
126
- val sessionId = request.cookies.get(" sessionId" )
127
- if sessionId.exists(cookie => sessionIds.contains(cookie.value)):
128
- " You are logged in"
129
- else
130
- " You are not logged in"
124
+ @ cask.get(" /check" )
125
+ def checkLogin (request : cask.Request ): String =
126
+ val sessionId = request.cookies.get(" sessionId" )
127
+ if sessionId.exists(cookie => sessionIds.contains(cookie.value)) then
128
+ " You are logged in"
129
+ else
130
+ " You are not logged in"
131
131
132
132
@ cask.get(" /logout" )
133
- def logout (sessionId : cask.Cookie ) =
133
+ def logout (sessionId : cask.Cookie ): cask. Response [ String ] =
134
134
sessionIds.remove(sessionId.value)
135
135
cask.Response (data = " Successfully logged out!" , cookies = Seq (cask.Cookie (" sessionId" , " " , expires = Instant .EPOCH )))
136
136
@@ -154,7 +154,7 @@ through the last argument group. Here we are passing the session identifier to a
154
154
{% tab 'Scala 2' %}
155
155
``` scala
156
156
class loggedIn extends cask.RawDecorator {
157
- override def wrapFunction (ctx : cask.Request , delegate : Delegate ) = {
157
+ override def wrapFunction (ctx : cask.Request , delegate : Delegate ): Result [ Raw ] = {
158
158
ctx.cookies.get(" sessionId" ) match {
159
159
case Some (cookie) if sessionIds.contains(cookie.value) => delegate(Map (" sessionId" -> cookie.value))
160
160
case _ => cask.router.Result .Success (cask.model.Response (" You aren't logged in" , 403 ))
@@ -164,15 +164,15 @@ class loggedIn extends cask.RawDecorator {
164
164
165
165
@ loggedIn()
166
166
@ cask.get(" /decorated" )
167
- def decorated ()(sessionId : String ) = {
167
+ def decorated ()(sessionId : String ): String = {
168
168
s " You are logged in with id: $sessionId"
169
169
}
170
170
```
171
171
{% endtab %}
172
172
{% tab 'Scala 3' %}
173
173
``` scala
174
174
class loggedIn extends cask.RawDecorator :
175
- override def wrapFunction (ctx : cask.Request , delegate : Delegate ) =
175
+ override def wrapFunction (ctx : cask.Request , delegate : Delegate ): Result [ Raw ] =
176
176
ctx.cookies.get(" sessionId" ) match
177
177
case Some (cookie) if sessionIds.contains(cookie.value) =>
178
178
delegate(Map (" sessionId" -> cookie.value))
@@ -182,7 +182,7 @@ class loggedIn extends cask.RawDecorator:
182
182
183
183
@ loggedIn()
184
184
@ cask.get(" /decorated" )
185
- def decorated ()(sessionId : String ) = s " You are logged in with id: $sessionId"
185
+ def decorated ()(sessionId : String ): String = s " You are logged in with id: $sessionId"
186
186
```
187
187
{% endtab %}
188
188
{% endtabs %}
0 commit comments