added IP address to the agents
This commit is contained in:
		
							parent
							
								
									2f2485fc25
								
							
						
					
					
						commit
						35ec77e5c6
					
				|  | @ -17,7 +17,7 @@ const( | |||
| 	webServerAddr 	= "127.0.0.1:3333" | ||||
| 	webSocketAddr   = "127.0.0.1:5555" | ||||
| 	registerURL = "http://" + webServerAddr + "/agents" | ||||
| 	wsURL 		= "ws://" + webSocketAddr + "/ws" | ||||
| 	wsURL 		= "ws://" + webSocketAddr + "/data" | ||||
| ) | ||||
| 
 | ||||
| type Agent struct { | ||||
|  |  | |||
|  | @ -3,7 +3,7 @@ | |||
| drop table if exists agents; | ||||
| create table agents ( | ||||
|     id UUID default uuid() Primary Key, | ||||
|     agentId int, | ||||
|     agentId int unique, | ||||
|     agentName varchar(255), | ||||
|     IPv4Address varchar(15), | ||||
|     initialContact timestamp, | ||||
|  | @ -11,4 +11,4 @@ create table agents ( | |||
|     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
								
								
								
								
							
							
						
						
									
										16
									
								
								main.go
								
								
								
								
							|  | @ -49,6 +49,8 @@ type Agent struct { | |||
| 	AgentName      string `json:"agentName"` | ||||
| 	InitialContact string `json:"initialContact"` | ||||
| 	LastContact    string `json:"lastContact"` | ||||
| 	IPv4Address    string `json:"IPv4Address"` | ||||
| 	Status         string `json:"status"` | ||||
| } | ||||
| 
 | ||||
| func init() { | ||||
|  | @ -243,11 +245,12 @@ func createAgent(w http.ResponseWriter, r * http.Request) { | |||
| 
 | ||||
| 	agentName := r.FormValue("agentName") | ||||
| 	agentId := r.FormValue("agentId") | ||||
| 	IPv4Address := r.FormValue("IPv4Address") | ||||
| 	// initalContact := r.FormValue("initialContact")
 | ||||
| 	// lastContact := r.FormValue("lastContact")
 | ||||
| 
 | ||||
| 	query := "INSERT INTO agents (agentId, agentName, initialContact, lastContact) VALUES (?, ?, NOW(), NOW())" | ||||
| 	_, err = db.Exec(query, agentId, agentName) | ||||
| 	query := "INSERT INTO agents (agentId, agentName, IPv4Address, initialContact, lastContact) VALUES (?, ?, ?, NOW(), NOW())" | ||||
| 	_, err = db.Exec(query, agentId, agentName, IPv4Address) | ||||
| 	if err != nil { | ||||
| 		http.Error(w, "Failed to create agent", http.StatusInternalServerError) | ||||
| 		return | ||||
|  | @ -266,9 +269,10 @@ func updateAgent(w http.ResponseWriter, r *http.Request, agentId string) { | |||
| 
 | ||||
| 	agentName := r.FormValue("agentName") | ||||
| 	lastContact := r.FormValue("lastContact") | ||||
| 	IPv4Address := r.FormValue("IPv4Address") | ||||
| 
 | ||||
| 	query := "UPDATE agents SET agentName = ?, lastContact = ? where agentId = ?" | ||||
| 	_, err = db.Exec(query, agentName, lastContact, agentId) | ||||
| 	query := "UPDATE agents SET agentName = ?, IPv4Address= ?, lastContact = ? where agentId = ?" | ||||
| 	_, err = db.Exec(query, agentName, IPv4Address, lastContact, agentId) | ||||
| 	if err != nil { | ||||
| 		http.Error(w, "Failed to update agent", http.StatusInternalServerError) | ||||
| 		return | ||||
|  | @ -278,7 +282,7 @@ func updateAgent(w http.ResponseWriter, r *http.Request, agentId string) { | |||
| } | ||||
| 
 | ||||
| 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) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
|  | @ -288,7 +292,7 @@ func getAgentsFromDB() ([]Agent, error) { | |||
| 	var agents []Agent | ||||
| 	for rows.Next() { | ||||
| 		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 { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  |  | |||
|  | @ -40,10 +40,18 @@ | |||
|                 <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"> | ||||
|                     <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"> | ||||
|                             <label for="agentName" class="form-label">Agent Name</label> | ||||
|                             <input type="text" class="form-control" id="agentName" name="agentName" required> | ||||
|                         </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"> | ||||
|                             <label for="initialContact" class="form-label">Initial Contact</label> | ||||
|                             <input type="datetime-local" class="form-control" id="initialContact" name="initialContact" required> | ||||
|  |  | |||
|  | @ -3,6 +3,7 @@ | |||
|         <tr> | ||||
|             <th>ID</th> | ||||
|             <th>Name</th> | ||||
|             <th>IPv4 Address</th> | ||||
|             <th>Initial Contact</th> | ||||
|             <th>Last Contact</th> | ||||
|             <th>Actions</th> | ||||
|  | @ -13,6 +14,7 @@ | |||
|         <tr> | ||||
|             <td>{{.AgentID}}</td> | ||||
|             <td>{{.AgentName}}</td> | ||||
|             <td>{{.IPv4Address}}</td> | ||||
|             <td>{{.InitialContact}}</td> | ||||
|             <td>{{.LastContact}}</td> | ||||
|             <td> | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue