Skip to content

Milkjuiceman/MicroserviceA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Microservice A

This program is a registered user list microservice that allows for adding, listing, retrieving, and deleting users. It uses a file-based communication.

Important Notes

  1. The microservice must be running in a separate process for this communication to work.
  2. Make sure the microservice (user_management.py) is started before attempting to send commands.
  3. The microservice operates using a file-based communication pattern where commands are written to a commands.txt file and responses are read from a results.txt file.

How to REQUEST Data

To request data from the microservice:

  1. Write your command to the commands.txt file
  2. Each command should be written as a single line of text
  3. Wait for the response (1-2 seconds is usually sufficient)
  4. Read the response from the results.txt file

Supported Commands:

Command Format Description
add add <username> <password> Create a new user
list list List all users
get get <username> Get details for a specific user
delete delete <username> Delete a specific user
purge purge Delete all users

Example Code for Requesting Data:

def send_command(command):
    """
    Send a command to the user management microservice.
    
    Args:
        command (str): The command to send (e.g., "add user1 pass123")
        
    Returns:
        bool: True if the command was sent successfully
    """
    try:
        # Write the command to the commands.txt file
        with open("commands.txt", 'w') as f:  # Use 'w' mode to ensure clean command
            f.write(command + "\n")
        return True
    except Exception as e:
        print(f"Error sending command: {e}")
        return False

# Example usage:
# To add a user
send_command("add testuser password123")

# To list all users
send_command("list")

# To get a specific user
send_command("get testuser")

# To delete a user
send_command("delete testuser")

How to RECEIVE Data

After sending a command, the microservice will process it and write the result to the results.txt file. To receive the response:

  1. Wait a short time for processing (1-2 seconds)
  2. Read the contents of the results.txt file
  3. Parse the relevant information from the response

Response Format:

Responses are timestamped entries in the following format:

[YYYY-MM-DD HH:MM:SS] Response message

For user details and lists, the format is more structured:

===== USER DETAILS =====
ID: <uuid>
Username: <username>
Password: <password>
Created At: <timestamp>
=======================

or

===== USER LIST =====
ID                                   Username             Password             Created At                    
----------------------------------------------------------------------------------------------------------
<uuid>                               <username>           <password>           <timestamp>
<uuid>                               <username>           <password>           <timestamp>
----------------------------------------------------------------------------------------------------------
Total users: <count>
====================

Example Code for Receiving Data:

import time

def receive_response():
    """
    Receive and parse a response from the user management microservice.
    
    Returns:
        str: The response message
    """
    time.sleep(1.5)  # Adjust wait time as needed
    
    try:
        with open("results.txt", 'r') as f:
            content = f.read()
        
        # Parse the response
        sections = content.split("[")
        if len(sections) > 1:
            # Get the latest response 
            latest_response = "[" + sections[-1]
            
            # Special handling for structured data
            if "USER DETAILS" in latest_response:
                details_index = latest_response.find("===== USER DETAILS =====")
                return latest_response[details_index:]
            elif "USER LIST" in latest_response:
                list_index = latest_response.find("===== USER LIST =====")
                return latest_response[list_index:]
            else:
                return latest_response.strip()
        else:
            return "No response found"
    except Exception as e:
        return f"Error reading response: {e}"

# Example Command
send_command("get testuser")

response = receive_response()
print(response)

UML Sequence Diagram

image

About

Microservice for Neil Juan Main program

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors