Skip to content

Commit

Permalink
Merge pull request #51 from moevm/queries_v2
Browse files Browse the repository at this point in the history
Импорт и экспорт БД
  • Loading branch information
defrozentruth authored Dec 2, 2024
2 parents 1c9e908 + 40b55f0 commit bd42e97
Show file tree
Hide file tree
Showing 10 changed files with 848 additions and 0 deletions.
35 changes: 35 additions & 0 deletions server/api/data/v1/data.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
syntax = "proto3";

package data.v1;

option go_package = "data/v1;data_v1";

import "google/api/annotations.proto";

service DataAPI {
rpc ExportDBV1 (ExportDBV1Request) returns (ExportDBV1Response) {
option (google.api.http) = {
get: "/api/data"
};
};
rpc ImportDBV1 (ImportDBV1Request) returns (ImportDBV1Response) {
option (google.api.http) = {
post: "/api/data"
body: "*"
};
};
}
message ExportDBV1Request {

}


message ExportDBV1Response {
string db = 1;
}

message ImportDBV1Request {
string db = 1;
}

message ImportDBV1Response {}
8 changes: 8 additions & 0 deletions server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"google.golang.org/grpc"

"plants/internal/config"
"plants/internal/handlers/data"
"plants/internal/handlers/plants"
"plants/internal/handlers/trades"
"plants/internal/handlers/users"
dataAPI "plants/internal/pkg/pb/data/v1"
plantsAPI "plants/internal/pkg/pb/plants/v1"
tradesAPI "plants/internal/pkg/pb/trades/v1"
usersAPI "plants/internal/pkg/pb/users/v1"
Expand Down Expand Up @@ -47,6 +49,9 @@ func Run(cfg *config.Config) error {
tradesHandler := trades.New(repo)
tradesAPI.RegisterTradesServer(gRPCServer, tradesHandler)

dataHandler := data.New(repo)
dataAPI.RegisterDataAPIServer(gRPCServer, dataHandler)

mux := runtime.NewServeMux()
if err = plantsAPI.RegisterPlantsAPIHandlerServer(ctx, mux, plantsHandler); err != nil {
return err
Expand All @@ -59,6 +64,9 @@ func Run(cfg *config.Config) error {
if err = tradesAPI.RegisterTradesHandlerServer(ctx, mux, tradesHandler); err != nil {
return err
}
if err = dataAPI.RegisterDataAPIHandlerServer(ctx, mux, dataHandler); err != nil {
return err
}

var group errgroup.Group
l, err := net.Listen("tcp", fmt.Sprintf(":%v", cfg.GRPC.Port))
Expand Down
18 changes: 18 additions & 0 deletions server/internal/handlers/data/export_db_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package data

import (
"context"

api "plants/internal/pkg/pb/data/v1"
)

func (h *Handler) ExportDBV1(
ctx context.Context,
req *api.ExportDBV1Request,
) (*api.ExportDBV1Response, error) {
res, err := h.storage.ExportDB(ctx)
if err != nil {
return &api.ExportDBV1Response{}, err
}
return &api.ExportDBV1Response{Db: res}, nil
}
18 changes: 18 additions & 0 deletions server/internal/handlers/data/import_db_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package data

import (
"context"

api "plants/internal/pkg/pb/data/v1"
)

func (h *Handler) ImportDBV1(
ctx context.Context,
req *api.ImportDBV1Request,
) (*api.ImportDBV1Response, error) {
err := h.storage.ImportDB(ctx, req.Db)
if err != nil {
return &api.ImportDBV1Response{}, err
}
return &api.ImportDBV1Response{}, nil
}
23 changes: 23 additions & 0 deletions server/internal/handlers/data/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package data

import (
"context"

v1 "plants/internal/pkg/pb/data/v1"
)

type Storage interface {
ExportDB(context.Context) (string, error)
ImportDB(context.Context, string) error
}

type Handler struct {
v1.UnimplementedDataAPIServer
storage Storage
}

func New(storage Storage) *Handler {
return &Handler{
storage: storage,
}
}
Loading

0 comments on commit bd42e97

Please sign in to comment.