Created
July 28, 2015 23:16
-
-
Save dckc/235c8593073ce2ec938d to your computer and use it in GitHub Desktop.
monte web server example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def [=> UTF8] | _ := import("lib/codec/utf8") | |
def [=> makeHTTPEndpoint] | _ := import("lib/http/server") | |
def [=> tag] | _ := import("lib/http/tag") | |
def simplePage(content): | |
escape badBody: | |
def body := UTF8.encode(content, badBody) | |
def headers := [ | |
"Connection" => "close", | |
"Content-Length" => `${body.size()}`, | |
] | |
return [200, headers, body] | |
catch _: | |
return null | |
def helloWeb(request): | |
escape badRequest: | |
def [[verb, path], headers] := request | |
def body := tag.body( | |
tag.h1("Hello!"), | |
tag.p("This is a Monte webserver."), | |
tag.h2("Request Info"), | |
tag.p(verb), | |
tag.p(path), | |
tag.p(`$headers`)) | |
return simplePage(`$body`) | |
catch _: | |
return null | |
def start(makeSocket): | |
makeHTTPEndpoint(makeSocket()).listen(helloWeb) | |
[=> start] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def [=> strToInt] | _ := import("lib/atoi") | |
def [=> start] | _ := import("../tut/web1") | |
def makeSocket(): | |
def portArg := currentProcess.getArguments().last() | |
def via (strToInt) portNum := portArg | |
return makeTCP4ServerEndpoint(portNum) | |
start(makeSocket) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment