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 (
|
const (
|
||||||
keyServerAddr = "serverAddr"
|
keyServerAddr = "serverAddr"
|
||||||
dbHost = "172.17.0.2"
|
dbHost = "127.0.0.1"
|
||||||
dbUser = "root"
|
dbUser = "mysql"
|
||||||
dbPort = 3306
|
dbPort = 3306
|
||||||
dbPassword = "root"
|
dbPassword = "mysql"
|
||||||
dbName = "gomatic"
|
dbName = "gomatic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// type Agent struct {
|
|
||||||
// agentId int
|
|
||||||
// agentName string
|
|
||||||
// initialContact string
|
|
||||||
// lastContact string
|
|
||||||
// }
|
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
AgentID int `json:"agentId"`
|
AgentID int `json:"agentId"`
|
||||||
AgentName string `json:"agentName"`
|
AgentName string `json:"agentName"`
|
||||||
|
@ -58,20 +51,6 @@ func init() {
|
||||||
tmpl, _ = template.ParseGlob("templates/*.html")
|
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 () {
|
func initDB () {
|
||||||
var err error
|
var err error
|
||||||
dbOpen := fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbUser,dbPassword, dbHost, dbPort, dbName)
|
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")
|
log.Println("Connected to database")
|
||||||
}
|
}
|
||||||
|
|
||||||
// func fetchAgents(w http.ResponseWriter, r *http.Request) {
|
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||||
// agents, _ := getAgents(db)
|
t, err := template.ParseFiles(tmpl)
|
||||||
|
if err != nil {
|
||||||
// log.Printf("%s",agents)
|
log.Printf("Failed to load template %s: %v", tmpl, err)
|
||||||
// tmpl.ExecuteTemplate(w, "agentList", agents)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
// }
|
return
|
||||||
|
}
|
||||||
// func getAgents(dbPointer *sql.DB) ([]Agent, error) {
|
if err := t.Execute(w, data); err != nil {
|
||||||
// query := "Select agentId, agentName, initialContact, lastContact from agents"
|
log.Printf("Failed to render template %s: %v", tmpl, err)
|
||||||
// rows, err := dbPointer.Query(query)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
// 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 agentsHandler(w http.ResponseWriter, r *http.Request) {
|
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/"), "/")
|
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/agents/"), "/")
|
||||||
agentId := ""
|
agentId := ""
|
||||||
if len(parts) > 0 && parts[0] != "" {
|
if len(parts) > 0 && parts[0] != "" {
|
||||||
agentId = 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 {
|
switch r.Method {
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
deleteAgent(w, r, agentId)
|
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) {
|
func listAgents(w http.ResponseWriter, r *http.Request) {
|
||||||
agents, err := getAgentsFromDB()
|
agents, err := getAgents()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -229,7 +126,7 @@ func deleteAgent(w http.ResponseWriter, r *http.Request, agentID string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
agents, err := getAgentsFromDB()
|
agents, err := getAgents()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -282,7 +179,7 @@ func updateAgent(w http.ResponseWriter, r *http.Request, agentId string) {
|
||||||
listAgents(w, r,)
|
listAgents(w, r,)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAgentsFromDB() ([]Agent, error) {
|
func getAgents() ([]Agent, error) {
|
||||||
query := "SELECT agentId, agentName, IPv4Address, 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 {
|
||||||
|
@ -302,29 +199,6 @@ func getAgentsFromDB() ([]Agent, error) {
|
||||||
return agents, rows.Err()
|
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) {
|
func getAgent(w http.ResponseWriter, r *http.Request, agentId string) {
|
||||||
query := "Select agentId, agentName, initialContact, lastContact from agents where agentId = ?"
|
query := "Select agentId, agentName, initialContact, lastContact from agents where agentId = ?"
|
||||||
var agent Agent
|
var agent Agent
|
||||||
|
@ -337,8 +211,6 @@ func getAgent(w http.ResponseWriter, r *http.Request, agentId string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// return agent, nil
|
|
||||||
// TODO: Add agent_detail.html
|
|
||||||
renderTemplate(w, "templates/partials/agent_detail.html", agent)
|
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 agentSockets = make(map[string]*websocket.Conn)
|
||||||
var agentSocketsMutex sync.Mutex
|
var agentSocketsMutex sync.Mutex
|
||||||
|
|
||||||
|
@ -440,7 +307,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
webSocketMux := http.NewServeMux()
|
webSocketMux := http.NewServeMux()
|
||||||
// webSocketMux.Handle("/data", webSocketHandler)
|
|
||||||
webSocketMux.Handle("/data", webSocketHandler)
|
webSocketMux.Handle("/data", webSocketHandler)
|
||||||
webSocketMux.Handle("/executeCommand", executeCommand)
|
webSocketMux.Handle("/executeCommand", executeCommand)
|
||||||
websocketServer := &http.Server{
|
websocketServer := &http.Server{
|
||||||
|
@ -455,11 +321,7 @@ func main() {
|
||||||
// webMux.HandleFunc("/agents", fetchAgents)
|
// webMux.HandleFunc("/agents", fetchAgents)
|
||||||
webMux.HandleFunc("/agents", agentsHandler)
|
webMux.HandleFunc("/agents", agentsHandler)
|
||||||
webMux.HandleFunc("/agentNames", getAgentNames)
|
webMux.HandleFunc("/agentNames", getAgentNames)
|
||||||
// webMux.HandleFunc("/newagentform", getAgentForm)
|
|
||||||
// webMux.HandleFunc("/getagentupdateform/{agentId}", getAgentUpdateForm)
|
|
||||||
webMux.HandleFunc("/agents/{agentId}", agentsHandler)
|
webMux.HandleFunc("/agents/{agentId}", agentsHandler)
|
||||||
// webMux.HandleFunc
|
|
||||||
|
|
||||||
|
|
||||||
initDB()
|
initDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
|
@ -66,7 +66,7 @@
|
||||||
<!-- <button type="submit" class="btn btn-primary">Execute</button> -->
|
<!-- <button type="submit" class="btn btn-primary">Execute</button> -->
|
||||||
<!-- </form> -->
|
<!-- </form> -->
|
||||||
<!-- <pre id="commandOutput"></pre> -->
|
<!-- <pre id="commandOutput"></pre> -->
|
||||||
</div>
|
<!-- </div> -->
|
||||||
|
|
||||||
<!-- Add Agent Form -->
|
<!-- Add Agent Form -->
|
||||||
<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>
|
||||||
|
|
Loading…
Reference in New Issue