bunbun.dev/client-handler.fnl
2024-05-21 15:14:49 -07:00

44 lines
1.1 KiB
Fennel

(local utils (require :utils))
(local read-pages (. (require :page-handler) :read-pages))
(local client-handler {})
(fn client-handler.handle-client [client]
(let [request (client:receive :*l)]
(io.stdout:setvbuf :no)
(print (: "Request from: %s:%s" :format _G.client-address _G.client-port))
(print request)
(while true
(local request (client:receive :*l))
(print request)
(if (or (= request "") (not request))
(lua :break)))
(var uri (request:match "GET%s+([^%s]+)%s+HTTP"))
(set uri (uri:gsub "^/" ""))
(var content "")
(read-pages)
;; Maybe use scss instead of css
;; (uri:match "%.css$") (.. "./" (utils.compile-sass (uri:gsub "%.css$" ".scss")))
(set content
(if (= uri "") (. _G.files :main)
(. _G.files uri) (. _G.files uri)
(utils.file-exists? uri) (with-open [file (io.open uri :r)]
(file:read :*a))
(with-open [file (io.open :404.html :r)]
(file:read :*a))))
(client:send "HTTP/1.1 200 OK\r\n\r\n")
(client:send content)
(client:close)))
client-handler