Refactor server structure: migrate v1 functionality to sv1 module, remove deprecated files, and update command handling

This commit is contained in:
alex
2025-06-22 22:11:14 +03:00
parent 114fc82b90
commit 03195dca59
12 changed files with 33 additions and 82 deletions

View File

@@ -7,15 +7,13 @@ import (
"github.com/akyaiy/GoSally-mvp/config" "github.com/akyaiy/GoSally-mvp/config"
"github.com/akyaiy/GoSally-mvp/logs" "github.com/akyaiy/GoSally-mvp/logs"
//"github.com/akyaiy/GoSally-mvp/v1" "github.com/akyaiy/GoSally-mvp/sv1"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
var allowedCmd = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
var log *slog.Logger var log *slog.Logger
var cfg *config.ConfigConf var cfg *config.ConfigConf
var listAllowedCmd = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) // allowed symbols after first symbol
func init() { func init() {
cfg = config.MustLoadConfig() cfg = config.MustLoadConfig()
@@ -28,17 +26,22 @@ func init() {
} }
func main() { 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 := chi.NewRouter()
r.Route("/v1/com", func(r chi.Router) { r.Route("/v1/com", func(r chi.Router) {
r.Get("/", handleV1ComList) r.Get("/", serverv1.HandleList)
r.Get("/{cmd}", handleV1) r.Get("/{cmd}", serverv1.Handle)
}) })
// r.Route("/v2/com", func(r chi.Router) { // r.Route("/v2/com", func(r chi.Router) {
// r.Get("/", handleV1ComList) // r.Get("/", handleV1ComList)
// r.Get("/{cmd}", handleV1) // r.Get("/{cmd}", handleV1)
// }) // })
r.NotFound(notFound) r.NotFound(serverv1.ErrNotFound)
log.Info("Server started", slog.String("address", cfg.Address)) log.Info("Server started", slog.String("address", cfg.Address))
http.ListenAndServe(cfg.Address, r) http.ListenAndServe(cfg.Address, r)

View File

@@ -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
)

View File

@@ -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=

5
go.mod
View File

@@ -3,14 +3,13 @@ module github.com/akyaiy/GoSally-mvp
go 1.24.4 go 1.24.4
require ( 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/go-chi/chi/v5 v5.2.2
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/yuin/gopher-lua v1.1.1
) )
require ( require (
github.com/BurntSushi/toml v1.5.0 // indirect github.com/BurntSushi/toml v1.5.0 // indirect
github.com/ilyakaznacheev/cleanenv v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/joho/godotenv v1.5.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect

2
go.sum
View File

@@ -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/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 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -1,3 +0,0 @@
module github.com/akyaiy/GoSally-mvp/logs
go 1.24.4

View File

@@ -1,4 +1,4 @@
package v1 package sv1
import ( import (
"encoding/json" "encoding/json"

View File

@@ -1,4 +1,4 @@
package v1 package sv1
import ( import (
"encoding/json" "encoding/json"

View File

@@ -1,4 +1,4 @@
package v1 package sv1
import ( import (
"log/slog" "log/slog"
@@ -27,6 +27,14 @@ type ServerV1Contract interface {
_handleList() _handleList()
} }
// structure only for initialization
type HandlerV1InitStruct struct {
Log slog.Logger
Config *config.ConfigConf
AllowedCmd *regexp.Regexp
ListAllowedCmd *regexp.Regexp
}
type HandlerV1 struct { type HandlerV1 struct {
w http.ResponseWriter w http.ResponseWriter
r *http.Request r *http.Request
@@ -39,8 +47,13 @@ type HandlerV1 struct {
listAllowedCmd *regexp.Regexp listAllowedCmd *regexp.Regexp
} }
func InitV1Server(o *HandlerV1) *HandlerV1 { func InitV1Server(o *HandlerV1InitStruct) *HandlerV1 {
return o return &HandlerV1{
log: o.Log,
cfg: o.Config,
allowedCmd: o.AllowedCmd,
listAllowedCmd: o.ListAllowedCmd,
}
} }
func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) { func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) {

View File

@@ -1,4 +1,4 @@
package v1 package sv1
import ( import (
"crypto/rand" "crypto/rand"

View File

@@ -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
)

View File

@@ -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=