diff --git a/cmd/main/node.go b/cmd/main/node.go index 28c0880..7e793d5 100644 --- a/cmd/main/node.go +++ b/cmd/main/node.go @@ -7,6 +7,7 @@ import ( "github.com/akyaiy/GoSally-mvp/internal/config" "github.com/akyaiy/GoSally-mvp/internal/logs" + "github.com/akyaiy/GoSally-mvp/internal/server/v1" "github.com/go-chi/chi/v5" ) @@ -27,15 +28,16 @@ func init() { } func main() { + r := chi.NewRouter() r.Route("/v1/com", func(r chi.Router) { r.Get("/", handleV1ComList) r.Get("/{cmd}", handleV1) }) - r.Route("/v2/com", func(r chi.Router) { - r.Get("/", handleV1ComList) - r.Get("/{cmd}", handleV1) - }) + // r.Route("/v2/com", func(r chi.Router) { + // r.Get("/", handleV1ComList) + // r.Get("/{cmd}", handleV1) + // }) r.NotFound(notFound) log.Info("Server started", slog.String("address", cfg.Address)) http.ListenAndServe(cfg.Address, r) diff --git a/go.mod b/go.mod index cb5fc03..95fb9dc 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,14 @@ module github.com/akyaiy/GoSally-mvp go 1.24.4 require ( + github.com/akyaiy/GoSally-mvp/internal/config v0.0.0-20250622084833-eaff815270bd + github.com/akyaiy/GoSally-mvp/internal/logs v0.0.0-20250622084833-eaff815270bd github.com/go-chi/chi/v5 v5.2.2 - github.com/ilyakaznacheev/cleanenv v1.5.0 ) 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 db7174a..a5fcd73 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ 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/internal/config v0.0.0-20250622084833-eaff815270bd h1:GsTUtyfK/ltBVEJbt7fmL/bOng29XtigkCDjoXUMLTQ= +github.com/akyaiy/GoSally-mvp/internal/config v0.0.0-20250622084833-eaff815270bd/go.mod h1:VCJJWOEkisTU5IBIuNVEc2ahosMRnoVy/I/Dnf79KVM= +github.com/akyaiy/GoSally-mvp/internal/logs v0.0.0-20250622084833-eaff815270bd h1:3PT955Brkjb8F+MoLJfHPBrGKP2DowE+MO7ubCroL0o= +github.com/akyaiy/GoSally-mvp/internal/logs v0.0.0-20250622084833-eaff815270bd/go.mod h1:K2nf8Uk/yx8K61VazkRTjTOHUToQZAugychzlGVx3aw= 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= diff --git a/internal/logs/go.mod b/internal/logs/go.mod index c6aff4c..ee77246 100644 --- a/internal/logs/go.mod +++ b/internal/logs/go.mod @@ -1,12 +1,3 @@ module github.com/akyaiy/GoSally-mvp/internal/logs 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/internal/server/v1/go.mod b/internal/server/v1/go.mod index 4ff106c..24b9db3 100644 --- a/internal/server/v1/go.mod +++ b/internal/server/v1/go.mod @@ -1,4 +1,4 @@ -module github.com/akyaiy/GoSally-mvp/internal/server_v1 +module github.com/akyaiy/GoSally-mvp/internal/server/v1 go 1.24.4 diff --git a/internal/server/v1/handle_com.go b/internal/server/v1/handle_com.go index 45cc882..09e9bf5 100644 --- a/internal/server/v1/handle_com.go +++ b/internal/server/v1/handle_com.go @@ -1,4 +1,4 @@ -package server_v1 +package v1 import ( "encoding/json" diff --git a/internal/server/v1/handle_list.go b/internal/server/v1/handle_list.go index 65ce138..085c3cc 100644 --- a/internal/server/v1/handle_list.go +++ b/internal/server/v1/handle_list.go @@ -1,4 +1,4 @@ -package server_v1 +package v1 import ( "encoding/json" diff --git a/internal/server/v1/server.go b/internal/server/v1/server.go index d030073..d3833ce 100644 --- a/internal/server/v1/server.go +++ b/internal/server/v1/server.go @@ -1,4 +1,4 @@ -package server_v1 +package v1 import ( "log/slog" @@ -12,7 +12,9 @@ type ServerV1UtilsContract interface { extractDescriptionStatic(path string) (string, error) writeJSONError(status int, msg string) newUUID() string - errNotFound() + + _errNotFound() + ErrNotFound(w http.ResponseWriter, r *http.Request) } type ServerV1Contract interface { @@ -37,6 +39,10 @@ type HandlerV1 struct { listAllowedCmd *regexp.Regexp } +func InitV1Server(o *HandlerV1) *HandlerV1 { + return o +} + func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) { h.w = w h.r = r diff --git a/internal/server/v1/utils.go b/internal/server/v1/utils.go index 8b9be0c..315c5a1 100644 --- a/internal/server/v1/utils.go +++ b/internal/server/v1/utils.go @@ -1,4 +1,4 @@ -package server_v1 +package v1 import ( "crypto/rand" @@ -10,6 +10,12 @@ import ( "regexp" ) +func (h *HandlerV1) ErrNotFound(w http.ResponseWriter, r *http.Request) { + h.w = w + h.r = r + h._errNotFound() +} + func (h *HandlerV1) newUUID() string { bytes := make([]byte, 16) _, err := rand.Read(bytes) @@ -20,7 +26,7 @@ func (h *HandlerV1) newUUID() string { return hex.EncodeToString(bytes) } -func (h *HandlerV1) errNotFound() { +func (h *HandlerV1) _errNotFound() { h.writeJSONError(http.StatusBadRequest, "invalid request") h.log.Error("HTTP request error", slog.String("remote", h.r.RemoteAddr), slog.String("method", h.r.Method), slog.String("url", h.r.URL.String()), slog.Int("status", http.StatusBadRequest)) }