diff --git a/cmd/cake_crm/main.go b/cmd/cake_crm/main.go index 15ae042..b967048 100644 --- a/cmd/cake_crm/main.go +++ b/cmd/cake_crm/main.go @@ -2,9 +2,20 @@ package main import ( "cake_crm/internal/models/storage/storage_file" + "cake_crm/internal/services/server_web" + "context" ) func main() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + storage := storage_file.NewStorageFile("resources/db.json") _ = storage + + server := server_web.NewServer(storage, 8080) + err := server.Run(ctx) + if err != nil { + panic(err) + } } diff --git a/go.mod b/go.mod index bed3259..5ef30ff 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module cake_crm go 1.22 + +require github.com/go-pkgz/routegroup v1.1.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0cdb981 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-pkgz/routegroup v1.1.1 h1:Dm5IBiEmUbQT+3rliBimhX0SifnZp/uRF/WOu3XPmms= +github.com/go-pkgz/routegroup v1.1.1/go.mod h1:kDDPDRLRiRY1vnENrZJw1jQAzQX7fvsbsHGRQFNQfKc= diff --git a/internal/services/server_web/interface.go b/internal/services/server_web/interface.go new file mode 100644 index 0000000..b3a74d3 --- /dev/null +++ b/internal/services/server_web/interface.go @@ -0,0 +1,8 @@ +package server_web + +import "context" + +type IServer interface { + Run(ctx context.Context) error + Stop(ctx context.Context) error +} diff --git a/internal/services/server_web/server.go b/internal/services/server_web/server.go new file mode 100644 index 0000000..0d38c31 --- /dev/null +++ b/internal/services/server_web/server.go @@ -0,0 +1,43 @@ +package server_web + +import ( + "cake_crm/internal/models/storage" + "context" + "fmt" + "github.com/go-pkgz/routegroup" + "net/http" +) + +type server struct { + storage storage.IStorage + port int +} + +func NewServer( + storage storage.IStorage, + port int, +) IServer { + return &server{ + storage: storage, + port: port, + } +} + +func (s *server) Run(ctx context.Context) error { + router := routegroup.New(http.NewServeMux()) + router.HandleFunc("GET /products", s.getAllProductsHandler) + router.HandleFunc("GET /products/:id", s.getProductById) + router.HandleFunc("GET /category/:id/breadcrumbs", s.getBreadcrumbsByCategoryId) + return http.ListenAndServe(fmt.Sprintf(":%d", s.port), router) +} + +func (s *server) Stop(ctx context.Context) error { + //TODO implement me + panic("implement me") +} + +func (s *server) getAllProductsHandler(w http.ResponseWriter, r *http.Request) {} + +func (s *server) getProductById(w http.ResponseWriter, r *http.Request) {} + +func (s *server) getBreadcrumbsByCategoryId(w http.ResponseWriter, r *http.Request) {}