-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bastien Rigaud
committed
Feb 16, 2024
1 parent
9a9505a
commit a1e5f9b
Showing
8 changed files
with
163 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/Golerplate/user-store-svc/src/internal/config" | ||
handler_user_v1 "github.com/Golerplate/user-store-svc/src/internal/handler/user/v1" | ||
server_user_v1 "github.com/Golerplate/user-store-svc/src/internal/server/user/v1" | ||
"github.com/Golerplate/user-store-svc/src/internal/service" | ||
"github.com/rs/zerolog/log" | ||
"golang.org/x/net/http2" | ||
"golang.org/x/net/http2/h2c" | ||
|
||
serviceDatastore "github.com/Golerplate/user-store-svc/src/internal/service/datastore/planetscale" | ||
) | ||
|
||
func main() { | ||
// ctx, cancel := context.WithCancel(context.Background()) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
// sigs := make(chan os.Signal, 1) | ||
// signal.Notify(sigs, syscall.SIGTERM) | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGTERM) | ||
|
||
// cfg := config.GetServiceConfig() | ||
cfg := config.GetServiceConfig() | ||
|
||
// userStoreServiceDatastore := serviceDatastore.NewPlanetScaleDatastore() | ||
// userStoreService := service.NewUserStoreService(userStoreServiceDatastore) | ||
// userStoreServiceHandler := handler_user_v1.NewUserStoreServiceHandler(userStoreService) | ||
userStoreServiceDatastore := serviceDatastore.NewPlanetScaleDatastore() | ||
userStoreService := service.NewUserStoreService(userStoreServiceDatastore) | ||
userStoreServiceHandler := handler_user_v1.NewUserStoreServiceHandler(userStoreService) | ||
|
||
// grpcBuilder := server_user_v1.NewGRPCBuilder(userStoreServiceHandler) | ||
grpcBuilder := server_user_v1.NewGRPCBuilder(userStoreServiceHandler) | ||
|
||
// grpcServer := http.Server{ | ||
// Addr: fmt.Sprintf(":%d", cfg.GRPCServerConfig.Port), | ||
// Handler: h2c.NewHandler(grpcBuilder, &http2.Server{}), | ||
// } | ||
grpcServer := http.Server{ | ||
Addr: fmt.Sprintf(":%d", cfg.GRPCServerConfig.Port), | ||
Handler: h2c.NewHandler(grpcBuilder, &http2.Server{}), | ||
} | ||
|
||
// go func() { | ||
// if err := grpcServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
// log.Fatal().Err(err).Msg("unable to start grpc server") | ||
// } | ||
// }() | ||
go func() { | ||
if err := grpcServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatal().Err(err).Msg("unable to start grpc server") | ||
} | ||
}() | ||
|
||
// <-sigs | ||
<-sigs | ||
|
||
// log.Info().Msg("caught SIGTERM, exiting") | ||
// cancel() | ||
log.Info().Msg("caught SIGTERM, exiting") | ||
cancel() | ||
|
||
// if err := grpcServer.Shutdown(ctx); err != nil { | ||
// log.Error().Err(err).Msg("unable to stop grpc server") | ||
// } | ||
if err := grpcServer.Shutdown(ctx); err != nil { | ||
log.Error().Err(err).Msg("unable to stop grpc server") | ||
} | ||
|
||
// log.Info().Msg("work done; exiting") | ||
log.Info().Msg("work done; exiting") | ||
|
||
// os.Exit(0) | ||
fmt.Println("Hello, World!") | ||
os.Exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package handler_user_v1 | ||
|
||
// import ( | ||
// "github.com/Golerplate/contracts/generated/services/user/store/v1/storev1connect" | ||
// "github.com/Golerplate/user-store-svc/src/internal/service" | ||
// ) | ||
import ( | ||
"github.com/Golerplate/contracts/generated/services/user/store/v1/storev1connect" | ||
"github.com/Golerplate/user-store-svc/src/internal/service" | ||
) | ||
|
||
// type handler struct { | ||
// userStoreService service.UserStoreService | ||
// } | ||
type handler struct { | ||
userStoreService service.UserStoreService | ||
} | ||
|
||
// func NewUserStoreServiceHandler(userStoreService service.UserStoreService) storev1connect.UserStoreServiceHandler { | ||
// return &handler{ | ||
// userStoreService: userStoreService, | ||
// } | ||
// } | ||
func NewUserStoreServiceHandler(userStoreService service.UserStoreService) storev1connect.UserStoreServiceHandler { | ||
return &handler{ | ||
userStoreService: userStoreService, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package handler_user_v1 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
userv1 "github.com/Golerplate/contracts/generated/services/user/store/v1" | ||
"github.com/Golerplate/pkg/grpc" | ||
entity_user_v1 "github.com/Golerplate/user-store-svc/src/internal/entity/user/v1" | ||
connectgo "github.com/bufbuild/connect-go" | ||
"github.com/golang/protobuf/ptypes/wrappers" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func (h *handler) CreateUser(ctx context.Context, c *connectgo.Request[userv1.CreateUserRequest]) (*connectgo.Response[userv1.CreateUserResponse], error) { | ||
|
||
if c.Msg.GetUsername() == nil || c.Msg.GetUsername().GetValue() == "" { | ||
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid username")) | ||
} | ||
if c.Msg.GetEmail() == nil || c.Msg.GetEmail().GetValue() == "" { | ||
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid email")) | ||
} | ||
if c.Msg.GetPassword() == nil || c.Msg.GetPassword().GetValue() == "" { | ||
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid password")) | ||
} | ||
|
||
user, err := h.userStoreService.CreateUser(ctx, &entity_user_v1.CreateUserRequest{ | ||
Username: c.Msg.GetUsername().GetValue(), | ||
Email: c.Msg.GetEmail().GetValue(), | ||
Password: c.Msg.GetPassword().GetValue(), | ||
}) | ||
if err != nil { | ||
return nil, grpc.TranslateToGRPCError(ctx, err) | ||
} | ||
|
||
return connectgo.NewResponse(&userv1.CreateUserResponse{ | ||
User: &userv1.User{ | ||
Id: &wrappers.StringValue{Value: user.ID}, | ||
Username: &wrappers.StringValue{Value: user.Username}, | ||
Email: &wrappers.StringValue{Value: user.Email}, | ||
IsAdmin: &wrappers.BoolValue{Value: user.IsAdmin}, | ||
IsBanned: &wrappers.BoolValue{Value: user.IsBanned}, | ||
CreatedAt: ×tamppb.Timestamp{Seconds: int64(user.CreatedAt.Second()), Nanos: int32(user.CreatedAt.Nanosecond())}, | ||
UpdatedAt: ×tamppb.Timestamp{Seconds: int64(user.UpdatedAt.Second()), Nanos: int32(user.UpdatedAt.Nanosecond())}, | ||
}, | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
package server_user_v1 | ||
|
||
// import ( | ||
// "net/http" | ||
import ( | ||
"net/http" | ||
|
||
// connectgo "github.com/bufbuild/connect-go" | ||
// grpcreflect "github.com/bufbuild/connect-grpcreflect-go" | ||
// connect_go_prometheus "github.com/easyCZ/connect-go-prometheus" | ||
// ) | ||
"github.com/Golerplate/contracts/generated/services/servicesconnect" | ||
"github.com/Golerplate/contracts/generated/services/user/store/v1/storev1connect" | ||
handlers "github.com/Golerplate/pkg/grpc/handlers" | ||
sharedmidlewares "github.com/Golerplate/pkg/grpc/interceptors" | ||
connectgo "github.com/bufbuild/connect-go" | ||
grpcreflect "github.com/bufbuild/connect-grpcreflect-go" | ||
) | ||
|
||
// func NewGRPCBuilder( | ||
// chnlStoreServiceHandler storev1connect.ChnlStoreServiceHandler, | ||
// ) *http.ServeMux { | ||
// interceptorchain := append(sharedmidlewares.ServerDefaultChain(), connect_go_prometheus.NewInterceptor()) | ||
// interceptors := connectgo.WithInterceptors(interceptorchain...) | ||
func NewGRPCBuilder( | ||
userStoreServiceHandler storev1connect.UserStoreServiceHandler, | ||
) *http.ServeMux { | ||
interceptors := connectgo.WithInterceptors(sharedmidlewares.ServerDefaultChain()...) | ||
|
||
// reflector := grpcreflect.NewStaticReflector( | ||
// "services.user.store.v1.UserStoreService", "services.health.HealthService", | ||
// ) | ||
reflector := grpcreflect.NewStaticReflector( | ||
"services.user.store.v1.UserStoreSvc", "services.health.HealthService", | ||
) | ||
|
||
// mux := http.NewServeMux() | ||
// mux.Handle(servicesconnect.NewHealthServiceHandler(handlers.NewHealthHandler())) | ||
// mux.Handle(storev1connect.NewChnlStoreServiceHandler(chnlStoreServiceHandler, interceptors)) | ||
// mux.Handle(grpcreflect.NewHandlerV1(reflector)) | ||
// mux.Handle(grpcreflect.NewHandlerV1Alpha(reflector)) | ||
mux := http.NewServeMux() | ||
mux.Handle(servicesconnect.NewHealthServiceHandler(handlers.NewHealthHandler())) | ||
mux.Handle(storev1connect.NewUserStoreServiceHandler(userStoreServiceHandler, interceptors)) | ||
mux.Handle(grpcreflect.NewHandlerV1(reflector)) | ||
mux.Handle(grpcreflect.NewHandlerV1Alpha(reflector)) | ||
|
||
// return mux | ||
// } | ||
return mux | ||
} |