Created
June 24, 2012 00:06
-
-
Save coopernurse/2980627 to your computer and use it in GitHub Desktop.
Barrister RPC + angularjs + coffeescript
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
# | |
# generic Barrister transport - can be used with any endpoint | |
# | |
angularTrans = (url, $http) -> | |
return (req, callback) -> | |
reqJson = Barrister.JSON_stringify(req) | |
post = $http.post url, reqJson | |
post.success (data, status, headers, config) -> | |
callback data | |
post.error (data, status, headers, config) -> | |
callback data | |
angular.module('calcApp', []).factory 'BarristerSvc', ($http) -> | |
svc = { } | |
client = new Barrister.Client angularTrans('/calc', $http), { coerce: true } | |
# TODO: race condition here. svc.Calculator may not be | |
# initialized before user clicks the 'add' button in the UI | |
# still playing around with the best way to init a service in AngularJS that | |
# has async code | |
client.loadContract (err) -> | |
console.log "setting Calculator slot on svc" | |
svc.Calculator = client.proxy "Calculator" | |
svc | |
window.CalcCtrl = ($scope, BarristerSvc) -> | |
$scope.result = "" | |
$scope.add = -> | |
console.log "adding #{$scope.a} to #{$scope.b}" | |
BarristerSvc.Calculator.add $scope.a, $scope.b, (err, result) -> | |
console.log "get err: #{JSON.stringify(err)}" | |
console.log "got result: #{result}" | |
$scope.result = result | |
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
<!doctype html> | |
<html ng-app="calcApp"> | |
<head> | |
<script src="http://code.angularjs.org/angular-1.0.1-ffb27013.min.js"></script> | |
<script src="js/barrister.browser.js"></script> | |
</head> | |
<body> | |
<h2>Calculator</h2> | |
<div ng-controller="CalcCtrl"> | |
<form ng-submit="add()"> | |
<input type="text" ng-model="a" size="5"> | |
<input type="text" ng-model="b" size="5"> | |
<input class="btn-primary" type="submit" value="add"> | |
result: <span>{{result}}</span> | |
</form> | |
</div> | |
<!-- contains compiled calc.coffee output --> | |
<script src="js/calc.js"></script> | |
</body> | |
</html> |
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
#!/usr/bin/env python | |
# this is just my example calc server.py from the barrister web site | |
from flask import Flask, request, make_response | |
import barrister | |
# Our implementation of the 'Calculator' interface in the IDL | |
class Calculator(object): | |
# Parameters match the params in the functions in the IDL | |
def add(self, a, b): | |
return a+b | |
def subtract(self, a, b): | |
return a-b | |
contract = barrister.contract_from_file("../calc.json") | |
server = barrister.Server(contract) | |
server.add_handler("Calculator", Calculator()) | |
app = Flask(__name__) | |
@app.route("/calc", methods=["POST"]) | |
def calc(): | |
resp_data = server.call_json(request.data) | |
resp = make_response(resp_data) | |
resp.headers['Content-Type'] = 'application/json' | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment