Skip to content

Instantly share code, notes, and snippets.

@arteymix
Last active February 9, 2016 21:00
Show Gist options
  • Save arteymix/0f3acedc8d6e08d4cbf9 to your computer and use it in GitHub Desktop.
Save arteymix/0f3acedc8d6e08d4cbf9 to your computer and use it in GitHub Desktop.
Controller using GObject Introspection
public HandlerCallback controller (string namespace) {
return (req, res, next, ctx) => {
var controller_type = Type.from_name ("%s%s".printf (namespace, ctx["controller"]));
if (!controller_type.is_classed ())
throw new ClientError.NOT_FOUND ("'%s' is not an existing controller", ctx["controller"]);
var controller = Object.new (controller_type);
var action_info = GI.Repository.get_default ().find_by_name (namespace,
"%s_action_%s".printf (ctx["controller"],
ctx["action"].get_string () ?? "index"));
if (action_info is UnresolvedInfo)
throw new ClientError ("no such action '%s'", ctx["action"].get_string () ?? "index");
// assume you go through http://valadoc.org/#!wiki=gobject-introspection-1.0/index
//
// - obtain an CallableInfo for the action
// - obtain the function symbol
// - build the arguments (tricky part)
CallableInfo.invoke (action_info as CallableInfo, callable_symbol, arguments, 4, null, 0, true, true);
}
}
using Valum;
using VSGI.HTTP;
var app = new Router ();
app.rule (Method.GET | Method.POST, "<controller>(/<action>)", controller);
new Server ("org.valum.example.Controller", app.handle).run ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment