some changes

This commit is contained in:
2025-11-30 12:50:38 +02:00
parent 004bb7ef7f
commit c1e5fc90ee
9 changed files with 140 additions and 27 deletions

32
api/router.go Normal file
View File

@@ -0,0 +1,32 @@
package api
import (
"net/http"
"path/filepath"
"git.oblat.lv/alex/triggerssmith/internal/config"
"github.com/go-chi/chi/v5"
)
type Router struct {
r chi.Router
cfg *config.Config
}
func NewRouter(cfg *config.Config) *Router {
r := chi.NewRouter()
return &Router{
r: r,
cfg: cfg,
}
}
func (r *Router) RouteHandler() chi.Router {
r.r.Get("/", func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, filepath.Join(r.cfg.Server.StaticFilesPath, "index.html"))
})
fs := http.FileServer(http.Dir("static"))
r.r.Handle("/static/*", http.StripPrefix("/static/", fs))
return r.r
}

View File

@@ -58,8 +58,13 @@ var reloadCmd = &cobra.Command{
if err != nil { if err != nil {
slog.Error("failed to find process", slog.Int("pid", *optsReloadCmd.PID), slog.String("err", err.Error())) slog.Error("failed to find process", slog.Int("pid", *optsReloadCmd.PID), slog.String("err", err.Error()))
} }
proc.Signal(syscall.SIGHUP) err = proc.Signal(syscall.SIGHUP)
slog.Debug("done") if err != nil {
slog.Error("failed to reload process", slog.Int("pid", *optsReloadCmd.PID), slog.String("err", err.Error()))
} else {
slog.Debug("done")
}
}, },
} }

View File

@@ -10,8 +10,10 @@ import (
"syscall" "syscall"
"time" "time"
"git.oblat.lv/alex/triggerssmith/api"
application "git.oblat.lv/alex/triggerssmith/internal/app" application "git.oblat.lv/alex/triggerssmith/internal/app"
"git.oblat.lv/alex/triggerssmith/internal/config" "git.oblat.lv/alex/triggerssmith/internal/config"
"git.oblat.lv/alex/triggerssmith/internal/server"
"git.oblat.lv/alex/triggerssmith/internal/vars" "git.oblat.lv/alex/triggerssmith/internal/vars"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -97,22 +99,24 @@ var serveCmd = &cobra.Command{
} }
app.LoadConfiguration(cfg) app.LoadConfiguration(cfg)
server := app.Server() srv := app.Server()
mux := http.NewServeMux() //mux := http.NewServeMux()
// static files // static files
staticPath := cfg.Server.StaticFilesPath // staticPath := cfg.Server.StaticFilesPath
slog.Debug("Setting up static file server", slog.String("path", staticPath)) // slog.Debug("Setting up static file server", slog.String("path", staticPath))
fs := http.FileServer(http.Dir(staticPath)) // fs := http.FileServer(http.Dir(staticPath))
mux.Handle("/static/", http.StripPrefix("/static/", fs)) // mux.Handle("/static/", http.StripPrefix("/static/", fs))
handler := loggingMiddleware(mux) // handler := loggingMiddleware(mux)
server.SetHandler(handler) router := api.NewRouter(cfg)
server.Init()
srv.SetHandler(router.RouteHandler())
srv.Init()
var addr = net.JoinHostPort(cfg.Server.Addr, fmt.Sprintf("%d", cfg.Server.Port)) var addr = net.JoinHostPort(cfg.Server.Addr, fmt.Sprintf("%d", cfg.Server.Port))
slog.Debug("Binding listener", slog.String("address", addr)) slog.Debug("Binding listener", slog.String("address", addr))
err = server.Start(addr) err = srv.Start(addr)
if err != nil { if err != nil {
slog.Error("Failed to start server", slog.String("error", err.Error())) slog.Error("Failed to start server", slog.String("error", err.Error()))
return return
@@ -133,28 +137,30 @@ var serveCmd = &cobra.Command{
slog.Info("Configuration reloaded") slog.Info("Configuration reloaded")
var addr = net.JoinHostPort(cfg.Server.Addr, fmt.Sprintf("%d", cfg.Server.Port)) var addr = net.JoinHostPort(cfg.Server.Addr, fmt.Sprintf("%d", cfg.Server.Port))
slog.Debug("New configuration", slog.Any("config", cfg)) slog.Debug("New configuration", slog.Any("config", cfg))
err = server.Reload(addr) err = srv.Reload(addr)
if err != nil { if err != nil {
slog.Error("Failed to restart server with new configuration", slog.String("error", err.Error())) slog.Error("Failed to restart server with new configuration", slog.String("error", err.Error()))
} }
} }
case syscall.SIGINT: case syscall.SIGINT:
slog.Info("Stopping server by SIGINT") slog.Info("Stopping server by SIGINT")
err := server.Stop()
if err != nil {
slog.Error("Failed to stop server", slog.String("err", err.Error()))
os.Exit(1)
}
os.Remove(vars.PID_PATH) os.Remove(vars.PID_PATH)
_ = server.StopAll()
//err := srv.Stop()
// if err != nil {
// slog.Error("Failed to stop server", slog.String("err", err.Error()))
// os.Exit(1)
// }
return return
case syscall.SIGTERM: case syscall.SIGTERM:
slog.Info("Stopping server by SIGTERM") slog.Info("Stopping server by SIGTERM")
err := server.Stop()
if err != nil {
slog.Error("Failed to stop server", slog.String("err", err.Error()))
os.Exit(1)
}
os.Remove(vars.PID_PATH) os.Remove(vars.PID_PATH)
_ = server.StopAll()
//err := srv.Stop()
// if err != nil {
// slog.Error("Failed to stop server", slog.String("err", err.Error()))
// os.Exit(1)
// }
return return
} }
} }

