Website/server.spl
2024-09-19 20:59:04 +02:00

57 lines
1.3 KiB
Text

"#http/server.spl" import
func destub { html | with html title ;
stub.html
:replace<"+title" title>
:replace<"+body" html>
:replace<"Lorem ipsum" lorem-ipsum>
}
def lorem-ipsum "lorem-ipsum" read-file:replace<"\n" "<br>"> =lorem-ipsum
def stub.html "stub.html" read-file =stub.html
def style.css "style.css" read-file =style.css
def 404.html "404.html" read-file destub<"404"> =404.html
def index.html "index.html" read-file destub<"Home"> =index.html
def features.html "features.html" read-file destub<"Features"> =features.html
func main { mega | with args ;
def server net:http:Server:new<"localhost" 13130> =server
"server is listening on 13130" println;
while { 1 } {
server:accept { | with client ;
catch {
client:read;
client handle-client;
} { with err ;
"A request has errored out with " err:message concat println;
"at: " println;
&println err:trace:foreach;
}
} fork pop
}
0
}
func handle-client { | with client ;
client:path "/" eq if {
index.html client:write-ok:write-html-body:finish;
2 stop
}
client:path "/features" eq if {
features.html client:write-ok:write-html-body:finish;
2 stop
}
client:path "/style.css" eq if {
style.css client:write-ok:write-content-type<"text/css">:write-str-body:finish;
2 stop
}
404.html client:write-head<404 "Not found">:write-html-body:finish;
}