Skip to content

Instantly share code, notes, and snippets.

@MadcapJake
Last active August 29, 2015 14:15
Show Gist options
  • Save MadcapJake/a8b59a2ec665b809d009 to your computer and use it in GitHub Desktop.
Save MadcapJake/a8b59a2ec665b809d009 to your computer and use it in GitHub Desktop.
Ceylon Express Typesafe Interface
"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();
}
@chochos
Copy link

chochos commented Feb 12, 2015

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"));
}

@MadcapJake
Copy link
Author

Excellent! I'll pull ceylon-js later this afternoon then and give it a go!

@chochos
Copy link

chochos commented Feb 12, 2015

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