2024-05-20 20:23:38 -07:00
|
|
|
(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)]
|
|
|
|
|
2024-05-20 20:40:11 -07:00
|
|
|
(io.stdout:setvbuf :no)
|
|
|
|
|
|
|
|
(print (: "Request from: %s:%s" :format _G.client-address _G.client-port))
|
2024-05-20 20:23:38 -07:00
|
|
|
(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))
|
2024-05-21 15:13:24 -07:00
|
|
|
(with-open [file (io.open :404.html :r)]
|
|
|
|
(file:read :*a))))
|
2024-05-20 20:23:38 -07:00
|
|
|
|
|
|
|
(client:send "HTTP/1.1 200 OK\r\n\r\n")
|
|
|
|
(client:send content)
|
|
|
|
|
|
|
|
(client:close)))
|
|
|
|
|
|
|
|
client-handler
|