Browse Source

refactor(api): implement latest grpc changes

Mustafa ARICI 4 năm trước cách đây
mục cha
commit
a7cc149961
3 tập tin đã thay đổi với 16 bổ sung8 xóa
  1. 2 2
      api/auth.go
  2. 2 2
      api/rest.go
  3. 12 4
      api/rpc.go

+ 2 - 2
api/auth.go

@@ -4,9 +4,9 @@ import (
 	"fmt"
 	"strings"
 
-	"github.com/sirupsen/logrus"
 	"github.com/cad/ovpm"
 	"github.com/cad/ovpm/permset"
+	"github.com/sirupsen/logrus"
 	gcontext "golang.org/x/net/context"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/codes"
@@ -26,7 +26,7 @@ func authRequired(ctx gcontext.Context, req interface{}, handler grpc.UnaryHandl
 		return nil, grpc.Errorf(codes.Unauthenticated, "access denied")
 	}
 
-	// Set user's permissions according to it's criterias.
+	// Set user's permissions according to it's criteria.
 	var permissions permset.Permset
 	if user.IsAdmin() {
 		permissions = permset.New(ovpm.AdminPerms()...)

+ 2 - 2
api/rest.go

@@ -10,7 +10,7 @@ import (
 	"github.com/cad/ovpm/bundle"
 	assetfs "github.com/elazarl/go-bindata-assetfs"
 	"github.com/go-openapi/runtime/middleware"
-	"github.com/grpc-ecosystem/grpc-gateway/runtime"
+	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
 	"github.com/sirupsen/logrus"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
@@ -26,7 +26,7 @@ func NewRESTServer(grpcPort string) (http.Handler, context.CancelFunc, error) {
 	}
 	endPoint := fmt.Sprintf("localhost:%s", grpcPort)
 	ctx = NewOriginTypeContext(ctx, OriginTypeREST)
-	gmux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
+	gmux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{}))
 	opts := []grpc.DialOption{grpc.WithInsecure()}
 	err := pb.RegisterVPNServiceHandlerFromEndpoint(ctx, gmux, endPoint, opts)
 	if err != nil {

+ 12 - 4
api/rpc.go

@@ -15,7 +15,9 @@ import (
 	"golang.org/x/net/context"
 )
 
-type AuthService struct{}
+type AuthService struct {
+	pb.UnimplementedAuthServiceServer
+}
 
 func (s *AuthService) Status(ctx context.Context, req *pb.AuthStatusRequest) (*pb.AuthStatusResponse, error) {
 	logrus.Debug("rpc call: auth status")
@@ -65,7 +67,9 @@ func (s *AuthService) Authenticate(ctx context.Context, req *pb.AuthAuthenticate
 	return &pb.AuthAuthenticateResponse{Token: token}, nil
 }
 
-type UserService struct{}
+type UserService struct {
+	pb.UnimplementedUserServiceServer
+}
 
 func (s *UserService) List(ctx context.Context, req *pb.UserListRequest) (*pb.UserResponse, error) {
 	logrus.Debug("rpc call: user list")
@@ -326,7 +330,9 @@ func (s *UserService) GenConfig(ctx context.Context, req *pb.UserGenConfigReques
 	return nil, grpc.Errorf(codes.PermissionDenied, "Permissions are required for this operation.")
 }
 
-type VPNService struct{}
+type VPNService struct {
+	pb.UnimplementedVPNServiceServer
+}
 
 func (s *VPNService) Status(ctx context.Context, req *pb.VPNStatusRequest) (*pb.VPNStatusResponse, error) {
 	logrus.Debugf("rpc call: vpn status")
@@ -426,7 +432,9 @@ func (s *VPNService) Restart(ctx context.Context, req *pb.VPNRestartRequest) (*p
 	return &pb.VPNRestartResponse{}, nil
 }
 
-type NetworkService struct{}
+type NetworkService struct {
+	pb.UnimplementedNetworkServiceServer
+}
 
 func (s *NetworkService) List(ctx context.Context, req *pb.NetworkListRequest) (*pb.NetworkListResponse, error) {
 	logrus.Debug("rpc call: network list")