Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to add new computers via the web interface #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MRID0007
Copy link

Description:

This pull request introduces the functionality to add new computers via the web interface, enhancing the usability of the GO-REST-WOL application. Below is a detailed description of the changes made:

1. Front-end Changes (pages/index.html):

  • New Form for Adding Computers:

    • Added an HTML form to allow users to input the name, MAC address, and broadcast IP address of a new computer.
    • The form includes the following fields:
      • Computer Name
      • MAC Address
      • Broadcast IP Address
  • JavaScript for Form Handling:

    • Implemented a JavaScript function to handle the form submission.
    • The function sends a POST request to the /api/add/computer endpoint with the new computer details.
    • If the request is successful, a success message "Computer added successfully" is displayed, and the new computer is added to the table dynamically.
  • Snackbar Notifications:

    • Updated the snackbar notification mechanism to display appropriate success or error messages based on the response from the server.
<form id="newComputerForm">
  <div class="form-group">
    <label for="computerName">Computer Name</label>
    <input type="text" class="form-control" id="computerName" placeholder="Enter computer name" required>
  </div>
  <div class="form-group">
    <label for="macAddress">MAC Address</label>
    <input type="text" class="form-control" id="macAddress" placeholder="Enter MAC address" required>
  </div>
  <div class="form-group">
    <label for="broadcastIp">Broadcast IP Address</label>
    <input type="text" class="form-control" id="broadcastIp" placeholder="Enter broadcast IP address" required>
  </div>
  <button type="submit" class="btn btn-primary">Add Computer</button>
</form>

2. Back-end Changes:

rest.go:
  • New Endpoint for Adding Computers:
    • Added a new REST API endpoint /api/add/computer to handle POST requests for adding new computers.
    • The handler function restAddComputer processes the incoming request, appends the new computer to the list, and saves the updated list to the CSV file.
// restAddComputer - REST Handler for adding a new computer
func restAddComputer(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "application/json")

  var newComputer Computer
  if err := json.NewDecoder(r.Body).Decode(&newComputer); err != nil {
    http.Error(w, err.Error(), http.StatusBadRequest)
    return
  }

  // Append the new computer to the list
  ComputerList = append(ComputerList, newComputer)

  // Save the updated list to the CSV file
  if err := saveComputerList(DefaultComputerFilePath, ComputerList); err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }

  // Return success message
  response := WakeUpResponseObject{
    Success: true,
    Message: "Computer added successfully",
    ErrorObject: nil,
  }
  json.NewEncoder(w).Encode(response)
}
main.go:
  • Registering the New Endpoint:
    • Registered the new /api/add/computer endpoint in the HTTP router.
// Define route for adding a new computer
router.HandleFunc("/api/add/computer", restAddComputer).Methods("POST")
data.go:
  • Function to Save Computer List to CSV:
    • Implemented a function saveComputerList to save the updated computer list to the computer.csv file.
// SaveComputerList saves the computer list to a CSV file
func saveComputerList(computerCsvFilePath string, computers []Computer) error {
  file, err := os.OpenFile(computerCsvFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  if err != nil {
    return err
  }
  defer file.Close()

  return gocsv.MarshalFile(&computers, file)
}

3. Testing:

  • Local Testing:

    • Built and tested the Docker image locally to ensure the new functionality works as expected.
    • Verified that new computers can be added via the web interface and the Wake-on-LAN functionality remains operational.
  • Synology NAS Testing:

    • Deployed and tested the updated application on a Synology NAS to replicate the production environment.
    • Confirmed that the new functionality works seamlessly in this environment as well.

By merging this pull request, you will enable users to add new computers via the web interface, improving the overall usability and functionality of the GO-REST-WOL application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant