Kiln / Applications
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
04 Kiln / 01
Applications
A new application, a server that runs it, and the files it serves.
A Kiln application is a folder with everything in its place: the code that starts the server, the handlers that answer requests, the templates and the files a browser asks for. This page covers making one, running it while you work on it, what the server does with each request, and how stylesheets and images reach the browser. How a request finds its handler has a page of its own.
A new application
Kiln makes an application in an app of its own, ready to run. Make its folder, give it a lode.hem and Kiln, and lode run kiln new writes the rest. Start lode run kiln dev, and it's serving:
mkdir reading-list
cd reading-list
lode init
lode add kiln --git <where it is>
lode run kiln new
lode run kiln devEach part of the application has a place in the folder:
app.hemstarts the server.pages.hemhas the handlers.views/has the templates.public/has the files served as they are.
The application is named after its folder.
While you work
lode run kiln dev, run in the application's folder, runs app.hem and watches the folder. Whenever a .hem, .och or .sql file in it changes, it starts the application again on the same port. Files in public/ are read on each request, so they need no restart at all. What follows dev goes to app.hem, like a port: lode run kiln dev 4000.
You don't reload the page yourself. Pages open in a browser reload the moment the application has started again, or a file it serves from a folder (with Route.files) has changed, so a saved template is on the screen straight away. A rebuilt stylesheet doesn't even need a reload: the page swaps it in where it is. And a page only reloads once the server answers, so a restart never leaves it on the browser's error page.
When the application doesn't start, because of a mistake in the code, say, the message says why, and the page shows a note while it waits. The next change you save starts it again, and the page catches up by itself.
For this, under kiln dev alone, each whole page of HTML (one with a </body>) gets a small script before its </body>, which keeps a connection open to /.kiln/live. A piece of a page, for htmx, gets no script, and nothing is added to a program run any other way. The application stops when kiln dev does, however it's stopped. To run it once, without watching, use hem run app.hem.
Starting a server
Server.start takes a port and a list of routes, and serves them until it's stopped:
import "@kiln"
class Pages
static def home(request: Request): Response
return Response.html(Html.raw("<h1>Welcome</h1>"), status: 200)
end
static def user(request: Request, id: Int): Response
return Response.text("user #{id}", status: 200)
end
end
try Server.start(port: 3000, routes: [
Route.get("/", to: Pages.home),
Route.get("/users/:id", to: Pages.user),
])Once it's running, it says where it's listening, then logs each request with its status and how long it took:
http: listening on http://127.0.0.1:3000 (Ctrl-C stops it)
GET / 200 0.2ms
GET /users/42 200 0.1msIt listens on 127.0.0.1, so only this computer can reach it. Port 0 picks a free port, which the first line shows.
Which route answers
The first route that fits a request gets it. A path that fits only routes for other methods gets 405, with an allow header saying which methods would work; a path that fits none gets 404. A HEAD request is answered by the GET route, without the body.
Extra slashes don't matter: /users/42/ is /users/42. Escapes are undone, so /hello/Ada%20L gives the handler Ada L.
What the server looks after
The server says how long each body is, so a handler's own content-length, transfer-encoding and connection headers are left out. A request body may be up to 10 MB of UTF-8 text: a larger one gets 413, and one that isn't text gets 400.
A handler that stops with a mistake doesn't stop the server. The browser gets 500 ("Internal server error."), the mistake goes into the log with its request, and the server goes on. A response whose status isn't from 100 to 599 is handled the same way. Failures a handler passes on get statuses of their own, which Handlers that fail lists.
When it can't start
Since Server.start runs until it's stopped, the only failure it gives back comes before it listens:
- The port is taken: kind
network. - The port needs rights, below 1024: kind
denied. - The number isn't a port at all: kind
invalid. - Two routes lead the same requests to different handlers: kind
invalid.
Many requests at once
Several requests are handled at once, one per core. Each core's worker has a copy of the program of its own, with its statics set up for it, and runs one handler at a time. Handlers can't see the file's script, its top-level vars and consts.
Files
Stylesheets, scripts and images are served from a folder by Route.files. It's a route like the others, in the same list:
import "@kiln"
try Server.start(port: 3000, routes: [
Route.files("/assets", from: "public"), # /assets/app.css is public/app.css
Route.get("/", to: Pages.home),
])The folder is found from where hem runs, the same way as File.read's paths. One that isn't there is a failure (kind missing) when the server starts. Each file's type comes from its extension (.css, .js, .png, .svg, .woff2, ...).
- Only the folder
- Only what's inside it is served: never
.., a hidden file like.env, or a link that leads outside, and no folder's list of files. - Shares paths
- A file there is served before any route is tried. A path that's no file there goes on to the routes, so
Route.files("/", from: "public")can share a path with the site's own pages. - Read only
GETandHEADread a file; other methods get 405. A request for a folder gets itsindex.html.- Never stale
- Browsers keep files, but ask whether they changed before using them again, and get 304 if not. After an edit, they get the new file.
The database
Kiln keeps no data of its own: an application keeps its data with the database package, whose guide is its README.
lode add database --git <where it is>Its tables are statics, so every worker has them, and each worker opens the database the program opened the first time it uses it.