mirror of
https://codeberg.org/bunbun/bunbun.dev
synced 2025-01-22 12:44:29 -08:00
41 lines
1.0 KiB
Plaintext
41 lines
1.0 KiB
Plaintext
|
(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)]
|
||
|
|
||
|
(print (: "Request from: %s:%s" :format (client:getpeername)))
|
||
|
(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))
|
||
|
"404 - Not Found"))
|
||
|
|
||
|
(client:send "HTTP/1.1 200 OK\r\n\r\n")
|
||
|
(client:send content)
|
||
|
|
||
|
(client:close)))
|
||
|
|
||
|
client-handler
|