From 03195dca599e762c6e08634bd49fa7840a41963b Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 22 Jun 2025 22:11:14 +0300 Subject: [PATCH] Refactor server structure: migrate v1 functionality to sv1 module, remove deprecated files, and update command handling --- cmd/{main => node}/node.go | 17 ++++++++++------- config/go.mod | 12 ------------ config/go.sum | 13 ------------- go.mod | 5 ++--- go.sum | 2 ++ logs/go.mod | 3 --- {v1 => sv1}/handle_com.go | 2 +- {v1 => sv1}/handle_list.go | 2 +- {v1 => sv1}/server.go | 19 ++++++++++++++++--- {v1 => sv1}/utils.go | 2 +- v1/go.mod | 17 ----------------- v1/go.sum | 21 --------------------- 12 files changed, 33 insertions(+), 82 deletions(-) rename cmd/{main => node}/node.go (66%) delete mode 100644 config/go.mod delete mode 100644 config/go.sum delete mode 100644 logs/go.mod rename {v1 => sv1}/handle_com.go (99%) rename {v1 => sv1}/handle_list.go (99%) rename {v1 => sv1}/server.go (68%) rename {v1 => sv1}/utils.go (99%) delete mode 100644 v1/go.mod delete mode 100644 v1/go.sum diff --git a/cmd/main/node.go b/cmd/node/node.go similarity index 66% rename from cmd/main/node.go rename to cmd/node/node.go index 40b53ba..8782fe4 100644 --- a/cmd/main/node.go +++ b/cmd/node/node.go @@ -7,15 +7,13 @@ import ( "github.com/akyaiy/GoSally-mvp/config" "github.com/akyaiy/GoSally-mvp/logs" - //"github.com/akyaiy/GoSally-mvp/v1" + "github.com/akyaiy/GoSally-mvp/sv1" "github.com/go-chi/chi/v5" ) -var allowedCmd = regexp.MustCompile(`^[a-zA-Z0-9]+$`) var log *slog.Logger var cfg *config.ConfigConf -var listAllowedCmd = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) // allowed symbols after first symbol func init() { cfg = config.MustLoadConfig() @@ -28,17 +26,22 @@ func init() { } func main() { - + serverv1 := sv1.InitV1Server(&sv1.HandlerV1InitStruct{ + Log: *logs.SetupLogger(cfg.Mode), + Config: cfg, + AllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9]+$`), + ListAllowedCmd: regexp.MustCompile(`^[a-zA-Z0-9_-]+$`), + }) r := chi.NewRouter() r.Route("/v1/com", func(r chi.Router) { - r.Get("/", handleV1ComList) - r.Get("/{cmd}", handleV1) + r.Get("/", serverv1.HandleList) + r.Get("/{cmd}", serverv1.Handle) }) // r.Route("/v2/com", func(r chi.Router) { // r.Get("/", handleV1ComList) // r.Get("/{cmd}", handleV1) // }) - r.NotFound(notFound) + r.NotFound(serverv1.ErrNotFound) log.Info("Server started", slog.String("address", cfg.Address)) http.ListenAndServe(cfg.Address, r) diff --git a/config/go.mod b/config/go.mod deleted file mode 100644 index a02ac1d..0000000 --- a/config/go.mod +++ /dev/null @@ -1,12 +0,0 @@ -module github.com/akyaiy/GoSally-mvp/config - -go 1.24.4 - -require github.com/ilyakaznacheev/cleanenv v1.5.0 - -require ( - github.com/BurntSushi/toml v1.5.0 // indirect - github.com/joho/godotenv v1.5.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect - olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect -) diff --git a/config/go.sum b/config/go.sum deleted file mode 100644 index 9e4a00f..0000000 --- a/config/go.sum +++ /dev/null @@ -1,13 +0,0 @@ -github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= -github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= -github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4= -github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ= -olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw= diff --git a/go.mod b/go.mod index b855562..6111cc9 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,13 @@ module github.com/akyaiy/GoSally-mvp go 1.24.4 require ( - github.com/akyaiy/GoSally-mvp/config v0.0.0-20250622141207-5326dd45b694 - github.com/akyaiy/GoSally-mvp/logs v0.0.0-20250622141207-5326dd45b694 github.com/go-chi/chi/v5 v5.2.2 + github.com/ilyakaznacheev/cleanenv v1.5.0 + github.com/yuin/gopher-lua v1.1.1 ) require ( github.com/BurntSushi/toml v1.5.0 // indirect - github.com/ilyakaznacheev/cleanenv v1.5.0 // indirect github.com/joho/godotenv v1.5.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect diff --git a/go.sum b/go.sum index df55eda..92f3ef5 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2l github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/logs/go.mod b/logs/go.mod deleted file mode 100644 index b7c90b0..0000000 --- a/logs/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/akyaiy/GoSally-mvp/logs - -go 1.24.4 diff --git a/v1/handle_com.go b/sv1/handle_com.go similarity index 99% rename from v1/handle_com.go rename to sv1/handle_com.go index 09e9bf5..6cf0a24 100644 --- a/v1/handle_com.go +++ b/sv1/handle_com.go @@ -1,4 +1,4 @@ -package v1 +package sv1 import ( "encoding/json" diff --git a/v1/handle_list.go b/sv1/handle_list.go similarity index 99% rename from v1/handle_list.go rename to sv1/handle_list.go index 085c3cc..54f31ca 100644 --- a/v1/handle_list.go +++ b/sv1/handle_list.go @@ -1,4 +1,4 @@ -package v1 +package sv1 import ( "encoding/json" diff --git a/v1/server.go b/sv1/server.go similarity index 68% rename from v1/server.go rename to sv1/server.go index 9565942..ddc49fc 100644 --- a/v1/server.go +++ b/sv1/server.go @@ -1,4 +1,4 @@ -package v1 +package sv1 import ( "log/slog" @@ -27,6 +27,14 @@ type ServerV1Contract interface { _handleList() } +// structure only for initialization +type HandlerV1InitStruct struct { + Log slog.Logger + Config *config.ConfigConf + AllowedCmd *regexp.Regexp + ListAllowedCmd *regexp.Regexp +} + type HandlerV1 struct { w http.ResponseWriter r *http.Request @@ -39,8 +47,13 @@ type HandlerV1 struct { listAllowedCmd *regexp.Regexp } -func InitV1Server(o *HandlerV1) *HandlerV1 { - return o +func InitV1Server(o *HandlerV1InitStruct) *HandlerV1 { + return &HandlerV1{ + log: o.Log, + cfg: o.Config, + allowedCmd: o.AllowedCmd, + listAllowedCmd: o.ListAllowedCmd, + } } func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) { diff --git a/v1/utils.go b/sv1/utils.go similarity index 99% rename from v1/utils.go rename to sv1/utils.go index 315c5a1..6fe38a9 100644 --- a/v1/utils.go +++ b/sv1/utils.go @@ -1,4 +1,4 @@ -package v1 +package sv1 import ( "crypto/rand" diff --git a/v1/go.mod b/v1/go.mod deleted file mode 100644 index de1f8dc..0000000 --- a/v1/go.mod +++ /dev/null @@ -1,17 +0,0 @@ -module github.com/akyaiy/GoSally-mvp/v1 - -go 1.24.4 - -require ( - github.com/akyaiy/GoSally-mvp/config v0.0.0-20250622140947-27bda702bda3 - github.com/go-chi/chi/v5 v5.2.2 - github.com/yuin/gopher-lua v1.1.1 -) - -require ( - github.com/BurntSushi/toml v1.5.0 // indirect - github.com/ilyakaznacheev/cleanenv v1.5.0 // indirect - github.com/joho/godotenv v1.5.1 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect - olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect -) diff --git a/v1/go.sum b/v1/go.sum deleted file mode 100644 index af50e9b..0000000 --- a/v1/go.sum +++ /dev/null @@ -1,21 +0,0 @@ -github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= -github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= -github.com/akyaiy/GoSally-mvp/config v0.0.0-20250622140947-27bda702bda3 h1:WJ3bUDGpZikuxTqOljdX3jWybBTNT7jqH9+7giPYsh4= -github.com/akyaiy/GoSally-mvp/config v0.0.0-20250622140947-27bda702bda3/go.mod h1:2eaoBiPQmvZoC9DAzn11zHzWmscBI4dMTi4HeGO96XQ= -github.com/akyaiy/GoSally-mvp/internal/config v0.0.0-20250622140114-c15e6c5b1592 h1:V5QzqnFoDnOVpjxMb0ZsBAX5jDaPE6OEiHxZ40/pqxM= -github.com/akyaiy/GoSally-mvp/internal/config v0.0.0-20250622140114-c15e6c5b1592/go.mod h1:VCJJWOEkisTU5IBIuNVEc2ahosMRnoVy/I/Dnf79KVM= -github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618= -github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= -github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4= -github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= -github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ= -olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=