Giter Club home page Giter Club logo

apigateway-workshop's Introduction

API Gateway Workshop

  1. Install Java 8 and Maven
  2. Use Chrome and install JSONView https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?hl=en
  3. git clone [email protected]:willtran-/apigateway-workshop.git
  4. cd apigateway-workshop/nextbus
  5. mvn spring-boot:run
  6. http://localhost:8080/api/v1/agencies/

An API Gateway application starts with src/main/resources/app.js. Here, a Router is defined, which is responsible for handling HTTP requests and generating responses. A Router with no defined routes would look like this:

var Router = require("Router");

var appRouter = new Router();
\\ Your routes would go here
module.exports = appRouter;

Please see TODO: port Router docs over

Every API Gateway application is based on Spring Boot. Sometimes you'll want to write something in Java or use some pre-existing Java library. If you can write a Spring Bean that encapsulates what you want to do in Java, it's very easy to access that bean and call it in JavaScript:

package com.example;

import org.springframework.stereotype.Component;

@Component
public class HelloService {

    public String getHelloMessage() {
        return "Hello from a Spring Bean!";
    }

}
var helloService = spring.getBean("helloService");
appRouter.get("/hello-spring", function(req,res) {
  res.setBody({message: helloService.getHelloMessage()});
});

In API Gateway, modules are defined in Common-JS style. A Module looks like this:

src/main/resources/modules/echoModule.js

var echoFunction = function(someValue) {
  return { "echo" : someValue };
};
module.exports = {
  echo : echoFunction
};

You get a handle on the module by using require(), passing in either the absolute path (leading slash) or relative path (no leading slash, ./ or ../ are ok) to the module. You don't need the .js extension for js files either. Then you can use the module like this:

var echoModule = require('/modules/echoModule');
appRouter.get("/echo/:message", function(req, res, message) {
  res.setBody(echoModule.echo(message));
});

We've integrated Jasmine 2.0 as a behavior-driven testing framework: http://jasmine.github.io/2.0/introduction.html. Here's a test for the echo module:

src/test/resources/spec/echoModuleSpec.js

var echoModule = require('/modules/echoModule.js');
describe("echoModule", function() {
  describe("#echo", function() {
    it("returns an object containing the message it recieved ", function() {
      expect(echoModule.echo("someMessage")).toEqual({echo:"someMessage"});
    });
  });
});

You place your spec files in src/test/resources/spec. These tests will run as part of the Maven lifecycle, and you can also run them interactively. With interactive testing, you can iterate quickly between writing tests, writing application code, and running the tests, all without having to restart the server.

To turn on interactive testing, set the property enableInteractiveTesting=true in src/main/resources/application.properties. Then restart the server, and go to http://localhost:8080/test/index.html. You see the results of all the tests, and you can click on an individual spec to re-run just that spec.

apigateway-workshop's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.