Files, HTTP, JSON & SQLite
Build web servers, fetch APIs, read/write files, parse JSON/CSV, and query SQLite databases.
Filesystem (fs)
files.dl
import fs
fs.write_text("hello.txt", "Hello from Dlang!")
content = fs.read_text("hello.txt")
show contentHTTP Client & Server (http)
http.serve(address, handler, max_requests) starts a synchronous HTTP server. Handlers receive {method, path, query, headers, body} and return {status, content_type, body}.
server.dl
import http
fn handle(req)
return {
status: 200,
content_type: "text/plain; charset=utf-8",
body: "Hello from Dlang on " + req.path
}
end
http.serve("127.0.0.1:8080", handle, 0)json, csv, & sqlite
data_io.dl
import json
import sqlite
payload = json.parse("{\"ok\":true,\"count\":3}")
show payload.count
conn = sqlite.open(":memory:")
sqlite.exec(conn, "CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)", [])
sqlite.exec(conn, "INSERT INTO t (name) VALUES (?)", ["dlang"])
show sqlite.query(conn, "SELECT * FROM t", [])
sqlite.close(conn)