diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 14:20:18 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 14:20:18 -0400 |
| commit | 2bb2ff871fa1761478442bec5f6a32c9428360a1 (patch) | |
| tree | 9b73df7a0c50c02353ae7bca7c2cd54788ef0077 /examples/http-server | |
| parent | 59845e610f2c90474f34079d27b5f1e07071ded4 (diff) | |
Change method calls to use `foo.baz()` instead of `foo:baz()`
Diffstat (limited to 'examples/http-server')
| -rw-r--r-- | examples/http-server/connection-queue.tm | 18 | ||||
| -rw-r--r-- | examples/http-server/http-server.tm | 52 | ||||
| -rwxr-xr-x | examples/http-server/sample-site/random.tm | 2 |
3 files changed, 36 insertions, 36 deletions
diff --git a/examples/http-server/connection-queue.tm b/examples/http-server/connection-queue.tm index 84f92362..3c8058d5 100644 --- a/examples/http-server/connection-queue.tm +++ b/examples/http-server/connection-queue.tm @@ -5,21 +5,21 @@ func _assert_success(name:Text, val:Int32; inline): struct ConnectionQueue(_connections:@[Int32]=@[], _mutex=pthread_mutex_t.new(), _cond=pthread_cond_t.new()): func enqueue(queue:ConnectionQueue, connection:Int32): - queue._mutex:lock() - queue._connections:insert(connection) - queue._mutex:unlock() - queue._cond:signal() + queue._mutex.lock() + queue._connections.insert(connection) + queue._mutex.unlock() + queue._cond.signal() func dequeue(queue:ConnectionQueue -> Int32): conn : Int32? = none - queue._mutex:lock() + queue._mutex.lock() while queue._connections.length == 0: - queue._cond:wait(queue._mutex) + queue._cond.wait(queue._mutex) - conn = queue._connections:pop(1) - queue._mutex:unlock() - queue._cond:signal() + conn = queue._connections.pop(1) + queue._mutex.unlock() + queue._cond.signal() return conn! diff --git a/examples/http-server/http-server.tm b/examples/http-server/http-server.tm index 1a09b601..649a9e12 100644 --- a/examples/http-server/http-server.tm +++ b/examples/http-server/http-server.tm @@ -19,9 +19,9 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th connections := ConnectionQueue() workers : &[@pthread_t] = &[] for i in num_threads: - workers:insert(pthread_t.new(func(): + workers.insert(pthread_t.new(func(): repeat: - connection := connections:dequeue() + connection := connections.dequeue() request_text := inline C : Text { Text_t request = EMPTY_TEXT; char buf[1024] = {}; @@ -35,7 +35,7 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th } request := HTTPRequest.from_text(request_text) or skip - response := handler(request):bytes() + response := handler(request).bytes() inline C { if (_$response.stride != 1) Array$compact(&_$response, 1); @@ -65,26 +65,26 @@ func serve(port:Int32, handler:func(request:HTTPRequest -> HTTPResponse), num_th repeat: conn := inline C : Int32 { accept(_$sock, NULL, NULL) } stop if conn < 0 - connections:enqueue(conn) + connections.enqueue(conn) say("Shutting down...") for w in workers: - w:cancel() + w.cancel() struct HTTPRequest(method:Text, path:Text, version:Text, headers:[Text], body:Text): func from_text(text:Text -> HTTPRequest?): - m := text:pattern_captures($Pat'{word} {..} HTTP/{..}{crlf}{..}') or return none + m := text.pattern_captures($Pat'{word} {..} HTTP/{..}{crlf}{..}') or return none method := m[1] - path := m[2]:replace_pattern($Pat'{2+ /}', '/') + path := m[2].replace_pattern($Pat'{2+ /}', '/') version := m[3] - rest := m[-1]:pattern_captures($Pat/{..}{2 crlf}{0+ ..}/) or return none - headers := rest[1]:split_pattern($Pat/{crlf}/) + rest := m[-1].pattern_captures($Pat/{..}{2 crlf}{0+ ..}/) or return none + headers := rest[1].split_pattern($Pat/{crlf}/) body := rest[-1] return HTTPRequest(method, path, version, headers, body) struct HTTPResponse(body:Text, status=200, content_type="text/plain", headers:{Text=Text}={}): func bytes(r:HTTPResponse -> [Byte]): - body_bytes := r.body:bytes() + body_bytes := r.body.bytes() extra_headers := (++: "$k: $v$(\r\n)" for k,v in r.headers) or "" return " HTTP/1.1 $(r.status) OK$\r @@ -93,10 +93,10 @@ struct HTTPResponse(body:Text, status=200, content_type="text/plain", headers:{T Connection: close$\r $extra_headers $\r$\n - ":bytes() ++ body_bytes + ".bytes() ++ body_bytes func _content_type(file:Path -> Text): - when file:extension() is "html": return "text/html" + when file.extension() is "html": return "text/html" is "tm": return "text/html" is "js": return "text/javascript" is "css": return "text/css" @@ -105,30 +105,30 @@ func _content_type(file:Path -> Text): enum RouteEntry(ServeFile(file:Path), Redirect(destination:Text)): func respond(entry:RouteEntry, request:HTTPRequest -> HTTPResponse): when entry is ServeFile(file): - body := if file:can_execute(): - Command(Text(file)):get_output()! + body := if file.can_execute(): + Command(Text(file)).get_output()! else: - file:read()! + file.read()! return HTTPResponse(body, content_type=_content_type(file)) is Redirect(destination): return HTTPResponse("Found", 302, headers={"Location"=destination}) func load_routes(directory:Path -> {Text=RouteEntry}): routes : &{Text=RouteEntry} = &{} - for file in (directory ++ (./*)):glob(): - skip unless file:is_file() - contents := file:read() or skip - server_path := "/" ++ "/":join(file:relative_to(directory).components) - if file:base_name() == "index.html": - canonical := server_path:without_suffix("index.html") + for file in (directory ++ (./*)).glob(): + skip unless file.is_file() + contents := file.read() or skip + server_path := "/" ++ "/".join(file.relative_to(directory).components) + if file.base_name() == "index.html": + canonical := server_path.without_suffix("index.html") routes[server_path] = Redirect(canonical) routes[canonical] = ServeFile(file) - else if file:extension() == "html": - canonical := server_path:without_suffix(".html") + else if file.extension() == "html": + canonical := server_path.without_suffix(".html") routes[server_path] = Redirect(canonical) routes[canonical] = ServeFile(file) - else if file:extension() == "tm": - canonical := server_path:without_suffix(".tm") + else if file.extension() == "tm": + canonical := server_path.without_suffix(".tm") routes[server_path] = Redirect(canonical) routes[canonical] = ServeFile(file) else: @@ -142,7 +142,7 @@ func main(directory:Path, port=Int32(8080)): serve(port, func(request:HTTPRequest): if handler := routes[request.path]: - return handler:respond(request) + return handler.respond(request) else: return HTTPResponse("Not found!", 404) ) diff --git a/examples/http-server/sample-site/random.tm b/examples/http-server/sample-site/random.tm index 502618e8..29b93be7 100755 --- a/examples/http-server/sample-site/random.tm +++ b/examples/http-server/sample-site/random.tm @@ -11,7 +11,7 @@ func main(): </head> <body> <h1>Random Number</h1> - Your random number is: $(random:int(1,100)) + Your random number is: $(random.int(1,100)) </body> </html> ") |
