Skip to content

Services API

Overview

The Services API allows for managing and testing various services in the application. It provides endpoints to create a services table, register, update, delete, and test services.

Endpoints

Create Services Table

  • URL: /api/authService/service/table
  • Method: POST
  • Description: Creates the services table in the database.
  • Response:
  • 200 OK: Services table successfully created.

Register Service

  • URL: /api/authService/service
  • Method: POST
  • Description: Registers a new service with the provided name, port, and URI.
  • Request Body:
    {
      "name": "service_name",
      "port": 3000,
      "uri": "http://localhost"
    }
    
  • Response:
    {
      "id": 1,
      "name": "service_name",
      "port": 3000,
      "uri": "http://localhost"
    }
    
  • Example Request:
    curl -X POST http://localhost:3000/api/authService/service -H "Content-Type: application/json" -d '{"name": "example-service", "port": 3000, "uri": "http://localhost"}'
    
  • Example Response:
    {
      "id": 1,
      "name": "example-service",
      "port": 3000,
      "uri": "http://localhost"
    }
    

Get All Services

  • URL: /api/authService/service
  • Method: GET
  • Description: Retrieves a list of all registered services.
  • Response:
    [
      {
        "id": 1,
        "name": "example-service",
        "port": 3000,
        "uri": "http://localhost"
      }
    ]
    
  • Example Request:
    curl -X GET http://localhost:3000/api/authService/service
    
  • Example Response:
    [
      {
        "id": 1,
        "name": "example-service",
        "port": 3000,
        "uri": "http://localhost"
      }
    ]
    

Get Service by ID

  • URL: /api/authService/service/:id
  • Method: GET
  • Description: Retrieves the details of a specific service by its ID.
  • Response:
    {
      "id": 1,
      "name": "example-service",
      "port": 3000,
      "uri": "http://localhost"
    }
    
  • Example Request:
    curl -X GET http://localhost:3000/api/authService/service/1
    
  • Example Response:
    {
      "id": 1,
      "name": "example-service",
      "port": 3000,
      "uri": "http://localhost"
    }
    

Update Service by ID

  • URL: /api/authService/service/:id
  • Method: PUT
  • Description: Updates the details of a specific service by its ID.
  • Request Body:
    {
      "name": "updated_service_name",
      "port": 4000,
      "uri": "http://localhost"
    }
    
  • Response:
    {
      "id": 1,
      "name": "updated_service_name",
      "port": 4000,
      "uri": "http://localhost"
    }
    
  • Example Request:
    curl -X PUT http://localhost:3000/api/authService/service/1 -H "Content-Type: application/json" -d '{"name": "updated-service", "port": 4000, "uri": "http://localhost"}'
    
  • Example Response:
    {
      "id": 1,
      "name": "updated-service",
      "port": 4000,
      "uri": "http://localhost"
    }
    

Delete Service by ID

  • URL: /api/authService/service/:id
  • Method: DELETE
  • Description: Deletes a service by its ID.
  • Response:
  • 200 OK: Service successfully deleted.
  • Example Request:
    curl -X DELETE http://localhost:3000/api/authService/service/1
    
  • Example Response:
    {
      "message": "Service deleted successfully"
    }
    

Test All Services

  • URL: /api/authService/service/test
  • Method: GET
  • Description: Tests all registered services and returns their status.
  • Response:
    [
      {
        "id": 1,
        "name": "example-service",
        "port": 3000,
        "uri": "http://localhost",
        "status": "OK"
      }
    ]
    
  • Example Request:
    curl -X GET http://localhost:3000/api/authService/service/test
    
  • Example Response:
    [
      {
        "id": 1,
        "name": "example-service",
        "port": 3000,
        "uri": "http://localhost",
        "status": "OK"
      }
    ]
    

Example Usage

Register Service with Axios

import axios from 'axios';

axios.post('/api/authService/service', {
  name: 'example-service',
  port: 3000,
  uri: 'http://localhost'
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

Get Service by ID with Axios

axios.get('/api/authService/service/1')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

Error Handling

  • 422 Unprocessable Entity: Invalid data provided (e.g., missing required fields).
  • 400 Bad Request: Invalid request or database error.
  • 404 Not Found: Service not found by ID.
  • 500 Internal Server Error: Server errors, such as issues with database operations.