Refactor server package structure and update error handling methods; remove unused dependencies

This commit is contained in:
alex
2025-06-22 11:54:16 +03:00
parent eaff815270
commit 0d8673254e
9 changed files with 32 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/akyaiy/GoSally-mvp/internal/config" "github.com/akyaiy/GoSally-mvp/internal/config"
"github.com/akyaiy/GoSally-mvp/internal/logs" "github.com/akyaiy/GoSally-mvp/internal/logs"
"github.com/akyaiy/GoSally-mvp/internal/server/v1"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
@@ -27,15 +28,16 @@ func init() {
} }
func main() { func main() {
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("/", handleV1ComList)
r.Get("/{cmd}", handleV1) r.Get("/{cmd}", handleV1)
}) })
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(notFound)
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)

4
go.mod
View File

@@ -3,12 +3,14 @@ module github.com/akyaiy/GoSally-mvp
go 1.24.4 go 1.24.4
require ( 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/go-chi/chi/v5 v5.2.2
github.com/ilyakaznacheev/cleanenv v1.5.0
) )
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

4
go.sum
View File

@@ -1,6 +1,10 @@
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= 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 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= 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 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= 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 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=

View File

@@ -1,12 +1,3 @@
module github.com/akyaiy/GoSally-mvp/internal/logs module github.com/akyaiy/GoSally-mvp/internal/logs
go 1.24.4 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,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 go 1.24.4

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
package server_v1 package v1
import ( import (
"log/slog" "log/slog"
@@ -12,7 +12,9 @@ type ServerV1UtilsContract interface {
extractDescriptionStatic(path string) (string, error) extractDescriptionStatic(path string) (string, error)
writeJSONError(status int, msg string) writeJSONError(status int, msg string)
newUUID() string newUUID() string
errNotFound()
_errNotFound()
ErrNotFound(w http.ResponseWriter, r *http.Request)
} }
type ServerV1Contract interface { type ServerV1Contract interface {
@@ -37,6 +39,10 @@ type HandlerV1 struct {
listAllowedCmd *regexp.Regexp listAllowedCmd *regexp.Regexp
} }
func InitV1Server(o *HandlerV1) *HandlerV1 {
return o
}
func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) { func (h *HandlerV1) Handle(w http.ResponseWriter, r *http.Request) {
h.w = w h.w = w
h.r = r h.r = r

View File

@@ -1,4 +1,4 @@
package server_v1 package v1
import ( import (
"crypto/rand" "crypto/rand"
@@ -10,6 +10,12 @@ import (
"regexp" "regexp"
) )
func (h *HandlerV1) ErrNotFound(w http.ResponseWriter, r *http.Request) {
h.w = w
h.r = r
h._errNotFound()
}
func (h *HandlerV1) newUUID() string { func (h *HandlerV1) newUUID() string {
bytes := make([]byte, 16) bytes := make([]byte, 16)
_, err := rand.Read(bytes) _, err := rand.Read(bytes)
@@ -20,7 +26,7 @@ func (h *HandlerV1) newUUID() string {
return hex.EncodeToString(bytes) return hex.EncodeToString(bytes)
} }
func (h *HandlerV1) errNotFound() { func (h *HandlerV1) _errNotFound() {
h.writeJSONError(http.StatusBadRequest, "invalid request") 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)) 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))
} }