cleanup
This commit is contained in:
parent
b6056297f4
commit
4e714159a6
182
main.go
182
main.go
|
@ -31,20 +31,13 @@ var db *sql.DB
|
|||
|
||||
const (
|
||||
keyServerAddr = "serverAddr"
|
||||
dbHost = "172.17.0.2"
|
||||
dbUser = "root"
|
||||
dbHost = "127.0.0.1"
|
||||
dbUser = "mysql"
|
||||
dbPort = 3306
|
||||
dbPassword = "root"
|
||||
dbPassword = "mysql"
|
||||
dbName = "gomatic"
|
||||
)
|
||||
|
||||
// type Agent struct {
|
||||
// agentId int
|
||||
// agentName string
|
||||
// initialContact string
|
||||
// lastContact string
|
||||
// }
|
||||
|
||||
type Agent struct {
|
||||
AgentID int `json:"agentId"`
|
||||
AgentName string `json:"agentName"`
|
||||
|
@ -58,20 +51,6 @@ func init() {
|
|||
tmpl, _ = template.ParseGlob("templates/*.html")
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
t, err := template.ParseFiles(tmpl)
|
||||
if err != nil {
|
||||
log.Printf("Failed to load template %s: %v", tmpl, err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := t.Execute(w, data); err != nil {
|
||||
log.Printf("Failed to render template %s: %v", tmpl, err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func initDB () {
|
||||
var err error
|
||||
dbOpen := fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbUser,dbPassword, dbHost, dbPort, dbName)
|
||||
|
@ -86,113 +65,27 @@ func initDB () {
|
|||
log.Println("Connected to database")
|
||||
}
|
||||
|
||||
// func fetchAgents(w http.ResponseWriter, r *http.Request) {
|
||||
// agents, _ := getAgents(db)
|
||||
|
||||
// log.Printf("%s",agents)
|
||||
// tmpl.ExecuteTemplate(w, "agentList", agents)
|
||||
// }
|
||||
|
||||
// func getAgents(dbPointer *sql.DB) ([]Agent, error) {
|
||||
// query := "Select agentId, agentName, initialContact, lastContact from agents"
|
||||
// rows, err := dbPointer.Query(query)
|
||||
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// defer rows.Close()
|
||||
|
||||
// var agents []Agent
|
||||
// for rows.Next() {
|
||||
// var agent Agent
|
||||
|
||||
// rowErr := rows.Scan(&agent.AgentID, &agent.AgentName, &agent.InitialContact, &agent.LastContact)
|
||||
// if rowErr != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// agents = append(agents, agent)
|
||||
// }
|
||||
|
||||
// if err = rows.Err(); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return agents, nil
|
||||
// // return agents, rows.Err()
|
||||
// }
|
||||
|
||||
// func getRoot(w http.ResponseWriter, r *http.Request) {
|
||||
// ctx := r.Context()
|
||||
|
||||
// hasFirst := r.URL.Query().Has("first")
|
||||
// first := r.URL.Query().Get("first")
|
||||
// hasSecond := r.URL.Query().Has("second")
|
||||
// second := r.URL.Query().Get("second")
|
||||
|
||||
// body, err := ioutil.ReadAll(r.Body)
|
||||
|
||||
// if err != nil {
|
||||
// fmt.Printf("Could not read body: %s\n", err)
|
||||
// }
|
||||
|
||||
// fmt.Printf("%s: got / request. first(%t)=%s, second(%t)=%s body:\n%s\n",
|
||||
// ctx.Value(keyServerAddr),
|
||||
// hasFirst, first,
|
||||
// hasSecond, second,
|
||||
// body,
|
||||
// )
|
||||
|
||||
// // fmt.Printf("%s: got / request\n", ctx.Value(keyServerAddr))
|
||||
// io.WriteString(w, "This is my website!\n")
|
||||
// }
|
||||
|
||||
// func getHello(w http.ResponseWriter, r *http.Request) {
|
||||
// ctx := r.Context()
|
||||
|
||||
// fmt.Printf("%s: go /hello request\n", ctx.Value(keyServerAddr))
|
||||
|
||||
// myName := r.PostFormValue("myName")
|
||||
// if myName == "" {
|
||||
// w.Header().Set("x-missing-field", "myName")
|
||||
// w.WriteHeader(http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
// io.WriteString(w, fmt.Sprintf("Hello, %s!\n", myName))
|
||||
// }
|
||||
|
||||
func getHomepage(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl.ExecuteTemplate(w, "index.html", nil)
|
||||
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
t, err := template.ParseFiles(tmpl)
|
||||
if err != nil {
|
||||
log.Printf("Failed to load template %s: %v", tmpl, err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := t.Execute(w, data); err != nil {
|
||||
log.Printf("Failed to render template %s: %v", tmpl, err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func agentsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// if r.URL.Path == "/agents" {
|
||||
// if r.Method == http.MethodGet{
|
||||
// agents, err := getAgentsFromDB()
|
||||
// if err != nil {
|
||||
// http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// renderTemplate(w, "templates/partials/agent_list.html", agents)
|
||||
// } else {
|
||||
// http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/agents/"), "/")
|
||||
agentId := ""
|
||||
if len(parts) > 0 && parts[0] != "" {
|
||||
agentId = parts[0]
|
||||
}
|
||||
|
||||
// if len (parts) < 1 || parts[0] == "" {
|
||||
// http.Error(w, "Agent ID required", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
// agentId := parts[0]
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodDelete:
|
||||
deleteAgent(w, r, agentId)
|
||||
|
@ -211,8 +104,12 @@ func agentsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func getHomepage(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl.ExecuteTemplate(w, "index.html", nil)
|
||||
}
|
||||
|
||||
func listAgents(w http.ResponseWriter, r *http.Request) {
|
||||
agents, err := getAgentsFromDB()
|
||||
agents, err := getAgents()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -229,7 +126,7 @@ func deleteAgent(w http.ResponseWriter, r *http.Request, agentID string) {
|
|||
return
|
||||
}
|
||||
|
||||
agents, err := getAgentsFromDB()
|
||||
agents, err := getAgents()
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -282,7 +179,7 @@ func updateAgent(w http.ResponseWriter, r *http.Request, agentId string) {
|
|||
listAgents(w, r,)
|
||||
}
|
||||
|
||||
func getAgentsFromDB() ([]Agent, error) {
|
||||
func getAgents() ([]Agent, error) {
|
||||
query := "SELECT agentId, agentName, IPv4Address, initialContact, lastContact FROM agents"
|
||||
rows, err := db.Query(query)
|
||||
if err != nil {
|
||||
|
@ -302,29 +199,6 @@ func getAgentsFromDB() ([]Agent, error) {
|
|||
return agents, rows.Err()
|
||||
}
|
||||
|
||||
// func agentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/agents/"), "/")
|
||||
// if len (parts) < 1 || parts[0] == "" {
|
||||
// http.Error(w, "Agent ID required", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
// agentId := parts[0]
|
||||
|
||||
// switch r.Method {
|
||||
// case http.MethodGet:
|
||||
// getAgent(w, r, agentId)
|
||||
// // case http.MethodPost:
|
||||
// // createAgent(w, r, agentId)
|
||||
// // case http.MethodPut:
|
||||
// // updateAgent(w, r, agentId)
|
||||
// // case http.MethodDelete:
|
||||
// // deleteAgent(w, r, agentId)
|
||||
// default:
|
||||
// http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
// }
|
||||
// }
|
||||
|
||||
func getAgent(w http.ResponseWriter, r *http.Request, agentId string) {
|
||||
query := "Select agentId, agentName, initialContact, lastContact from agents where agentId = ?"
|
||||
var agent Agent
|
||||
|
@ -337,8 +211,6 @@ func getAgent(w http.ResponseWriter, r *http.Request, agentId string) {
|
|||
return
|
||||
}
|
||||
|
||||
// return agent, nil
|
||||
// TODO: Add agent_detail.html
|
||||
renderTemplate(w, "templates/partials/agent_detail.html", agent)
|
||||
|
||||
}
|
||||
|
@ -392,11 +264,6 @@ func (wsh webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// func executeCommand(w http.ResponseWrite, r *http.Request) {
|
||||
|
||||
// }
|
||||
|
||||
var agentSockets = make(map[string]*websocket.Conn)
|
||||
var agentSocketsMutex sync.Mutex
|
||||
|
||||
|
@ -440,7 +307,6 @@ func main() {
|
|||
}
|
||||
|
||||
webSocketMux := http.NewServeMux()
|
||||
// webSocketMux.Handle("/data", webSocketHandler)
|
||||
webSocketMux.Handle("/data", webSocketHandler)
|
||||
webSocketMux.Handle("/executeCommand", executeCommand)
|
||||
websocketServer := &http.Server{
|
||||
|
@ -455,11 +321,7 @@ func main() {
|
|||
// webMux.HandleFunc("/agents", fetchAgents)
|
||||
webMux.HandleFunc("/agents", agentsHandler)
|
||||
webMux.HandleFunc("/agentNames", getAgentNames)
|
||||
// webMux.HandleFunc("/newagentform", getAgentForm)
|
||||
// webMux.HandleFunc("/getagentupdateform/{agentId}", getAgentUpdateForm)
|
||||
webMux.HandleFunc("/agents/{agentId}", agentsHandler)
|
||||
// webMux.HandleFunc
|
||||
|
||||
|
||||
initDB()
|
||||
defer db.Close()
|
||||
|
|
|
@ -66,7 +66,7 @@
|
|||
<!-- <button type="submit" class="btn btn-primary">Execute</button> -->
|
||||
<!-- </form> -->
|
||||
<!-- <pre id="commandOutput"></pre> -->
|
||||
</div>
|
||||
<!-- </div> -->
|
||||
|
||||
<!-- Add Agent Form -->
|
||||
<button class="btn btn-primary mt-3" data-bs-toggle="collapse" data-bs-target="#addAgentForm">Add Agent</button>
|
||||
|
|
Loading…
Reference in New Issue