HTTP server
Save as http-server.atom and run atom run http-server.atom -- 18080. Choose an unused loopback port and use the same one for the client. The server handles one request, then closes its listener. With no client it times out after ten seconds.
use http
use net
use os
func respond(request):
if request.method != 'GET':
return {status = 405, body = 'method not allowed'}
return {
status = 200
headers = {['content-type'] = 'text/plain; charset=utf-8'}
body = 'Received {request.target}'
}
port = convert(i64, get(os.args, 0, '18080'))
listener = http.listen('127.0.0.1', port, {timeout = 10})
try:
http.serve(listener, respond, 1)
finally:
net.close(listener)