View File

@@ -41,12 +41,21 @@ var stopCmd = &cobra.Command{
} }
if *optsStopCmd.Force { if *optsStopCmd.Force {
slog.Debug("force stopping server by SIGKILL", slog.Int("pid", *optsStopCmd.PID)) slog.Debug("force stopping server by SIGKILL", slog.Int("pid", *optsStopCmd.PID))
proc.Signal(syscall.SIGKILL) err = proc.Signal(syscall.SIGKILL)
if err != nil {
slog.Error("failed to reload process", slog.Int("pid", *optsReloadCmd.PID), slog.String("err", err.Error()))
} else {
slog.Debug("done")
}
} else { } else {
slog.Debug("stopping server", slog.Int("pid", *optsStopCmd.PID)) slog.Debug("stopping server", slog.Int("pid", *optsStopCmd.PID))
proc.Signal(syscall.SIGTERM) err = proc.Signal(syscall.SIGTERM)
if err != nil {
slog.Error("failed to reload process", slog.Int("pid", *optsReloadCmd.PID), slog.String("err", err.Error()))
} else {
slog.Debug("done")
}
} }
slog.Debug("done")
}, },
} }

1
go.mod
View File

@@ -9,6 +9,7 @@ require (
require ( require (
github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-chi/chi/v5 v5.2.3 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect

2
go.sum
View File

@@ -7,6 +7,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=

View File

@@ -0,0 +1,27 @@
package server
import (
"sync"
)
type registry struct {
lss map[string]*LiveServer
mu sync.Mutex
}
var reg = registry{
lss: make(map[string]*LiveServer),
}
func pushLs(ls *LiveServer) {
reg.lss[ls.name] = ls
}
func isExists(name string) bool {
_, ok := reg.lss[name]
return ok
}
func deleteLs(name string) {
delete(reg.lss, name)
}

View File

@@ -114,7 +114,12 @@ func Create(name string) (*LiveServer, error) {
return nil, fmt.Errorf("server name is empty") return nil, fmt.Errorf("server name is empty")
} }
if isExists(name) {
return nil, fmt.Errorf("server with this name is already exists")
}
ls := &LiveServer{name: name} ls := &LiveServer{name: name}
pushLs(ls)
ls.setStatus(Status{ID: StatusStopped}) ls.setStatus(Status{ID: StatusStopped})
return ls, nil return ls, nil
} }
@@ -241,6 +246,17 @@ func (ls *LiveServer) stop(inst *instance) error {
return nil return nil
} }
func StopAll() (errors []error) {
for key, ls := range reg.lss {
slog.Debug("stopping LiveServer", slog.String("name", key))
err := ls.Stop()
if err != nil {
errors = append(errors, err)
}
}
return
}
func (ls *LiveServer) Stop() error { func (ls *LiveServer) Stop() error {
inst := ls.active.Load().(*instance) inst := ls.active.Load().(*instance)
if inst == nil { if inst == nil {
@@ -280,6 +296,12 @@ func (ls *LiveServer) Reload(newAddr string) error {
return nil return nil
} }
// Close deletes [LiveServer] object from registry, and sets ls to nil
func (ls *LiveServer) Close() {
deleteLs(ls.name)
ls = nil
}
// package server // package server
// import ( // import (

View File

@@ -1 +1,10 @@
Hiwqe <!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Панель управления</title>
<link rel="stylesheet" href="/static/style.css">
</head>
</html>