Skip to content

Notes

Notes is a small web app. You write a note in a form, and the app saves it in a SQLite database. The home page lists every note. The app also has a small JSON API for the same notes.

The app shows how the parts of a Novis project work together:

  • HTML pages built with html templates, which escape every value.
  • A SQLite database, opened with Core\Db::connect.
  • Routes, written as attributes on the methods that handle them.
  • A form that is checked before anything is saved.
  • Tests that send requests to the app without a server.

Each tab shows one file of the app. These are the same files that you download below.

apps/notes/nvs.toml
# The server listens on port 8142. The other apps in this repository use other ports,
# so all of them can run at the same time.
[server]
listen = ["127.0.0.1:8142"]
# The notes are kept in the SQLite file `notes.sqlite`, next to this file.
# Novis creates the file when the first request opens the database.
[db.notes]
driver = "sqlite"
path = "notes.sqlite"
# The tests use this database. It is kept in memory and deleted when the tests end.
[db.scratch]
driver = "sqlite"
path = ":memory:"
# The program may open these two databases and no other.
[capabilities.db]
connect = ["notes", "scratch"]

Install Novis first. Then download only the folder apps/notes of the Novis repository. A sparse clone (a clone that checks out only the folders you name) does this:

Terminal window
git clone --depth 1 --filter=blob:none --sparse https://github.com/novis-lang/novis.git
cd novis
git sparse-checkout set apps/notes
cd apps/notes

Start the server from the folder apps/notes:

Terminal window
nvs serve public/index.nvs

Open http://127.0.0.1:8142/ in your browser. The first request creates the file notes.sqlite in the same folder. Your notes are kept there, so they are still there after a restart.

Terminal window
nvs test .

nvs test . reads every .nvs file in the folder as one program and runs each #[Test] method:

Notes\AppTest
✓ theHomePageShowsTheForm 12.4 ms
✓ aFormWithNoTitleShowsTheProblem 0.5 ms
✓ theApiChecksTheJson 0.2 ms
✓ aMissingNoteIsNotFound 0.4 ms
✓ anErrorShowsTheFailurePage 0.4 ms
✓ aWrongMethodIsNotAllowed 0.2 ms
✓ aFormFromAnotherSiteIsRefused 0.2 ms
Notes\DraftTest
✓ aTitleAndATextAreValid 0.1 ms
✓ aTitleWithOnlySpacesIsNotValid 0.1 ms
✓ aLongTitleIsNotValid 0.1 ms
✓ anEmptyTextIsValid 0.0 ms
Notes\StoreTest
✓ aSavedNoteCanBeFound 0.6 ms
✓ theNewestNoteIsFirst 0.3 ms
✓ aMissingNoteIsNull 0.2 ms
0 failed, 14 passed, 0 skipped, 0 flaky in 16 ms
notes/
nvs.toml the configuration: the port, the two databases and the permission to open them
public/
index.nvs the entry file: every request starts here
src/
App.nvs the routes: one method for each kind of request
AppTest.nvs sends requests to the app and checks the responses
Draft.nvs a note from the form or the API, and the checks on it
DraftTest.nvs tests the checks on a new note
Gate.nvs decides which routes may run, and shows the error pages
Note.nvs a saved note
Pages.nvs the HTML pages
Store.nvs reads and saves notes in the database
StoreTest.nvs tests saving and reading notes

[server] listen in nvs.toml sets the port of the server to 8142. The other example apps use other ports, so you can run them at the same time.

nvs.toml names two SQLite databases. notes is the file notes.sqlite. scratch is kept in memory, and only the tests use it. The section [capabilities.db] allows the app to open these two databases. A database that is not in this list cannot be opened.

public/index.nvs has two lines. The autoload line reads each class Notes\... from the folder src. Then it calls Core\Router::dispatch(new Notes\Gate()). dispatch finds the route that matches the request and calls its method.

src/App.nvs has one method for each route. A #[Core\Route] attribute gives the path and the HTTP method. A #[Core\Access] attribute says who may use the route. Every route of this app is public.

src/Gate.nvs is the gate. dispatch calls it for every request, and it has three methods:

  • allows runs before each route method. The route method runs only when allows returns true.
  • refused shows a page when no route method can handle the request. Examples are a path with no route (404), a wrong HTTP method (405) and a form sent from another website (403).
  • failed shows a page when a route method throws an error. The status is 500.

dispatch sets the status code before it calls refused or failed, so the gate only shows the page.

src/Draft.nvs is a new note before it is saved. The form and the JSON API both create a Draft. Its text came from the user, so it is tainted string. problems() returns one message for each property that is not valid.

src/Store.nvs runs the SQL. Every value goes to the database as a ? placeholder. The text of a note is never part of the SQL.

src/Pages.nvs builds each page with an html template. A value you put into it with {$name} is escaped. A note with the title <b>Milk</b> shows those characters as text.

Method and path What it does
GET / Shows the form and every note, newest first.
POST /notes Saves the note from the form, then goes back to /. If the form is not valid, it shows the form again with the status 422.
GET /notes/{id} Shows one note.
GET /api/notes Returns every note as JSON.
GET /api/notes/{id} Returns one note as JSON, or the status 404.
POST /api/notes Saves a note from a JSON body and returns it with the status 201.

Try the API with curl while the server runs:

Terminal window
curl -H "content-type: application/json" -d '{"title": "Milk", "body": "Buy two"}' http://127.0.0.1:8142/api/notes
curl http://127.0.0.1:8142/api/notes
  1. Copy the folder apps/notes and give it the name of your app.
  2. Choose a namespace for your app, for example Shop. Change namespace Notes; in each file under src/, and the autoload line and Notes\Gate in public/index.nvs.
  3. In nvs.toml, rename the database notes and its file. Change the name in [capabilities.db] and in the call to Core\Db::connect in src/App.nvs.
  4. Delete the classes you do not need, and their tests. Run nvs test . after each change.

Notes has no login. Anybody who can open the page can write a note. Before you put an app like this on the internet, add a login and a CSRF token to each form. A CSRF token is a value that proves a form came from your own page.

  1. In nvs.toml, set csrf_key under [http]. The key is 32 random bytes, written in base64.
  2. In the method that shows a form, call Core\Csrf::token(). It returns the token, and the response sends the session cookie.
  3. Put the token in a hidden field of the form: <input type="hidden" name="_csrf" value="{$token}">

The server then checks the token of each Post request before your method runs. A request without a valid token gets the status 403. The reference page of Core\Csrf::token shows a whole form. Then read the security checklist.