Skip to main content

A simple backend

Draft — needs verification against the real http/db module API

This example follows TLang's established naming conventions (module-dot- function, camelCase for multi-word names) but the exact function signatures below are illustrative, not confirmed against the actual http and db native modules. Swap in the real API before treating this as canonical documentation.

A minimal HTTP service with one route that reads from SQLite and returns JSON — the kind of thing the standard library's http, db, and json modules exist for.

server.tiny
// Open a connection to a local SQLite database
let conn be db.connect("app.db")

define getUser taking request
let id be request.param("id")
let row be db.queryOne(conn, "SELECT id, name, email FROM users WHERE id = ?", id)

if row == nil
return http.respond(404, json.encode("error" -> "user not found"))
otherwise
return http.respond(200, json.encode(row))

define createUser taking request
let body be json.decode(request.body())
let result be validate.check(body, "name" -> "required", "email" -> "required")

if not result.ok
return http.respond(422, json.encode(result.errors))

db.execute(conn, "INSERT INTO users (name, email) VALUES (?, ?)", body.name, body.email)
return http.respond(201, json.encode("status" -> "created"))

http.route("GET", "/users/:id", getUser)
http.route("POST", "/users", createUser)

log.info("server starting on port 8080")
http.listen(8080)

Run it the same way as any other script:

tlang run server.tiny

What this is meant to demonstrate​

  • Native modules are called as module.function(...), matching the established validate.check / jwt.verify pattern of returning failure as data rather than throwing — db.queryOne returning nil on a miss, validate.check returning { ok, errors }, are both examples of that "routine outcomes vs. programmer errors" split.
  • Route handlers are ordinary TLang functions taking a single request value and returning whatever http.respond produces.
  • Nothing here requires anything outside the standard library — no external HTTP framework, no ORM, no package manager step before this runs.