--- /dev/null
+#!/bin/sh
+# Copyright (c) 2024 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+. /etc/environment
+
+PAGE="
+<!DOCTYPE html>
+<html>
+<meta name='viewport' content='width=device-width'>
+<head>
+ <title>Metrics</title>
+ <style>
+ body {
+ background-color: #7fa99b;
+ }
+ form {
+ width: min(400px, 100%);
+ }
+ fieldset {
+ background-color: #f1d18a;
+ display: flex;
+ flex-direction: column;
+ row-gap: 5px;
+ }
+ label {
+ column-gap: 5px;
+ display: flex;
+ }
+ input[type=date] {
+ width: 100%;
+ }
+ </style>
+</head>
+<body>
+ <form method='post' action='/sleep'>
+ <fieldset>
+ <legend>Sleep</legend>
+
+ <label>
+ 📅<input type='date' id='date' name='date' required />
+ </label>
+
+ <input type='time' id='start' name='start' required />
+ <input type='time' id='end' name='end' required />
+
+ <button type='submit'>Record</>
+ </fieldset>
+ </form>
+</body>
+</html>"
+
+echo "HTTP/1.1 200 OK"
+echo "Content-Type: text/html"
+echo "${PAGE}" | sed 's/^[ \t]*//g'
--- /dev/null
+#!/bin/sh
+# Copyright (c) 2024 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+. /etc/environment
+
+request_body=$(echo "${REQUEST_BODY}" | /usr/bin/python3 -c "\
+import sys;
+from urllib.parse import unquote_plus;
+print(unquote_plus(sys.stdin.read()));")
+
+get_query_param() {
+ echo "${request_body}" | sed "s/^.*${1}=// ; s/\&.*$//"
+}
+
+query_db() {
+ /usr/bin/psql --user=metrics_tracker --dbname="${METRICS_DB}" \
+ --tuples-only --no-align --command="${1}"
+}
+
+sleep_date=$(get_query_param "date")
+start=$(get_query_param "start")
+finish=$(get_query_param "finish")
+sleep_id=$(query_db "\
+WITH new_sleep_id AS (
+ INSERT INTO sleep (
+ date,
+ start,
+ finish
+ )
+ VALUES (
+ '${sleep_date}',
+ '${start}',
+ '${finish}'
+ )
+ RETURNING id
+)
+SELECT *
+FROM new_sleep_id;")
+
+PAGE="
+<!DOCTYPE html>
+<html>
+<meta name='viewport' content='width=device-width'>
+<head>
+ <title>Purchase</title>
+ <style>
+ body {
+ white-space: pre-wrap;
+ font-size: 1.2em;
+ }
+ </style>
+</head>
+<body>
+Recorded new sleep (id=${sleep_id}) from ${start} to ${finish}.
+</body>
+</html>"
+
+echo "HTTP/1.1 200 OK"
+echo "Content-Type: text/html"
+echo "${PAGE}" | sed 's/^[ \t]*//g'