-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Cask tutorials #3056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Cask tutorials #3056
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,18 +11,18 @@ next-page: | |||||
|
||||||
## Using cookies | ||||||
|
||||||
Cookies are saved by adding them to the `cookies` parameter of `cask.Response` constructor. | ||||||
Cookies are saved by adding them to the `cookies` parameter of the `cask.Response` constructor. | ||||||
|
||||||
In this example we are building a rudimentary authentication service. The `getLogin` method provides a form where | ||||||
username and password can be inputted. The `postLogin` reads the credentials and if they match the expected ones, a session | ||||||
identifier is generated, saved in the application state and sends back a cookie with the identifier. | ||||||
In this example, we are building a rudimentary authentication service. The `getLogin` method provides a form where | ||||||
username and password can be inputted. The `postLogin` reads the credentials and, if they match the expected ones, a session | ||||||
identifier is generated, saved in the application state, and sends back a cookie with the identifier. | ||||||
|
||||||
Cookies can be read either with a method parameter with `cask.Cookie` type or by accessing `cask.Request` directly. | ||||||
If using the former method, names of parameters have to match the names of cookies. If a cookie with matching name is not | ||||||
found, an error response will be returned. In the `checkLogin` function the former method is used, as the cookie is not | ||||||
present before user logs in. | ||||||
Cookies can be read either with a method parameter of `cask.Cookie` type or by accessing `cask.Request` directly. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
If using the former method, the names of parameters have to match the names of cookies. If a cookie with a matching name is not | ||||||
found, an error response will be returned. In the `checkLogin` function, the former method is used, as the cookie is not | ||||||
present before the user logs in. | ||||||
|
||||||
To delete a cookie set its `expires` parameter to an instant in the past, for example `Instant.EPOCH`. | ||||||
To delete a cookie, set its `expires` parameter to an instant in the past, for example `Instant.EPOCH`. | ||||||
|
||||||
{% tabs web-server-cookies-1 class=tabs-scala-version %} | ||||||
{% tab 'Scala 2' %} | ||||||
|
@@ -144,7 +144,7 @@ object MyApp extends cask.MainRoutes: | |||||
Decorators can be used for extending endpoints functionality with validation or new parameters. They are defined by extending | ||||||
`cask.RawDecorator` class and then used as annotations. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
In this example, the `loggedIn` decorator is used for checking if user is logged in before accessing the `/decorated` | ||||||
In this example, the `loggedIn` decorator is used to check if the user is logged in before accessing the `/decorated` | ||||||
endpoint. | ||||||
|
||||||
The decorator class can pass additional arguments to the decorated endpoint using a map. The passed arguments are available | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -39,11 +39,11 @@ object MyApp extends cask.MainRoutes: | |||||
{% endtab %} | ||||||
{% endtabs %} | ||||||
|
||||||
The example above creates an endpoint returning the current date and time available at `/time`. The exact response will be | ||||||
The example above creates an endpoint, returning the current date and time available at `/time`. The exact response will be | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
recreated every time you refresh the webpage. | ||||||
|
||||||
Since the endpoint method has `String` output type, the result will be sent with `text/plain` [content type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type). | ||||||
If you want an HTML output being interpreted by the browser, you else need to set the `Content-Type` header manually | ||||||
Since the endpoint method has the `String` output type, the result will be sent with `text/plain` [content type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
If you want an HTML output to be interpreted by the browser, you will need to set the `Content-Type` header manually | ||||||
or [use the Scalatags templating library](/toolkit/web-server-dynamic.html#using-html-templates), supported by Cask. | ||||||
|
||||||
### Running the example | ||||||
|
@@ -71,15 +71,15 @@ In the terminal, the following command will start the server: | |||||
{% endtab %} | ||||||
{% endtabs %} | ||||||
|
||||||
Access [the endpoint](http://localhost:8080/time). You should see a result similar to the one below. | ||||||
Access the endpoint at [http://localhost:8080/time](http://localhost:8080/time). You should see a result similar to the one below. | ||||||
|
||||||
``` | ||||||
Current date is: 2024-07-22T09:07:05.752534+02:00[Europe/Warsaw] | ||||||
``` | ||||||
|
||||||
## Using path segments | ||||||
|
||||||
Cask gives you the ability to access segments of the URL path withing the endpoint function. | ||||||
Cask gives you the ability to access segments of the URL path within the endpoint function. | ||||||
Building on the example above, you can add a segment to specify that the endpoint should return the date and time | ||||||
in a given city. | ||||||
|
||||||
|
@@ -129,10 +129,10 @@ object MyApp extends cask.MainRoutes: | |||||
{% endtabs %} | ||||||
|
||||||
In the example above, the `:city` segment in `/time/:city` is available through the `city` argument of the endpoint method. | ||||||
The name of the argument must be identical to the segment name. The `getZoneIdForCity` helper method find the timezone for | ||||||
a given city and then the current date and time is translated to that timezone. | ||||||
The name of the argument must be identical to the segment name. The `getZoneIdForCity` helper method finds the timezone for | ||||||
a given city, and then the current date and time are translated to that timezone. | ||||||
|
||||||
Accessing [the endpoint](http://localhost:8080/time/Paris) (notice the `Paris` segment in the URL) will result with: | ||||||
Accessing the endpoint at[http://localhost:8080/time/Paris](http://localhost:8080/time/Paris) will result in: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
``` | ||||||
Current date is: 2024-07-22T09:08:33.806259+02:00[Europe/Paris] | ||||||
``` | ||||||
|
@@ -233,5 +233,5 @@ object MyApp extends cask.MainRoutes: | |||||
{% endtab %} | ||||||
{% endtabs %} | ||||||
|
||||||
Here we get the text of response and wrap it in a Scalatags template. Notice that the return type changed from `String` | ||||||
Here we get the text of the response and wrap it in a Scalatags template. Notice that the return type changed from `String` | ||||||
to `doctype`. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,15 +9,15 @@ next-page: web-server-static | |||||
|
||||||
Cask is an HTTP micro-framework, providing a simple and flexible way to build web applications. | ||||||
|
||||||
Its main focus is on the ease of use, which makes it ideal for newcomers, at cost of eschewing some features other | ||||||
Its main focus is on the ease of use, which makes it ideal for newcomers, at the cost of eschewing some features other | ||||||
frameworks provide, like asynchronicity. | ||||||
|
||||||
To define an endpoint it's enough to annotate a function with an appropriate annotation, specifying the request path. | ||||||
Cask allows for building the response manually using tools the Cask library provides, specifying the content, headers, | ||||||
status code etc. An endpoint function can also just return a string, [uPickle](https://com-lihaoyi.github.io/upickle/) JSON type, or a [Scalatags](https://com-lihaoyi.github.io/scalatags/) | ||||||
To define an endpoint it's enough to annotate a function with an annotation specifying the request path. | ||||||
Cask allows for building the response manually using tools Cask library provides, specifying the content, headers, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
status code, etc. An endpoint function can also just return a string, a [uPickle](https://com-lihaoyi.github.io/upickle/) JSON type, or a [Scalatags](https://com-lihaoyi.github.io/scalatags/) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
template and Cask will automatically create a response, setting all the necessary headers. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Cask comes bundled with uPickle library for handling JSONs, supports WebSockets and allows for extending endpoints with | ||||||
Cask comes bundled with the uPickle library for handling JSONs, supports WebSockets and allows for extending endpoints with | ||||||
decorators, which can be used to handle authentication or rate limiting. | ||||||
|
||||||
{% include markdown.html path="_markdown/install-cask.md" %} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,13 +11,13 @@ next-page: web-server-dynamic | |||||
|
||||||
## Serving a static file | ||||||
|
||||||
An endpoint is a specific URL where a particular webpage can be accessed. In Cask an endpoint is a function returning the | ||||||
An endpoint is a specific URL where a particular webpage can be accessed. In Cask, an endpoint is a function returning the | ||||||
webpage data together with an annotation describing the URL it's available at. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
To create a static file serving endpoint we need two things: an HTML file with the page content and a function that | ||||||
points out to the location of the file. | ||||||
To create a static file serving endpoint, we need two things: an HTML file with the page content and a function that | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
points out the location of the file. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Create a minimal HTML file named `hello.html` with following contents. | ||||||
Create a minimal HTML file named `hello.html` with the following contents. | ||||||
|
||||||
```html | ||||||
<!doctype html> | ||||||
|
@@ -89,18 +89,18 @@ object Example extends cask.MainRoutes: | |||||
{% endtab %} | ||||||
{% endtabs %} | ||||||
|
||||||
In the example above, `@cask.staticFiles` instructs the server to look for files accessed at `/static` path in the | ||||||
In the example above, `@cask.staticFiles` instructs the server to look for files accessed at the `/static` path in the | ||||||
`src/main/resources` directory. Cask will match any subpath coming after `/static` and append it to the directory path. | ||||||
If you access the `/static/hello.html` file, it will serve the file available at `src/main/resources/hello.html`. | ||||||
The directory path can be any path available to the server, relative or not. If the requested file cannot be found in the | ||||||
specified location, a 404 response with an error message will be returned instead. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
The `Example` object inherits from `cask.MainRoutes` class, providing the main function starting the server. The `initialize()` | ||||||
method call initializes the server routes, i.e. the association between URL paths and the code that handles them. | ||||||
The `Example` object inherits from the `cask.MainRoutes` class, providing the main function that starts the server. The `initialize()` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
method call initializes the server routes, i.e., the association between URL paths and the code that handles them. | ||||||
|
||||||
### Using the resources directory | ||||||
|
||||||
The `@cask.staticResources` annotation works in the same way as `@cask.staticFiles` used above with the difference that | ||||||
The `@cask.staticResources` annotation works in the same way as the `@cask.staticFiles` used above, with the difference that | ||||||
the path returned by the endpoint method describes the location of files _inside_ the resources directory. Since the | ||||||
previous example conveniently used the resources directory, it can be simplified with `@cask.staticResources`. | ||||||
|
||||||
|
@@ -126,9 +126,9 @@ object Example extends cask.MainRoutes: | |||||
{% endtab %} | ||||||
{% endtabs %} | ||||||
|
||||||
In the endpoint method the location is set to `"."`, telling the server that the files are available directly in the | ||||||
In the endpoint method, the location is set to `"."`, telling the server that the files are available directly in the | ||||||
resources directory. In general, you can use any nested location within the resources directory, for instance you could opt | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for placing your HTML files in `static` directory inside the resources directory or use different directories to sort out | ||||||
for placing your HTML files in the `static` directory inside the resources directory or using different directories to sort out | ||||||
files used by different endpoints. | ||||||
|
||||||
## Running the example | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.