mipha/gemini.moon

62 lines
1.2 KiB
Plaintext

context = require "openssl.ssl.context"
cqueues = require "cqueues"
pkey = require "openssl.pkey"
url = require "net.url"
x509 = require "openssl.x509"
make_tls_context = (cert, key) ->
fin, err = io.open cert
if err != nil
return nil, err
cert, err = x509.new fin\read "*a"
if err != nil
return nil, err
fin\close!
fin, err = io.open key
if err != nil
return nil, err
pkey, err = pkey.new fin\read "*a"
if err != nil
return nil, err
fin\close!
ctx = context.new "TLS", true
ctx\setCertificate cert
ctx\setPrivateKey pkey
ctx
class Server
new: (hdlr) =>
@cq = cqueues.new!
@hdlr = hdlr
attach: (cq) =>
cq\wrap ->
@cq\loop!
listen: (srv) =>
@cq\wrap ->
for conn in srv\clients!
@cq\wrap ->
with conn
_, ip = \peername!
request = \read "*l"
\shutdown "r"
u = url.parse request
if u.path == ""
u.path = "/"
ctx =
:ip
host: u.host
path: u.path
query: u.query
@hdlr\serve ctx, conn
\close!
{
:Server,
:make_tls_context
}