Compare commits
No commits in common. "0960f3e9dcf495c205ee3b886b2236eb76669dbd" and "286048d06860f473ce41d807dd14d228d33dacc7" have entirely different histories.
0960f3e9dc
...
286048d068
|
@ -1,8 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cake_crm/internal/app"
|
|
||||||
"cake_crm/internal/models/storage/storage_file"
|
|
||||||
crm "cake_crm/proto"
|
crm "cake_crm/proto"
|
||||||
"context"
|
"context"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||||
|
@ -13,12 +11,30 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type server struct {
|
||||||
|
crm.UnimplementedCRMServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer() *server {
|
||||||
|
return &server{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) GetCatalog(ctx context.Context, req *crm.GetCatalogReq) (*crm.CatalogRsp, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//ctx, cancel := context.WithCancel(context.Background())
|
//ctx, cancel := context.WithCancel(context.Background())
|
||||||
//defer cancel()
|
//defer cancel()
|
||||||
|
//
|
||||||
storage := storage_file.NewStorageFile()
|
//storage := storage_file.NewStorageFile("resources/db.json")
|
||||||
_ = storage
|
//_ = storage
|
||||||
|
//
|
||||||
|
//server := server_web.NewServer(storage, 8080)
|
||||||
|
//err := server.Run(ctx)
|
||||||
|
//if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
//}
|
||||||
|
|
||||||
// Create a listener on TCP port
|
// Create a listener on TCP port
|
||||||
lis, err := net.Listen("tcp", ":8080")
|
lis, err := net.Listen("tcp", ":8080")
|
||||||
|
@ -29,7 +45,7 @@ func main() {
|
||||||
// Create a gRPC server object
|
// Create a gRPC server object
|
||||||
s := grpc.NewServer()
|
s := grpc.NewServer()
|
||||||
// Attach the Greeter service to the server
|
// Attach the Greeter service to the server
|
||||||
crm.RegisterCRMServer(s, app.NewServer(storage))
|
crm.RegisterCRMServer(s, &server{})
|
||||||
// Serve gRPC server
|
// Serve gRPC server
|
||||||
log.Println("Serving gRPC on 0.0.0.0:8080")
|
log.Println("Serving gRPC on 0.0.0.0:8080")
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"cake_crm/internal/models/storage"
|
|
||||||
crm "cake_crm/proto"
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Server struct {
|
|
||||||
crm.UnimplementedCRMServer
|
|
||||||
storage storage.IStorage
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewServer(storage storage.IStorage) *Server {
|
|
||||||
return &Server{
|
|
||||||
storage: storage,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) GetCatalog(ctx context.Context, _ *crm.GetCatalogReq) (*crm.CatalogRsp, error) {
|
|
||||||
categories, err := s.storage.GetCatalog(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &crm.CatalogRsp{Categories: categories}, nil
|
|
||||||
}
|
|
|
@ -1,9 +1,6 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import "context"
|
||||||
crm "cake_crm/proto"
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
|
@ -20,5 +17,4 @@ type IStorage interface {
|
||||||
GetAllProducts(ctx context.Context) ([]Product, error)
|
GetAllProducts(ctx context.Context) ([]Product, error)
|
||||||
GetProductByID(ctx context.Context, id int) (Product, error)
|
GetProductByID(ctx context.Context, id int) (Product, error)
|
||||||
GetBreadcrumbs(ctx context.Context, id int) ([]Breadcrumb, error)
|
GetBreadcrumbs(ctx context.Context, id int) ([]Breadcrumb, error)
|
||||||
GetCatalog(ctx context.Context) ([]*crm.CatalogRsp_Category, error)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,28 +2,17 @@ package storage_file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cake_crm/internal/models/storage"
|
"cake_crm/internal/models/storage"
|
||||||
crm "cake_crm/proto"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type storageFile struct{}
|
type storageFile struct {
|
||||||
|
filepath string
|
||||||
func NewStorageFile() storage.IStorage {
|
|
||||||
return &storageFile{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageFile) GetCatalog(_ context.Context) ([]*crm.CatalogRsp_Category, error) {
|
func NewStorageFile(filepath string) storage.IStorage {
|
||||||
data, err := os.ReadFile("resources/catalog.json")
|
return &storageFile{
|
||||||
if err != nil {
|
filepath: filepath,
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
var res []*crm.CatalogRsp_Category
|
|
||||||
if err := json.Unmarshal(data, &res); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageFile) GetAllProducts(ctx context.Context) ([]storage.Product, error) {
|
func (s *storageFile) GetAllProducts(ctx context.Context) ([]storage.Product, error) {
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
[
|
|
||||||
{
|
|
||||||
"id": 0,
|
|
||||||
"name": "Главная",
|
|
||||||
"uri": "/categories/0",
|
|
||||||
"children": [
|
|
||||||
{
|
|
||||||
"id": 1,
|
|
||||||
"name": "Пряники",
|
|
||||||
"uri": "/categories/1",
|
|
||||||
"children": []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2,
|
|
||||||
"name": "Пахлава",
|
|
||||||
"uri": "/categories/2",
|
|
||||||
"children": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
Loading…
Reference in New Issue