|
| 1 | +# mu-javascript-template |
| 2 | + |
| 3 | +Template for writing mu.semte.ch services in JavaScript |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +Create a new folder. Add the following Dockerfile: |
| 8 | + |
| 9 | + FROM semtech/mu-javascript-template |
| 10 | + MAINTAINER Aad Versteden <[email protected]> |
| 11 | + |
| 12 | +Create your microservice: |
| 13 | + |
| 14 | + import { app, query } from 'mu'; |
| 15 | + |
| 16 | + app.get('/', function( req, res ) { |
| 17 | + res.send('Hello mu-javascript-template'); |
| 18 | + } ); |
| 19 | + |
| 20 | + |
| 21 | + app.get('/query', function( req, res ) { |
| 22 | + var myQuery = ` |
| 23 | + SELECT * |
| 24 | + WHERE { |
| 25 | + GRAPH <http://mu.semte.ch/application> { |
| 26 | + ?s ?p ?o. |
| 27 | + } |
| 28 | + }`; |
| 29 | + |
| 30 | + query( myQuery ) |
| 31 | + .then( function(response) { |
| 32 | + res.send( JSON.stringify( response ) ); |
| 33 | + }) |
| 34 | + .catch( function(err) { |
| 35 | + res.send( "Oops something went wrong: " + JSON.stringify( err ) ); |
| 36 | + }); |
| 37 | + } ); |
| 38 | + |
| 39 | +## Requirements |
| 40 | + |
| 41 | + - **database link**: You need a link to the `database` which exposes a sparql endpoint on `http://database:8890/sparql`. In line with other microservices. |
| 42 | + |
| 43 | +## Imports |
| 44 | + |
| 45 | +The following importable variables are available: |
| 46 | + |
| 47 | + - `app`: The application on which routes can be added |
| 48 | + - `query(query) => Promise`: Function for sending queries to the triplestore |
| 49 | + - `update(query) => Promise`: Function for sending updates to the triplestore |
| 50 | + |
| 51 | +You can either import specific attributes from the mu library, or import the whole mu object. |
| 52 | + |
| 53 | +An example of importing specific variables: |
| 54 | + |
| 55 | + import { app, query } from 'mu'; |
| 56 | + |
| 57 | + app.get('/', function( req, res ) { |
| 58 | + res.send('Hello mu-javascript-template'); |
| 59 | + } ); |
| 60 | + |
| 61 | +An example of importing the whole library: |
| 62 | + |
| 63 | + import mu from 'mu'; |
| 64 | + |
| 65 | + mu.app.get('/', function( req, res ) { |
| 66 | + res.send('Hello using full import'); |
| 67 | + } ); |
| 68 | + |
| 69 | +## Developing with the template |
| 70 | + |
| 71 | +When developing, you can use the template image, mount the volume with your sources on `/app` and add a link to the database. |
| 72 | + |
| 73 | + dr run --link virtuoso:database \ |
| 74 | + -v `pwd`:/app \ |
| 75 | + -p 8888:80 \ |
| 76 | + --name my-js-test \ |
| 77 | + semtech/mu-javascript-template |
| 78 | + |
| 79 | + |
0 commit comments