Send response: Microservice
This is more server-side, not spigot-server, but server-server, as in microservice-server, get it? Yeah, me neither
Example for making your first request
In this class, I will demonstrate the usage of all four HTTP methods. If you have previous experience with microservices, the material should be easily comprehensible.
Service files are usually nested in the /service(s)/* folder in your app. Files that end with .service.ts, hold the logic for when they get requested from our Spigot server.
import { Router } from '@spigotmc/microservice';
import { connection } from '..';
/**
 * Create a new router for this service.
 */
const service = new Router();
/**
 * This is only one example of four methods
 */
service.get('/load', (query) => {
  /**
   * The UUID of the player gets passed through
   * the query because this is a GET request.
   */
  const uuid: string = query.uuid;
  /**
   * This is the query that gets executed.
   */
  const sql = `SELECT * FROM players WHERE uuid='${}'`;
  /**
   * Execute the query and wait for the result.
   */
  return new Promise((resolve, reject) => {
    connection.query(sql, (error, results) => {
      /**
       * Reject the promise if an error occured.
       */
      if (error) return reject(error);
      /**
       * Resolve the promise with player's data.
       */
      resolve();
    });
  });
});
/**
 * Other endpoints like `/coin_update`, etc.,
 * should be implemented in this same file.
 */
export { service as playerService };import micro.api.service.*;
import micro.api.payload.*;
public class PlayerRepository extends ServiceEndpoint {
   /**
    * This points to `http://localhost:5000/api/player`
    */
    public PlayerRepository() {
       super("/player");
    }
    /**
     * Example of GET request, get player's data.
     * @return a new PlayerClient object.
     */
    public PlayerClient loadPlayer(UUID uuid) {
        Query query = new Query();
        query.add("uuid", uuid.toString());
        return super.get("/load", PlayerClient.class, query);
    }
    
    /**
     * Example of POST request, update player's coins.
     * @return The updated PlayerClient object.
     */
    public PlayerClient addFriend(UUID uuid, UUID target) {
        Body body = new Body();
        body.add("uuid", uuid.toString());
        body.add("target", target.toString());
        return super.post("/friend_add", PlayerClient.class, body);
    }
    
    /**
     * Example of PUT request, update player's coins.
     * @return The updated PlayerClient object.
     */
    public PlayerClient updateCoins(UUID uuid, int amount) {
        Body body = new Body();
        body.add("uuid", uuid.toString());
        body.add("coins", amount);
        return super.put("/coin_update", PlayerClient.class, body);
    }
    
    /**
     * Example of DELETE request, delete player data.
     * @return The deleted PlayerClient object.
     */
    public PlayerClient deletePlayer(UUID uuid) {
        Query query = new Query();
        query.add("uuid", uuid.toString());
        
        return super.delete("/delete", PlayerClient.class, query);
    }
}public class PlayerClient {
   /**
    * These fields will get initialized when
    * loading the player from the service.
    */
   private String uuid;
   private int level;
   private  rank;
   private int coins;
   private List<String> friends;
   public UUID getUUID() { 
      return UUID.fromString(uuid); 
   }
   public int getLevel() {
       return level;
   }
   public  getRank() {
      return rank;
   }
   public int getCoins() {
      return coins;
   }
   public List<String> getFriends() {
       return friends;
   }
} Once you made your first service file, you should import this in the /api router.
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;Last updated
