Skip to content

Commit 569d5df

Browse files
committed
Fixes
1 parent 48431b6 commit 569d5df

6 files changed

+67
-56
lines changed

_overviews/toolkit/web-server-cookies-and-decorators.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ To delete a cookie, set its `expires` parameter to an instant in the past, for e
3131
import java.util.UUID
3232
import java.util.concurrent.ConcurrentHashMap
3333

34-
object MyApp extends cask.MainRoutes {
34+
object Example extends cask.MainRoutes {
3535

3636
val sessionIds = ConcurrentHashMap.newKeySet[String]()
3737

3838
@cask.get("/login")
39-
def getLogin() = {
39+
def getLogin(): cask.Response[String] = {
4040
val html =
4141
"""<!doctype html>
4242
|<html>
@@ -55,7 +55,7 @@ object MyApp extends cask.MainRoutes {
5555
}
5656

5757
@cask.postForm("/login")
58-
def postLogin(name: String, password: String) = {
58+
def postLogin(name: String, password: String): cask.Response[String] = {
5959
if (name == "user" && password == "password") {
6060
val sessionId = UUID.randomUUID().toString
6161
sessionIds.add(sessionId)
@@ -66,7 +66,7 @@ object MyApp extends cask.MainRoutes {
6666
}
6767

6868
@cask.get("/check")
69-
def checkLogin(request: cask.Request) = {
69+
def checkLogin(request: cask.Request): String = {
7070
val sessionId = request.cookies.get("sessionId")
7171
if (sessionId.exists(cookie => sessionIds.contains(cookie.value))) {
7272
"You are logged in"
@@ -90,12 +90,12 @@ object MyApp extends cask.MainRoutes {
9090
import java.util.UUID
9191
import java.util.concurrent.ConcurrentHashMap
9292

93-
object MyApp extends cask.MainRoutes:
93+
object Example extends cask.MainRoutes:
9494

9595
val sessionIds = ConcurrentHashMap.newKeySet[String]()
9696

9797
@cask.get("/login")
98-
def getLogin() =
98+
def getLogin(): cask.Response[String] =
9999
val html =
100100
"""<!doctype html>
101101
|<html>
@@ -113,24 +113,24 @@ object MyApp extends cask.MainRoutes:
113113
cask.Response(data = html, headers = Seq("Content-Type" -> "text/html"))
114114

115115
@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
118118
val sessionId = UUID.randomUUID().toString
119119
sessionIds.add(sessionId)
120120
cask.Response(data = "Success!", cookies = Seq(cask.Cookie("sessionId", sessionId)))
121121
else
122122
cask.Response(data = "Authentication failed", statusCode = 401)
123123

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"
131131

132132
@cask.get("/logout")
133-
def logout(sessionId: cask.Cookie) =
133+
def logout(sessionId: cask.Cookie): cask.Response[String] =
134134
sessionIds.remove(sessionId.value)
135135
cask.Response(data = "Successfully logged out!", cookies = Seq(cask.Cookie("sessionId", "", expires = Instant.EPOCH)))
136136

@@ -154,7 +154,7 @@ through the last argument group. Here we are passing the session identifier to a
154154
{% tab 'Scala 2' %}
155155
```scala
156156
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] = {
158158
ctx.cookies.get("sessionId") match {
159159
case Some(cookie) if sessionIds.contains(cookie.value) => delegate(Map("sessionId" -> cookie.value))
160160
case _ => cask.router.Result.Success(cask.model.Response("You aren't logged in", 403))
@@ -164,15 +164,15 @@ class loggedIn extends cask.RawDecorator {
164164

165165
@loggedIn()
166166
@cask.get("/decorated")
167-
def decorated()(sessionId: String) = {
167+
def decorated()(sessionId: String): String = {
168168
s"You are logged in with id: $sessionId"
169169
}
170170
```
171171
{% endtab %}
172172
{% tab 'Scala 3' %}
173173
```scala
174174
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] =
176176
ctx.cookies.get("sessionId") match
177177
case Some(cookie) if sessionIds.contains(cookie.value) =>
178178
delegate(Map("sessionId" -> cookie.value))
@@ -182,7 +182,7 @@ class loggedIn extends cask.RawDecorator:
182182

183183
@loggedIn()
184184
@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"
186186
```
187187
{% endtab %}
188188
{% endtabs %}

_overviews/toolkit/web-server-dynamic.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can create an endpoint returning dynamically generated content with `@cask.g
1818
```scala
1919
import java.time.ZonedDateTime
2020

21-
object MyApp extends cask.MainRoutes {
21+
object Example extends cask.MainRoutes {
2222
@cask.get("/time")
2323
def dynamic(): String = s"Current date is: ${ZonedDateTime.now()}"
2424

@@ -30,7 +30,7 @@ object MyApp extends cask.MainRoutes {
3030
```scala
3131
import java.time.ZonedDateTime
3232

33-
object MyApp extends cask.MainRoutes:
33+
object Example extends cask.MainRoutes:
3434
@cask.get("/time")
3535
def dynamic(): String = s"Current date is: ${ZonedDateTime.now()}"
3636

@@ -58,9 +58,9 @@ scala-cli run Example.scala
5858
```
5959
{% endtab %}
6060
{% tab 'sbt' %}
61-
In the sbt shell, the following command will start the server:
61+
In the terminal, the following command will start the server:
6262
```
63-
sbt:example> example/run
63+
sbt example/run
6464
```
6565
{% endtab %}
6666
{% tab 'Mill' %}
@@ -88,7 +88,7 @@ in a given city.
8888
```scala
8989
import java.time.{ZoneId, ZonedDateTime}
9090

91-
object MyApp extends cask.MainRoutes {
91+
object Example extends cask.MainRoutes {
9292

9393
private def getZoneIdForCity(city: String): Option[ZoneId] = {
9494
import scala.jdk.CollectionConverters._
@@ -111,7 +111,7 @@ object MyApp extends cask.MainRoutes {
111111
```scala
112112
import java.time.{ZoneId, ZonedDateTime}
113113

114-
object MyApp extends cask.MainRoutes:
114+
object Example extends cask.MainRoutes:
115115

116116
private def getZoneIdForCity(city: String): Option[ZoneId] =
117117
import scala.jdk.CollectionConverters.*
@@ -177,7 +177,7 @@ setting the `Content-Type` header to `text/html`.
177177
import java.time.{ZoneId, ZonedDateTime}
178178
import scalatags.Text.all._
179179

180-
object MyApp extends cask.MainRoutes {
180+
object Example extends cask.MainRoutes {
181181

182182
private def getZoneIdForCity(city: String): Option[ZoneId] = {
183183
import scala.jdk.CollectionConverters._
@@ -209,7 +209,7 @@ object MyApp extends cask.MainRoutes {
209209
import java.time.{ZoneId, ZonedDateTime}
210210
import scalatags.Text.all.*
211211

212-
object MyApp extends cask.MainRoutes:
212+
object Example extends cask.MainRoutes:
213213

214214
private def getZoneIdForCity(city: String): Option[ZoneId] =
215215
import scala.jdk.CollectionConverters.*

_overviews/toolkit/web-server-input.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ with names corresponding to names of fields in the form and set the form method
1717
{% tabs web-server-input-1 class=tabs-scala-version %}
1818
{% tab 'Scala 2' %}
1919
```scala
20-
object MyApp extends cask.MainRoutes {
20+
object Example extends cask.MainRoutes {
2121

2222
@cask.get("/form")
23-
def getForm(): cask.Response = {
23+
def getForm(): cask.Response[String] = {
2424
val html =
2525
"""<!doctype html>
2626
|<html>
@@ -48,10 +48,10 @@ object MyApp extends cask.MainRoutes {
4848
{% endtab %}
4949
{% tab 'Scala 3' %}
5050
```scala
51-
object MyApp extends cask.MainRoutes:
51+
object Example extends cask.MainRoutes:
5252

5353
@cask.get("/form")
54-
def getForm(): cask.Response =
54+
def getForm(): cask.Response[String] =
5555
val html =
5656
"""<!doctype html>
5757
|<html>
@@ -78,8 +78,8 @@ object MyApp extends cask.MainRoutes:
7878
{% endtabs %}
7979

8080
In this example we create a form asking for name and surname of a user and then redirect the user to a greeting page. Notice the
81-
use of `cask.Response`. The default returned content type in case of `String` returning endpoint method is `text/plain`,
82-
set it to `text/html` in order for browser to display the form correctly.
81+
use of `cask.Response`. The `cask.Response` type allows user to set the status code, headers and cookies. The default
82+
content type in case of `String` returning endpoint method is `text/plain`, set it to `text/html` in order for browser to display the form correctly.
8383

8484
The `formEndpoint` endpoint reads the form data using `name` and `surname` parameters. The names of parameters must
8585
be identical to the field names of the form.
@@ -92,7 +92,7 @@ will be read into the endpoint method arguments.
9292
{% tabs web-server-input-2 class=tabs-scala-version %}
9393
{% tab 'Scala 2' %}
9494
```scala
95-
object MyApp extends cask.MainRoutes {
95+
object Example extends cask.MainRoutes {
9696

9797
@cask.postJson("/json")
9898
def jsonEndpoint(name: String, surname: String): String =
@@ -104,9 +104,9 @@ object MyApp extends cask.MainRoutes {
104104
{% endtab %}
105105
{% tab 'Scala 3' %}
106106
```scala
107-
object MyApp extends cask.MainRoutes:
107+
object Example extends cask.MainRoutes:
108108

109-
@cask.postJson("/json")
109+
@cask.postJson("/json")
110110
def jsonEndpoint(name: String, surname: String): String =
111111
"Hello " + name + " " + surname
112112

@@ -138,7 +138,7 @@ from uPickle library.
138138
{% tabs web-server-input-3 class=tabs-scala-version %}
139139
{% tab 'Scala 2' %}
140140
```scala
141-
object MyApp extends cask.MainRoutes {
141+
object Example extends cask.MainRoutes {
142142

143143
@cask.postJson("/json")
144144
def jsonEndpoint(value: ujson.Value): String =
@@ -151,7 +151,7 @@ object MyApp extends cask.MainRoutes {
151151
{% endtab %}
152152
{% tab 'Scala 3' %}
153153
```scala
154-
object MyApp extends cask.MainRoutes:
154+
object Example extends cask.MainRoutes:
155155

156156
@cask.postJson("/json")
157157
def jsonEndpoint(value: ujson.Value): String =
@@ -189,7 +189,9 @@ location. To serialize a case class into JSON, use type class derivation or defi
189189
{% tabs web-server-input-4 class=tabs-scala-version %}
190190
{% tab 'Scala 2' %}
191191
```scala
192-
object MyApp extends cask.MainRoutes {
192+
import java.time.{ZoneId, ZonedDateTime}
193+
194+
object Example extends cask.MainRoutes {
193195
import upickle.default.{ReadWriter, macroRW, writeJs}
194196
case class TimeData(timezone: Option[String], time: String)
195197
object TimeData {
@@ -210,12 +212,16 @@ object MyApp extends cask.MainRoutes {
210212
}
211213
writeJs(TimeData(timezone.map(_.toString), time))
212214
}
215+
216+
initialize()
213217
}
214218
```
215219
{% endtab %}
216220
{% tab 'Scala 3' %}
217221
```scala
218-
object MyApp extends cask.MainRoutes {
222+
import java.time.{ZoneId, ZonedDateTime}
223+
224+
object Example extends cask.MainRoutes:
219225
import upickle.default.{ReadWriter, writeJs}
220226
case class TimeData(timezone: Option[String], time: String) derives ReadWriter
221227

@@ -224,14 +230,14 @@ object MyApp extends cask.MainRoutes {
224230
ZoneId.getAvailableZoneIds.asScala.find(_.endsWith("/" + city)).map(ZoneId.of)
225231

226232
@cask.get("/time_json/:city")
227-
def timeJSON(city: String): ujson.Value = {
233+
def timeJSON(city: String): ujson.Value =
228234
val timezone = getZoneIdForCity(city)
229235
val time = timezone match
230236
case Some(zoneId)=> s"Current date is: ${ZonedDateTime.now().withZoneSameInstant(zoneId)}"
231237
case None => s"Couldn't find time zone for city $city"
232238
writeJs(TimeData(timezone.map(_.toString), time))
233-
}
234-
}
239+
240+
initialize()
235241
```
236242
{% endtab %}
237243
{% endtabs %}

_overviews/toolkit/web-server-query-parameters.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In this example, the `city` parameter will be optional, which you specify in Cas
2222
```scala
2323
import java.time.{ZoneId, ZonedDateTime}
2424

25-
object MyApp extends cask.MainRoutes {
25+
object Example extends cask.MainRoutes {
2626

2727
private def getZoneIdForCity(city: String): Option[ZoneId] = {
2828
import scala.jdk.CollectionConverters._
@@ -48,19 +48,19 @@ object MyApp extends cask.MainRoutes {
4848
```scala
4949
import java.time.{ZoneId, ZonedDateTime}
5050

51-
object MyApp extends cask.MainRoutes:
51+
object Example extends cask.MainRoutes:
5252

5353
private def getZoneIdForCity(city: String): Option[ZoneId] =
5454
import scala.jdk.CollectionConverters.*
5555
ZoneId.getAvailableZoneIds.asScala.find(_.endsWith("/" + city)).map(ZoneId.of)
5656

57-
@cask.get("/time")
58-
def dynamicWithParam(city: Option[String] = None): String =
59-
city match
60-
case Some(value) => getZoneIdForCity(value) match
61-
case Some(zoneId) => s"Current date is: ${ZonedDateTime.now().withZoneSameInstant(zoneId)}"
62-
case None => s"Couldn't find time zone for city $value"
63-
case None => s"Current date is: ${ZonedDateTime.now()}"
57+
@cask.get("/time")
58+
def dynamicWithParam(city: Option[String] = None): String =
59+
city match
60+
case Some(value) => getZoneIdForCity(value) match
61+
case Some(zoneId) => s"Current date is: ${ZonedDateTime.now().withZoneSameInstant(zoneId)}"
62+
case None => s"Couldn't find time zone for city $value"
63+
case None => s"Current date is: ${ZonedDateTime.now()}"
6464

6565
initialize()
6666
```

_overviews/toolkit/web-server-static.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ scala-cli run Example.scala
143143
```
144144
{% endtab %}
145145
{% tab 'sbt' %}
146-
In the sbt shell, the following command will start the server:
146+
In the terminal, the following command will start the server:
147147
```
148-
sbt:example> example/run
148+
sbt example/run
149149
```
150150
{% endtab %}
151151
{% tab 'Mill' %}

_overviews/toolkit/web-server-websockets.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private def getZoneIdForCity(city: String): Option[ZoneId] = {
6969
}
7070

7171
@cask.websocket("/websocket")
72-
def websocket(): cask.WsHandler =
72+
def websocket(): cask.WsHandler = {
7373
cask.WsHandler { channel =>
7474
cask.WsActor {
7575
case cask.Ws.Text("") => channel.send(cask.Ws.Close())
@@ -81,6 +81,9 @@ def websocket(): cask.WsHandler =
8181
channel.send(cask.Ws.Text(text))
8282
}
8383
}
84+
}
85+
86+
initialize()
8487
```
8588
{% endtab %}
8689
{% tab 'Scala 3' %}
@@ -104,6 +107,8 @@ def websocket(): cask.WsHandler =
104107
channel.send(cask.Ws.Text(text))
105108
}
106109
}
110+
111+
initialize()
107112
```
108113
{% endtab %}
109114
{% endtabs %}

0 commit comments

Comments
 (0)