Build a Minimal HTTP Server in Larzscript
Answer a real TCP connection with a real HTTP response - the raw socket_listen/accept/read/write builtins under the tcp package's serve() helper.
tcp.serve(port, handler) loops accept -> read -> your handler -> write forever, one connection at a time (this is a single-threaded interpreter, so no OS-thread concurrency) - genuinely a server that answers curl, not a simulation of one.
Code
import "tcp" as tcp
let body = "hello from Larzscript"
let response = "HTTP/1.1 200 OK\r\nContent-Length: " + str(len(body)) + "\r\n\r\n" + body
print(response)
# tcp.serve(8080, fn(request) { return response }) # uncomment to actually listenOutput
HTTP/1.1 200 OK Content-Length: 21 hello from Larzscript
Related
Make an HTTP GET Request in LarzscriptFetch a URL and read its status code and body - or parse a JSON API response directly.→Sign and Verify a Webhook Payload in LarzscriptSign an outgoing webhook payload with HMAC-SHA256, and verify an incoming one - the same shape real payment/webhook providers use.→Hash Data with SHA-256 in LarzscriptCompute a real SHA-256 hash and an HMAC-SHA256 message authentication code.→Generate a UUID in LarzscriptGenerate random (v4) and time-sortable (v7, ULID, nanoid) unique identifiers.→