Last active
August 29, 2015 14:15
-
-
Save MadcapJake/a8b59a2ec665b809d009 to your computer and use it in GitHub Desktop.
Ceylon Express Typesafe Interface
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
"Run the module `my.blog`." | |
shared void run() { | |
Application app; | |
app = Application(); | |
dynamic server = app.js.listen(3000, void () { | |
dynamic { | |
console.log( | |
"Express is listening to http://localhost:3000" | |
); | |
} | |
}); | |
} | |
shared dynamic AppInterface { | |
shared formal void listen(Integer port, Anything() callback); | |
} | |
shared class Application() { | |
"Encapsulates express logic that works when not interfaced" | |
dynamic dynRequire() { | |
dynamic { | |
dynamic required = require("express"); | |
//must call here; otherwise gets "type not callable" errors | |
dynamic calledRequired = required(); | |
return calledRequired; | |
} | |
} | |
"Should apply interface to javascript code" | |
AppInterface dynInit() { | |
dynamic { | |
return dynRequire(); | |
} | |
} | |
"Contains the actual js object." | |
shared AppInterface js = dynInit(); | |
} |
Here is the express.js code from the main export file:
exports = module.exports = createApplication;
/**
* Create an express application.
*
* @return {Function}
* @api public
*/
function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
}
It's the same way that koa.js handles it too.
OK there were a couple of issues with some of the core functions when dealing with a native function with additional stuff; I've just fixed that. And now I've just started a little express server with this:
dynamic Response {
shared formal void send(String text);
}
dynamic ExpressApp {
shared formal void listen(Integer port, Anything() callback=()=>null);
shared formal void get(String url, Anything(Anything,Response) callback);
}
shared void run() {
ExpressApp app;
dynamic {
app=require("express")();
}
print(app);
app.get("/", (req,resp) {
print("Responding...");
return resp.send("YEAH!");
});
app.listen(3000,
()=>print("OK, listening on port 3000"));
}
Excellent! I'll pull ceylon-js later this afternoon then and give it a go!
Don't forget to pull ceylon.language
as well, since the latest fixes were in native JS code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just a normal invocation but I've run into two problems getting it to invoke:
dynamic() Application { ... }
where the parens indicate a callable dynamic interface)