2025-01-06 11:07:53 +01:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2025-05-06 17:46:43 +02:00
|
|
|
|
"encoding/json"
|
2025-05-23 12:37:53 +02:00
|
|
|
|
"embed"
|
2025-01-06 11:07:53 +01:00
|
|
|
|
"os"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
2025-05-12 17:47:32 +02:00
|
|
|
|
"slices"
|
2025-01-06 11:07:53 +01:00
|
|
|
|
"database/sql"
|
2025-05-06 17:46:43 +02:00
|
|
|
|
"fmt"
|
2025-01-06 11:07:53 +01:00
|
|
|
|
"html/template"
|
|
|
|
|
|
"log"
|
2025-06-27 14:08:12 +02:00
|
|
|
|
"net"
|
2025-01-06 11:07:53 +01:00
|
|
|
|
"net/http"
|
2025-05-16 15:35:31 +02:00
|
|
|
|
"strconv"
|
2025-01-06 11:07:53 +01:00
|
|
|
|
"sync"
|
|
|
|
|
|
"syscall"
|
|
|
|
|
|
|
2025-06-02 11:27:51 +02:00
|
|
|
|
"net/http/httputil"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
// "golang.org/x/net/html"
|
2025-04-29 17:03:49 +02:00
|
|
|
|
"gontrol/src/logger"
|
2025-05-06 17:46:43 +02:00
|
|
|
|
"gontrol/src/randomname"
|
2025-01-15 16:38:23 +01:00
|
|
|
|
api "gontrol/src/server/api"
|
2025-05-06 17:46:43 +02:00
|
|
|
|
"gontrol/src/server/database"
|
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
|
|
|
|
)
|
|
|
|
|
|
|
2025-04-29 17:03:49 +02:00
|
|
|
|
var (
|
|
|
|
|
|
tmpl *template.Template
|
|
|
|
|
|
db *sql.DB
|
|
|
|
|
|
)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
2025-05-23 12:37:53 +02:00
|
|
|
|
//go:embed static/*
|
|
|
|
|
|
var staticFiles embed.FS
|
|
|
|
|
|
|
2025-06-27 14:08:12 +02:00
|
|
|
|
// //go:embed templates
|
|
|
|
|
|
// var templateFiles embed.FS
|
2025-05-06 17:46:43 +02:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
tmpl, _ = template.ParseGlob("templates/*.html")
|
|
|
|
|
|
|
2025-06-27 14:08:12 +02:00
|
|
|
|
// var err error
|
|
|
|
|
|
// tmpl, err = template.ParseFS(
|
|
|
|
|
|
// templateFiles,
|
|
|
|
|
|
// "templates/*.html",
|
|
|
|
|
|
// "templates/partials/*.html",
|
|
|
|
|
|
// )
|
|
|
|
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
// log.Fatalf("Failed to parse embedded templates: %v", err)
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2025-05-06 17:46:43 +02:00
|
|
|
|
// Sqlite3
|
|
|
|
|
|
err := logger.InitDB("/tmp/gontrol_logs.db")
|
2025-06-27 14:08:12 +02:00
|
|
|
|
|
2025-05-06 17:46:43 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
|
}
|
2025-05-26 16:19:49 +02:00
|
|
|
|
|
|
|
|
|
|
// Agents database
|
|
|
|
|
|
db = database.InitSQLiteDB("/tmp/gontrol_agents.db")
|
2025-05-06 17:46:43 +02:00
|
|
|
|
}
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
2025-06-27 14:08:12 +02:00
|
|
|
|
// func renderTemplate(w http.ResponseWriter, tmplPath string, data interface{}) {
|
|
|
|
|
|
// t := tmpl.Lookup(strings.TrimPrefix(tmplPath, "templates/"))
|
|
|
|
|
|
// if t == nil {
|
|
|
|
|
|
// log.Printf("Template %s not found", tmplPath)
|
|
|
|
|
|
// 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", tmplPath, err)
|
|
|
|
|
|
// http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
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
|
|
|
|
api.DeleteAgent(db, w, r, agentId)
|
|
|
|
|
|
listAgents(w,r)
|
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
|
|
|
|
agent, _ := api.GetAgent(db, w, r, agentId)
|
|
|
|
|
|
renderTemplate(w, "templates/partials/agent_detail.html", agent)
|
2025-06-27 14:08:12 +02:00
|
|
|
|
// renderTemplate(w, "agent_detail.html", agent)
|
2025-01-08 17:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
case http.MethodPost:
|
2025-01-15 16:38:23 +01:00
|
|
|
|
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
|
|
|
|
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-05-06 17:46:43 +02:00
|
|
|
|
var agents []api.Agent
|
2025-01-15 16:38:23 +01:00
|
|
|
|
agents, err := api.GetAgents(db)
|
2025-05-06 17:46:43 +02:00
|
|
|
|
currentAgents := getAgentsStatus()
|
|
|
|
|
|
|
2025-05-12 17:47:32 +02:00
|
|
|
|
for i := range agents {
|
|
|
|
|
|
if slices.Contains(currentAgents, agents[i].AgentName) {
|
|
|
|
|
|
agents[i].Status = "Connected"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
agents[i].Status = "Disconnected"
|
2025-05-06 17:46:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-23 12:37:53 +02:00
|
|
|
|
if strings.Contains(r.Header.Get("Accept"), "json") {
|
2025-05-16 15:35:31 +02:00
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
jsonData, err := json.Marshal(agents)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
http.Error(w, "Failed to encode agents to JSON", http.StatusInternalServerError)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
w.Write(jsonData)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-06-27 14:08:12 +02:00
|
|
|
|
// renderTemplate(w, "agent_list.html", agents)
|
2025-01-08 17:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-06 17:46:43 +02:00
|
|
|
|
func getAgentsStatus() []string {
|
|
|
|
|
|
resp, err := http.Get("http://localhost:5555/agentNames")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println("Error fetching agent names:", err)
|
|
|
|
|
|
logger.InsertLog(logger.Error, "Error fetching agent names from websocketServer")
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
var agentNames []string
|
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&agentNames); err != nil {
|
|
|
|
|
|
log.Println("Error decoding response:", err)
|
|
|
|
|
|
return []string{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return agentNames
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-24 14:35:45 +01:00
|
|
|
|
func getAgentIds(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
api.GetAgentIds(db, w, r)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 17:03:49 +02:00
|
|
|
|
func logsHandler(w http.ResponseWriter, r *http.Request) {
|
2025-05-23 12:37:53 +02:00
|
|
|
|
// Warning this bit me in the nose: var countStr is []string, but
|
|
|
|
|
|
// variable = countStr[0] is casted to int automatically
|
2025-05-16 15:35:31 +02:00
|
|
|
|
// when the string is a number. Jesus Christ, this is odd behavior!
|
|
|
|
|
|
levels := r.URL.Query()["level"]
|
|
|
|
|
|
countStr := r.URL.Query()["limit"]
|
2025-05-23 12:37:53 +02:00
|
|
|
|
var limit int = 128
|
2025-05-16 15:35:31 +02:00
|
|
|
|
if len(countStr) > 0 {
|
|
|
|
|
|
parsedCount, err := strconv.Atoi(countStr[0])
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
limit = parsedCount
|
|
|
|
|
|
} else {
|
|
|
|
|
|
http.Error(w, "Invalid count value", http.StatusBadRequest)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This enables not only `level` GET parameters but also selecting by paths
|
|
|
|
|
|
// For example /logs/error is now identical to /logs?level=error
|
|
|
|
|
|
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/logs/"), "/")
|
|
|
|
|
|
if (len(levels) == 0) && len(parts) > 0 && parts[0] != "" {
|
|
|
|
|
|
levels = []string{parts[0]}
|
|
|
|
|
|
}
|
2025-05-06 17:46:43 +02:00
|
|
|
|
|
2025-05-16 15:35:31 +02:00
|
|
|
|
// Call the police... I mean logger
|
|
|
|
|
|
logs, err := logger.FetchLogs(limit, levels)
|
2025-05-06 17:46:43 +02:00
|
|
|
|
|
2025-04-29 17:03:49 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
http.Error(w, "Error fetching logs", http.StatusInternalServerError)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-06 17:46:43 +02:00
|
|
|
|
renderTemplate(w, "templates/partials/logs_partial.html", logs)
|
2025-06-27 14:08:12 +02:00
|
|
|
|
// renderTemplate(w, "logs_partial.html", logs)
|
2025-04-29 17:03:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-27 14:08:12 +02:00
|
|
|
|
// proxyAgentHandler tunnels HTTP and WebSocket traffic to an agent
|
|
|
|
|
|
// selected by ?ip=…&port=… . It keeps the path *after* /proxyAgent,
|
|
|
|
|
|
// removes the two query parameters, disables HTTP/2 (mandatory for WS),
|
|
|
|
|
|
// and streams with no buffering.
|
2025-05-23 12:37:53 +02:00
|
|
|
|
func proxyAgentHandler(w http.ResponseWriter, r *http.Request) {
|
2025-06-27 14:08:12 +02:00
|
|
|
|
ip := r.URL.Query().Get("ip")
|
|
|
|
|
|
port := r.URL.Query().Get("port")
|
|
|
|
|
|
if ip == "" || port == "" {
|
|
|
|
|
|
http.Error(w, "ip and port query parameters are required", http.StatusBadRequest)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We leave the scheme "http" even for WebSockets – the Upgrade
|
|
|
|
|
|
// header does the rest. (Only cosmetic to change it to "ws”.)
|
|
|
|
|
|
target, err := url.Parse("http://" + ip + ":" + port)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
http.Error(w, "invalid ip/port: "+err.Error(), http.StatusBadRequest)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
proxy := &httputil.ReverseProxy{
|
|
|
|
|
|
Director: func(req *http.Request) {
|
|
|
|
|
|
// Point to the agent
|
|
|
|
|
|
req.URL.Scheme = target.Scheme
|
|
|
|
|
|
req.URL.Host = target.Host
|
|
|
|
|
|
|
|
|
|
|
|
// Trim the first "/proxyAgent" prefix
|
|
|
|
|
|
if strings.HasPrefix(req.URL.Path, "/proxyAgent") {
|
|
|
|
|
|
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/proxyAgent")
|
|
|
|
|
|
if req.URL.Path == "" {
|
|
|
|
|
|
req.URL.Path = "/"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Scrub ip/port from downstream query
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
|
|
q.Del("ip")
|
|
|
|
|
|
q.Del("port")
|
|
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
|
|
|
|
// Preserve Host (many CLI-style servers care)
|
|
|
|
|
|
req.Host = target.Host
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Critical tweaks for WebSockets
|
|
|
|
|
|
Transport: &http.Transport{
|
|
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
|
|
DialContext: (&net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
|
|
|
|
|
|
ForceAttemptHTTP2: false, // MUST be HTTP/1.1 for WS
|
|
|
|
|
|
ResponseHeaderTimeout: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
FlushInterval: -1, // stream bytes immediately
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
proxy.ServeHTTP(w, r)
|
2025-05-23 12:37:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-06 11:07:53 +01:00
|
|
|
|
func main() {
|
|
|
|
|
|
|
2025-05-06 17:46:43 +02:00
|
|
|
|
// sqlite3 has been initialized in init()
|
|
|
|
|
|
defer logger.CloseDB()
|
|
|
|
|
|
|
2025-05-26 16:19:49 +02:00
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
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
|
|
|
|
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("/agents", agentsHandler)
|
2025-01-11 02:36:21 +01:00
|
|
|
|
webMux.HandleFunc("/agentNames", getAgentNames)
|
2025-01-24 14:35:45 +01:00
|
|
|
|
webMux.HandleFunc("/agentIds", getAgentIds)
|
2025-01-08 17:59:04 +01:00
|
|
|
|
webMux.HandleFunc("/agents/{agentId}", agentsHandler)
|
2025-04-29 17:03:49 +02:00
|
|
|
|
webMux.HandleFunc("/logs", logsHandler)
|
2025-05-16 15:35:31 +02:00
|
|
|
|
webMux.HandleFunc("/logs/{level}", logsHandler)
|
2025-05-23 12:37:53 +02:00
|
|
|
|
webMux.Handle("/static/", http.FileServer(http.FS(staticFiles)))
|
|
|
|
|
|
webMux.HandleFunc("/proxyAgent", proxyAgentHandler)
|
2025-06-27 14:08:12 +02:00
|
|
|
|
webMux.HandleFunc("/proxyAgent/", proxyAgentHandler)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
2025-05-26 16:19:49 +02:00
|
|
|
|
// db := database.InitDB (cfg.Database.Username, cfg.Database.Password, cfg.Database.Host, cfg.Database.Port, cfg.Database.Name)
|
|
|
|
|
|
// defer db.Close()
|
2025-05-06 17:46:43 +02:00
|
|
|
|
|
2025-01-15 16:38:23 +01:00
|
|
|
|
name := randomname.GenerateRandomName()
|
2025-07-02 13:43:45 +02:00
|
|
|
|
log.Println("Server instance: ", name)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
|
|
|
|
|
webServer := &http.Server {
|
|
|
|
|
|
Addr: ":3333",
|
|
|
|
|
|
Handler: webMux,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
defer wg.Done()
|
2025-04-29 17:03:49 +02:00
|
|
|
|
logLine := "Websocket server is running on port 5555"
|
|
|
|
|
|
log.Println(logLine)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
if err := websocketServer.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
|
|
log.Fatalf("Websocket server failed: %s", err)
|
|
|
|
|
|
}
|
2025-05-06 17:46:43 +02:00
|
|
|
|
err := logger.InsertLog(logger.Info, logLine)
|
2025-04-29 17:03:49 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println("Error inserting log:", err)
|
|
|
|
|
|
}
|
2025-01-06 11:07:53 +01:00
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
defer wg.Done()
|
2025-04-29 17:03:49 +02:00
|
|
|
|
logLine := "Web server is running on port 3333"
|
|
|
|
|
|
log.Println(logLine)
|
2025-01-06 11:07:53 +01:00
|
|
|
|
|
|
|
|
|
|
if err := webServer.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
|
|
log.Fatalf("Web server failed: %s", err)
|
|
|
|
|
|
}
|
2025-04-29 17:03:49 +02:00
|
|
|
|
|
2025-05-06 17:46:43 +02:00
|
|
|
|
err := logger.InsertLog(logger.Info, logLine)
|
2025-04-29 17:03:49 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Println("Error inserting log:", err)
|
|
|
|
|
|
}
|
2025-01-06 11:07:53 +01:00
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
}
|