Before we begin making requests to our microservice, we need to initialize its address and port as well as the route, this is how it should be done.
Information
You can find a sample repo on GitHub that's using this library, here.
You can really see this as passing JSON object between your server and your microservice, but instead of passing raw JSON, we can serialize and deserialize it into useful objects actually used in your code.
Initialization of the API
Here an example of how to initialize the API on Spigot and how to setup your Node.js environment.
Parameter definitions
Parameter
Type
Default
Description
.host()
String
localhost
The microservice host
.route()
String
'/api'
The main API route
.port()
int
8080
The microservice port
.protocol
Protocol
Protocol.HTTP
The HTTP protocol
Creating a new Microservice object
To create a object, we use the ServiceBuilder class.
/**
* Initialize it with default values like this;
*/
new ServiceBuilder().build();
/**
* Initialize it with custom values like this;
*/
new ServiceBuilder()
/* To change default values, add .port(), .()... here. */
.host("customhost.com")
.protocol(Protocol.HTTPS)
.build();
Establish the host of the API in your Java plugin, which will be utilized for requests.
Plugin.java
import micro.api.*;
public final class Plugin extends JavaPlugin {
public ServiceHost host;
public PlayerRepository repo;
@Override
public void onEnable() {
/**
* This gives `http://localhost:8080/api` according
* to the default values stated here above.
*/
host = new ServiceBuilder().build().getHost();
repo = new PlayerRepository(host);
}
}
Step 1: Install the Microservice package from NPM:
npm install @spigotmc/microservice
Or if you're using Yarn:
yarn add @spigotmc/microservice
Step 2: Initialize a new microservice. Also, don't forget to add your API route.
index.ts
import Microservice from "@spigotmc/microservice";
import route from "./routes";
/**
* MySQL credentials
*/
const mysql = {
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
database: 'minecraft',
};
/**
* Create a new MySQL connection.
*/
export const connection = createConnection(mysql);
connection.connect();
/**
* Create a new Microservice instance.
*/
const service = Microservice.host(8080);
service.setDefiningRoute('/api', route.register());
Step 3: Make a separate file for all your routes to join, then later import that route in your microservice using #setDefiningRoute() as shown above.
Then create a new router and export it when you're finished adding all the sub-routes.
routes.ts
import { Router } from '@spigotmc/microservice';
import { playerService } from './services/player.service';
/**
* Create a new Router from the Microservice library.
*/
const router = new Router();
/**
* Add any sub-routes to the main router.
*/
router.use('/player', playerService);
/**
* Export the router so that we can use
* it in the main class where you decl-
* ared the microservice.
*/
export default router;
With that, the API has been successfully initialized on both your Java plugin and Node.js platform.