added IP address to the agents

This commit is contained in:
gurkenhabicht 2025-01-10 02:47:33 +01:00
parent 2f2485fc25
commit 35ec77e5c6
5 changed files with 23 additions and 9 deletions

View File

@ -17,7 +17,7 @@ const(
webServerAddr = "127.0.0.1:3333" webServerAddr = "127.0.0.1:3333"
webSocketAddr = "127.0.0.1:5555" webSocketAddr = "127.0.0.1:5555"
registerURL = "http://" + webServerAddr + "/agents" registerURL = "http://" + webServerAddr + "/agents"
wsURL = "ws://" + webSocketAddr + "/ws" wsURL = "ws://" + webSocketAddr + "/data"
) )
type Agent struct { type Agent struct {

View File

@ -3,7 +3,7 @@
drop table if exists agents; drop table if exists agents;
create table agents ( create table agents (
id UUID default uuid() Primary Key, id UUID default uuid() Primary Key,
agentId int, agentId int unique,
agentName varchar(255), agentName varchar(255),
IPv4Address varchar(15), IPv4Address varchar(15),
initialContact timestamp, initialContact timestamp,
@ -11,4 +11,4 @@ create table agents (
status Boolean status Boolean
); );
insert into agents (agentId, agentName, initialContact, lastContact) values ( 99, "testAgent", NOW(), NOW()); insert into agents (IPv4Address, agentId, agentName, initialContact, lastContact) values ( '127.0.0.1', 'testAgent', NOW(), NOW());

16
main.go
View File

@ -49,6 +49,8 @@ type Agent struct {
AgentName string `json:"agentName"` AgentName string `json:"agentName"`
InitialContact string `json:"initialContact"` InitialContact string `json:"initialContact"`
LastContact string `json:"lastContact"` LastContact string `json:"lastContact"`
IPv4Address string `json:"IPv4Address"`
Status string `json:"status"`
} }
func init() { func init() {
@ -243,11 +245,12 @@ func createAgent(w http.ResponseWriter, r * http.Request) {
agentName := r.FormValue("agentName") agentName := r.FormValue("agentName")
agentId := r.FormValue("agentId") agentId := r.FormValue("agentId")
IPv4Address := r.FormValue("IPv4Address")
// initalContact := r.FormValue("initialContact") // initalContact := r.FormValue("initialContact")
// lastContact := r.FormValue("lastContact") // lastContact := r.FormValue("lastContact")
query := "INSERT INTO agents (agentId, agentName, initialContact, lastContact) VALUES (?, ?, NOW(), NOW())" query := "INSERT INTO agents (agentId, agentName, IPv4Address, initialContact, lastContact) VALUES (?, ?, ?, NOW(), NOW())"
_, err = db.Exec(query, agentId, agentName) _, err = db.Exec(query, agentId, agentName, IPv4Address)
if err != nil { if err != nil {
http.Error(w, "Failed to create agent", http.StatusInternalServerError) http.Error(w, "Failed to create agent", http.StatusInternalServerError)
return return
@ -266,9 +269,10 @@ func updateAgent(w http.ResponseWriter, r *http.Request, agentId string) {
agentName := r.FormValue("agentName") agentName := r.FormValue("agentName")
lastContact := r.FormValue("lastContact") lastContact := r.FormValue("lastContact")
IPv4Address := r.FormValue("IPv4Address")
query := "UPDATE agents SET agentName = ?, lastContact = ? where agentId = ?" query := "UPDATE agents SET agentName = ?, IPv4Address= ?, lastContact = ? where agentId = ?"
_, err = db.Exec(query, agentName, lastContact, agentId) _, err = db.Exec(query, agentName, IPv4Address, lastContact, agentId)
if err != nil { if err != nil {
http.Error(w, "Failed to update agent", http.StatusInternalServerError) http.Error(w, "Failed to update agent", http.StatusInternalServerError)
return return
@ -278,7 +282,7 @@ func updateAgent(w http.ResponseWriter, r *http.Request, agentId string) {
} }
func getAgentsFromDB() ([]Agent, error) { func getAgentsFromDB() ([]Agent, error) {
query := "SELECT agentId, agentName, initialContact, lastContact FROM agents" query := "SELECT agentId, agentName, IPv4Address, initialContact, lastContact FROM agents"
rows, err := db.Query(query) rows, err := db.Query(query)
if err != nil { if err != nil {
return nil, err return nil, err
@ -288,7 +292,7 @@ func getAgentsFromDB() ([]Agent, error) {
var agents []Agent var agents []Agent
for rows.Next() { for rows.Next() {
var agent Agent var agent Agent
err := rows.Scan(&agent.AgentID, &agent.AgentName, &agent.InitialContact, &agent.LastContact) err := rows.Scan(&agent.AgentID, &agent.AgentName, &agent.IPv4Address, &agent.InitialContact, &agent.LastContact)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -40,10 +40,18 @@
<button class="btn btn-primary mt-3" data-bs-toggle="collapse" data-bs-target="#addAgentForm">Add Agent</button> <button class="btn btn-primary mt-3" data-bs-toggle="collapse" data-bs-target="#addAgentForm">Add Agent</button>
<div id="addAgentForm" class="collapse mt-2"> <div id="addAgentForm" class="collapse mt-2">
<form hx-post="/agents" hx-target="#agentList" hx-swap="innerHTML"> <form hx-post="/agents" hx-target="#agentList" hx-swap="innerHTML">
<div class="mb-3">
<label for="agentId" class="form-label">Agent Id</label>
<input type="text" class="form-control" id="agentId" name="agentId" required>
</div>
<div class="mb-3"> <div class="mb-3">
<label for="agentName" class="form-label">Agent Name</label> <label for="agentName" class="form-label">Agent Name</label>
<input type="text" class="form-control" id="agentName" name="agentName" required> <input type="text" class="form-control" id="agentName" name="agentName" required>
</div> </div>
<div class="mb-3">
<label for="IPv4Address" class="form-label">IPv4 Address</label>
<input type="text" class="form-control" id="IPv4Address" name="IPv4Address" required>
</div>
<div class="mb-3"> <div class="mb-3">
<label for="initialContact" class="form-label">Initial Contact</label> <label for="initialContact" class="form-label">Initial Contact</label>
<input type="datetime-local" class="form-control" id="initialContact" name="initialContact" required> <input type="datetime-local" class="form-control" id="initialContact" name="initialContact" required>

View File

@ -3,6 +3,7 @@
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Name</th> <th>Name</th>
<th>IPv4 Address</th>
<th>Initial Contact</th> <th>Initial Contact</th>
<th>Last Contact</th> <th>Last Contact</th>
<th>Actions</th> <th>Actions</th>
@ -13,6 +14,7 @@
<tr> <tr>
<td>{{.AgentID}}</td> <td>{{.AgentID}}</td>
<td>{{.AgentName}}</td> <td>{{.AgentName}}</td>
<td>{{.IPv4Address}}</td>
<td>{{.InitialContact}}</td> <td>{{.InitialContact}}</td>
<td>{{.LastContact}}</td> <td>{{.LastContact}}</td>
<td> <td>