Refactor configuration and update handling:

- Modify .luarc.json to include global Lua scripts.
- Update Makefile to include LDFLAGS for versioning.
- Enhance node.go to implement version checking and update handling.
- Refactor Lua global variables in _globals.lua and echo.lua to use new structures.
- Remove deprecated http.lua and update config.yaml for TLS and update settings.
- Introduce new update.go for version management and update checking.
- Add tests for version comparison in update_test.go.
- Improve error handling in various server methods.
This commit is contained in:
alex
2025-07-03 22:38:05 +03:00
parent 96fb13e3c7
commit d442871950
19 changed files with 527 additions and 143 deletions

View File

@@ -1,14 +1,19 @@
package main
import (
"fmt"
"log/slog"
"net"
"net/http"
"regexp"
"golang.org/x/net/netutil"
"github.com/akyaiy/GoSally-mvp/core/config"
gs "github.com/akyaiy/GoSally-mvp/core/general_server"
"github.com/akyaiy/GoSally-mvp/core/logs"
"github.com/akyaiy/GoSally-mvp/core/sv1"
"github.com/akyaiy/GoSally-mvp/core/update"
"github.com/go-chi/chi/v5"
)
@@ -27,34 +32,63 @@ func init() {
}
func main() {
updater := update.NewUpdater(*log, cfg)
versuion, versionType, _ := updater.GetCurrentVersion()
fmt.Printf("Current version: %s (%s)\n", versuion, versionType)
ver, vert, _ := updater.GetLatestVersion(versionType)
fmt.Printf("Latest version: %s (%s)\n", ver, vert)
fmt.Println("Checking for updates...")
isNewUpdate, _ := updater.CkeckUpdates()
fmt.Println("Update check result:", isNewUpdate)
serverv1 := sv1.InitV1Server(&sv1.HandlerV1InitStruct{
Log: *logs.SetupLogger(cfg.Mode),
Log: *log,
Config: cfg,
AllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9]+$`),
ListAllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9_-]+$`),
Ver: "v1",
})
s := gs.InitGeneral(&gs.GeneralServerInit{
Log: *logs.SetupLogger(cfg.Mode),
Log: *log,
Config: cfg,
}, serverv1)
r := chi.NewRouter()
r.Route("/api/{ver}/com", func(r chi.Router) {
r.Route(config.GetServerConsts().GetApiRoute()+config.GetServerConsts().GetComDirRoute(), func(r chi.Router) {
r.Get("/", s.HandleList)
r.Get("/{cmd}", s.Handle)
})
r.Route("/favicon.ico", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
})
r.NotFound(serverv1.ErrNotFound)
if cfg.TlsEnabled == "true" {
log.Info("Server started with TLS", slog.String("address", cfg.Address))
err := http.ListenAndServeTLS(cfg.Address, cfg.CertFile, cfg.KeyFile, r)
address := cfg.Address
if cfg.TlsEnabled {
log.Info("HTTPS server started with TLS", slog.String("address", address))
listener, err := net.Listen("tcp", address)
if err != nil {
log.Error("Failed to start TLS listener", slog.String("error", err.Error()))
return
}
limitedListener := netutil.LimitListener(listener, 100)
err = http.ServeTLS(limitedListener, r, cfg.CertFile, cfg.KeyFile)
if err != nil {
log.Error("Failed to start HTTPS server", slog.String("error", err.Error()))
}
}
log.Info("Server started", slog.String("address", cfg.Address))
err := http.ListenAndServe(cfg.Address, r)
if err != nil {
log.Error("Failed to start HTTP server", slog.String("error", err.Error()))
} else {
log.Info("HTTP server started", slog.String("address", address))
listener, err := net.Listen("tcp", address)
if err != nil {
log.Error("Failed to start listener", slog.String("error", err.Error()))
return
}
limitedListener := netutil.LimitListener(listener, 100)
err = http.Serve(limitedListener, r)
if err != nil {
log.Error("Failed to start HTTP server", slog.String("error", err.Error()))
}
}
}