Last active
May 2, 2019 21:39
-
-
Save piboistudios/5d262d0609a756f90bfe8ea8ab764751 to your computer and use it in GitHub Desktop.
Attempted CsContainer (sort of builds, c# compilation fails)
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
package; | |
import tink.http.containers.*; | |
import tink.http.Response; | |
import tink.web.routing.*; | |
class App { | |
static function main() { | |
var container = new tink.http.CsContainer(8080); | |
var router = new Router<Root>(new Root()); | |
container.run(function(req) { | |
return router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError); | |
}); | |
trace("Server listening on port 8080"); | |
} | |
} | |
class Root { | |
public function new() {} | |
@:get('/') | |
@:get('/$name') | |
public function hello(name = 'World') | |
return 'Hello, $name!'; | |
} |
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
package tink.http; | |
import tink.http.*; | |
import tink.http.Request; | |
import cs.system.net.*; | |
import tink.http.Container; | |
import tink.http.Header; | |
using Lambda; | |
import tink.CoreApi; | |
import tink.io.*; | |
class CsContainer implements Container { | |
var listener:HttpListener; | |
public function new(port:Int) { | |
var addr = IPAddress.Parse("127.0.0.1"); | |
this.listener = new HttpListener(); | |
// MUST RUN: | |
// netsh add urlacl url=http://+:$port/ user=DOMAIN\username listen=yes | |
// before listener.Start() | |
this.listener.Prefixes.Add('http://+:$port/'); | |
} | |
public function run(handler:tink.http.Handler) { | |
return Future.async(cb -> { | |
listener.Start(); | |
trace("Listening..."); | |
cb(Running({ | |
shutdown: _ -> { | |
listener.Close(); | |
return true; | |
}, | |
failures: Signal.trigger() | |
})); | |
while(true) { | |
var context = listener.GetContext(); | |
var request = context.Request; | |
var response = context.Response; | |
var sink = Sink.ofOutput('cs-sink', new cs.io.NativeOutput(response.OutputStream)); | |
var source = Source.ofInput('cs-input', new cs.io.NativeInput(request.InputStream)); | |
handler.process( | |
new IncomingRequest( | |
request.UserHostAddress, | |
new IncomingRequestHeader( | |
cast(request.HttpMethod), | |
request.Url.ToString(), | |
request.ProtocolVersion.ToString(), | |
[for(key in request.Headers.AllKeys) new HeaderField(key, ({iterator: () -> request.Headers.GetValues(key).iterator() }).array().join(';'))] | |
), | |
Plain(source) | |
) | |
).handle(out -> { | |
response.StatusCode = out.header.statusCode; | |
for(header in out.header) { | |
response.AddHeader(header.name, header.value); | |
} | |
out.body.pipeTo(sink).handle(_ -> { | |
response.Close(); | |
}); | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment