2025-01-06 11:07:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-15 16:38:23 +01:00
|
|
|
// "encoding/json"
|
2025-01-06 11:07:53 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
// "errors"
|
|
|
|
"fmt"
|
2025-01-09 16:42:27 +01:00
|
|
|
// "io"
|
|
|
|
// "io/ioutil"
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
|
|
// "net"
|
|
|
|
"database/sql"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2025-01-15 16:38:23 +01:00
|
|
|
"gontrol/src/randomname"
|
|
|
|
api "gontrol/src/server/api"
|
2025-01-17 10:04:20 +01:00
|
|
|
websocketserver "gontrol/src/server/websocket"
|
2025-01-15 16:38:23 +01:00
|
|
|
|
2025-01-06 11:07:53 +01:00
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2025-01-17 10:04:20 +01:00
|
|
|
"github.com/kelseyhightower/envconfig"
|
2025-01-06 11:07:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var tmpl *template.Template
|
|
|
|
var db *sql.DB
|
|
|
|
|
2025-01-17 10:04:20 +01:00
|
|
|
type Config struct {
|
|
|
|
Database struct {
|
|
|
|
Username string `envconfig:"DB_USERNAME"`
|
|
|
|
Password string `envconfig:"DB_PASSWORD"`
|
|
|
|
Port int16 `envconfig:"DB_PORT"`
|
|
|
|
Name string `envconfig:"DB_NAME"`
|
|
|
|
Host string `envconfig:"DB_HOST"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readEnv(cfg *Config) {
|
|
|
|
err := envconfig.Process("", cfg)
|
|
|
|
if err != nil {
|
|
|
|
processError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processError(err error) {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
|
|
type Agent struct {
|
2025-01-08 17:59:04 +01:00
|
|
|
AgentID int `json:"agentId"`
|
|
|
|
AgentName string `json:"agentName"`
|
|
|
|
InitialContact string `json:"initialContact"`
|
|
|
|
LastContact string `json:"lastContact"`
|
2025-01-10 02:47:33 +01:00
|
|
|
IPv4Address string `json:"IPv4Address"`
|
|
|
|
Status string `json:"status"`
|
2025-01-06 11:07:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
tmpl, _ = template.ParseGlob("templates/*.html")
|
|
|
|
}
|
|
|
|
|
2025-01-17 10:04:20 +01:00
|
|
|
func initDB (dbUser string, dbPassword string, dbHost string, dbPort int16, dbName string ) {
|
2025-01-06 11:07:53 +01:00
|
|
|
var err error
|
|
|
|
dbOpen := fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbUser,dbPassword, dbHost, dbPort, dbName)
|
|
|
|
db, err = sql.Open("mysql", dbOpen)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Println("Connected to database")
|
|
|
|
}
|
|
|
|
|
2025-01-14 15:47:04 +01:00
|
|
|
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
|
|
|
|
}
|
2025-01-06 11:07:53 +01:00
|
|
|
}
|
|
|
|
|
2025-01-08 17:59:04 +01:00
|
|
|
func agentsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/agents/"), "/")
|
|
|
|
agentId := ""
|
|
|
|
if len(parts) > 0 && parts[0] != "" {
|
|
|
|
agentId = parts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodDelete:
|
2025-01-15 16:38:23 +01:00
|
|
|
// deleteAgent(w, r, agentId)
|
|
|
|
api.DeleteAgent(db, w, r, agentId)
|
|
|
|
listAgents(w,r)
|
|
|
|
// renderTemplate(w, "templates/partials/agent_list.html", agents)
|
2025-01-08 17:59:04 +01:00
|
|
|
case http.MethodGet:
|
|
|
|
if agentId == "" {
|
|
|
|
listAgents(w, r)
|
|
|
|
} else {
|
2025-01-15 16:38:23 +01:00
|
|
|
// getAgent(w, r, agentId)
|
|
|
|
agent, _ := api.GetAgent(db, w, r, agentId)
|
|
|
|
renderTemplate(w, "templates/partials/agent_detail.html", agent)
|
2025-01-08 17:59:04 +01:00
|
|
|
}
|
|
|
|
case http.MethodPost:
|
2025-01-15 16:38:23 +01:00
|
|
|
// createAgent(w, r)
|
|
|
|
api.CreateAgent(db, w, r)
|
|
|
|
listAgents(w, r)
|
2025-01-08 17:59:04 +01:00
|
|
|
case http.MethodPut:
|
2025-01-15 16:38:23 +01:00
|
|
|
// updateAgent(w, r, agentId)
|
|
|
|
api.UpdateAgent(db, w, r, agentId)
|
|
|
|
listAgents(w, r)
|
2025-01-08 17:59:04 +01:00
|
|
|
default:
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-14 15:47:04 +01:00
|
|
|
func getHomepage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tmpl.ExecuteTemplate(w, "index.html", nil)
|
|
|
|
}
|
|
|
|
|
2025-01-08 17:59:04 +01:00
|
|
|
func listAgents(w http.ResponseWriter, r *http.Request) {
|
2025-01-15 16:38:23 +01:00
|
|
|
// agents, err := getAgents()
|
|
|
|
agents, err := api.GetAgents(db)
|
2025-01-08 17:59:04 +01:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to fetch agents", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(w, "templates/partials/agent_list.html", agents)
|
|
|
|
}
|
|
|
|
|
2025-01-11 02:36:21 +01:00
|
|
|
func getAgentNames(w http.ResponseWriter, r *http.Request) {
|
2025-01-15 16:38:23 +01:00
|
|
|
api.GetAgentNames(db, w, r)
|
|
|
|
return
|
2025-01-11 02:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-01-06 11:07:53 +01:00
|
|
|
func main() {
|
|
|
|
|
2025-01-17 10:04:20 +01:00
|
|
|
var cfg Config
|
|
|
|
readEnv(&cfg)
|
|
|
|
|
2025-01-06 11:07:53 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2025-01-15 16:38:23 +01:00
|
|
|
// webSocketHandler := webSocketHandler {
|
|
|
|
// upgrader: websocket.Upgrader{},
|
|
|
|
// }
|
|
|
|
|
|
|
|
websocketServer := websocketserver.Server()
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
webMux := http.NewServeMux()
|
|
|
|
webMux.HandleFunc("/", getHomepage)
|
2025-01-08 17:59:04 +01:00
|
|
|
// webMux.HandleFunc("/index", getRoot)
|
|
|
|
// webMux.HandleFunc("/hello", getHello)
|
|
|
|
// webMux.HandleFunc("/agents", fetchAgents)
|
|
|
|
webMux.HandleFunc("/agents", agentsHandler)
|
2025-01-11 02:36:21 +01:00
|
|
|
webMux.HandleFunc("/agentNames", getAgentNames)
|
2025-01-08 17:59:04 +01:00
|
|
|
webMux.HandleFunc("/agents/{agentId}", agentsHandler)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
2025-01-17 10:04:20 +01:00
|
|
|
initDB (cfg.Database.Username, cfg.Database.Password, cfg.Database.Host, cfg.Database.Port, cfg.Database.Name)
|
2025-01-06 11:07:53 +01:00
|
|
|
defer db.Close()
|
2025-01-15 16:38:23 +01:00
|
|
|
name := randomname.GenerateRandomName()
|
2025-01-14 17:02:14 +01:00
|
|
|
fmt.Println(name)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
|
|
webServer := &http.Server {
|
|
|
|
Addr: ":3333",
|
|
|
|
Handler: webMux,
|
|
|
|
// BaseContext: func(l net.Listener) context.Context {
|
|
|
|
// ctx = context.WithValue(ctx, keyServerAddr, l.Addr().String())
|
|
|
|
// return ctx
|
|
|
|
// },
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
log.Println("Websocket server is running on port 5555")
|
|
|
|
if err := websocketServer.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
log.Fatalf("Websocket server failed: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
log.Println("Web server is running on port 3333")
|
|
|
|
|
|
|
|
if err := webServer.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
log.Fatalf("Web server failed: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
shutdownCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
<-shutdownCh
|
|
|
|
log.Println("Shutdown signal received")
|
|
|
|
|
|
|
|
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer shutdownCancel()
|
|
|
|
|
|
|
|
if err := websocketServer.Shutdown(shutdownCtx); err != nil {
|
|
|
|
log.Printf("error shutting down websocket server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := webServer.Shutdown(shutdownCtx); err != nil {
|
|
|
|
log.Printf("Error shutting down web server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
log.Println("All servers stopped")
|
|
|
|
}
|