working commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/mbctl"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func (hand *Handler) Authentificate(ctx context.Context) (int64, error) {
|
||||
var err error
|
||||
var accountID int64
|
||||
|
||||
meta, _ := metadata.FromIncomingContext(ctx)
|
||||
usernameArr := meta["username"]
|
||||
passwordArr := meta["password"]
|
||||
if len(usernameArr) == 0 || len(passwordArr) == 0 {
|
||||
err := status.Errorf(codes.PermissionDenied, "Empty auth data")
|
||||
return accountID, err
|
||||
}
|
||||
username := meta["username"][0]
|
||||
password := meta["password"][0]
|
||||
validated, accountID, err := hand.lg.ValidateAcount(ctx, username, password)
|
||||
if !validated {
|
||||
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
|
||||
return accountID, err
|
||||
}
|
||||
return accountID, err
|
||||
}
|
||||
|
||||
func (hand *Handler) CreateAccount(ctx context.Context, params *mbctl.CreateAccountParams) (*mbctl.CreateAccountResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle CreateAccount call")
|
||||
res := &mbctl.CreateAccountResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.CreateAccount(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (hand *Handler) DeleteAccount(ctx context.Context, params *mbctl.DeleteAccountParams) (*mbctl.DeleteAccountResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle DeleteAccount call")
|
||||
res := &mbctl.DeleteAccountResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.DeleteAccount(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (hand *Handler) ListAccounts(ctx context.Context, params *mbctl.ListAccountsParams) (*mbctl.ListAccountsResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle ListAccounts call")
|
||||
res := &mbctl.ListAccountsResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.ListAccounts(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (hand *Handler) UpdateAccount(ctx context.Context, params *mbctl.UpdateAccountParams) (*mbctl.UpdateAccountResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle UpdateAccount call")
|
||||
res := &mbctl.UpdateAccountResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.UpdateAccount(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/mbctl"
|
||||
)
|
||||
|
||||
func (hand *Handler) GetDump(ctx context.Context, params *mbctl.GetDumpParams) (*mbctl.GetDumpResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle GetDump call")
|
||||
res := &mbctl.GetDumpResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.GetDump(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (hand *Handler) RestoreDump(ctx context.Context, params *mbctl.RestoreDumpParams) (*mbctl.RestoreDumpResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle GetDump call")
|
||||
res := &mbctl.RestoreDumpResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.RestoreDump(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/mbctl"
|
||||
)
|
||||
|
||||
func (hand *Handler) SetGrant(ctx context.Context, params *mbctl.SetGrantParams) (*mbctl.SetGrantResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle SetGrant call")
|
||||
res := &mbctl.SetGrantResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.SetGrant(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (hand *Handler) DeleteGrant(ctx context.Context, params *mbctl.DeleteGrantParams) (*mbctl.DeleteGrantResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle DeleteGrant call")
|
||||
res := &mbctl.DeleteGrantResult{}
|
||||
accountID, err := hand.Authentificate(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res, err = hand.lg.DeleteGrant(ctx, accountID, params)
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"mbase/app/logic"
|
||||
"mbase/pkg/mbctl"
|
||||
"mbase/pkg/logger"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type HandlerConfig struct {
|
||||
Logic *logic.Logic
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
mbctl.UnimplementedControlServer
|
||||
lg *logic.Logic
|
||||
log *logger.Logger
|
||||
}
|
||||
|
||||
func NewHandler(conf *HandlerConfig) *Handler {
|
||||
hand := Handler{
|
||||
lg: conf.Logic,
|
||||
}
|
||||
hand.log = logger.NewLogger("ghandler")
|
||||
return &hand
|
||||
}
|
||||
|
||||
func (hand *Handler) Register(gsrv *grpc.Server) {
|
||||
mbctl.RegisterControlServer(gsrv, hand)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mbase/pkg/mbctl"
|
||||
)
|
||||
|
||||
func (hand *Handler) GetHello(ctx context.Context, params *mbctl.GetHelloParams) (*mbctl.GetHelloResult, error) {
|
||||
var err error
|
||||
hand.log.Debugf("Handle getHello call")
|
||||
res, err := hand.lg.GetHello(ctx, params)
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user