Public Access
Chore/refactor #1
@@ -49,8 +49,6 @@ import (
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
_ "github.com/jackc/pgx/v5/stdlib"
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var interruptSignals = []os.Signal{
|
var interruptSignals = []os.Signal{
|
||||||
@@ -59,25 +57,24 @@ var interruptSignals = []os.Signal{
|
|||||||
syscall.SIGINT,
|
syscall.SIGINT,
|
||||||
}
|
}
|
||||||
|
|
||||||
// @title Simple Bank API
|
// @title App Template API
|
||||||
// @version 1.0
|
// @version 1.0
|
||||||
// @description API for Simple Bank application
|
// @description REST API template: accounts (register/login/email verification/refresh tokens) and multi-tenant organizations (members/invites) already wired up.
|
||||||
// @termsOfService http://swagger.io/terms/
|
// @termsOfService http://swagger.io/terms/
|
||||||
|
|
||||||
// @contact.name API Support
|
// @contact.name App Template
|
||||||
// @contact.email [email protected]
|
|
||||||
|
|
||||||
// @license.name MIT
|
// @license.name MIT
|
||||||
// @license.url https://opensource.org/licenses/MIT
|
// @license.url https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
// @host localhost:8080
|
// @host localhost:8080
|
||||||
// @BasePath /v1
|
// @BasePath /api/v1
|
||||||
// @schemes http https
|
// @schemes http https
|
||||||
|
|
||||||
// @securityDefinitions.apikey BearerAuth
|
// @securityDefinitions.apikey BearerAuth
|
||||||
// @in header
|
// @in header
|
||||||
// @name Authorization
|
// @name Authorization
|
||||||
// @description Type "Bearer" followed by a space and JWT token.
|
// @description PASETO access token. Format: "Bearer <token>". Some endpoints also accept the equivalent value via the "access_token" cookie.
|
||||||
func main() {
|
func main() {
|
||||||
config, err := config.LoadConfig(".")
|
config, err := config.LoadConfig(".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -86,6 +83,7 @@ func main() {
|
|||||||
|
|
||||||
if config.Environment == "development" {
|
if config.Environment == "development" {
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), interruptSignals...)
|
ctx, cancel := signal.NotifyContext(context.Background(), interruptSignals...)
|
||||||
@@ -114,15 +112,20 @@ func main() {
|
|||||||
|
|
||||||
userService := service.NewUserService(store, taskDist)
|
userService := service.NewUserService(store, taskDist)
|
||||||
authService := service.NewAuthService(store, tokenMaker, config)
|
authService := service.NewAuthService(store, tokenMaker, config)
|
||||||
|
orgService := service.NewOrganizationService(store, taskDist)
|
||||||
|
|
||||||
wg, ctx := errgroup.WithContext(ctx)
|
wg, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
{{ if not .UseGRPC }}
|
{{ if not .UseGRPC }}
|
||||||
runGinServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService)
|
services := api.Services{
|
||||||
|
UserService: userService,
|
||||||
|
AuthService: authService,
|
||||||
|
OrganizationService: orgService,
|
||||||
|
}
|
||||||
|
runGinServer(ctx, wg, config, store, taskDist, tokenMaker, services)
|
||||||
{{ else }}
|
{{ else }}
|
||||||
// Assuming you want Gateway AND gRPC when gRPC is selected
|
runGatewayServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService, orgService)
|
||||||
runGatewayServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService)
|
runGrpcServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService, orgService)
|
||||||
runGrpcServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService)
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
runTaskProcessor(ctx, wg, redisOpt, store, config)
|
runTaskProcessor(ctx, wg, redisOpt, store, config)
|
||||||
@@ -133,8 +136,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTaskProcessor(ctx context.Context, wg *errgroup.Group, redisOpt asynq.RedisClientOpt, store db.Store, config config.Config) {
|
func runTaskProcessor(
|
||||||
mailer := mailer.NewGmailSender(config.EmailSenderName, config.EmailSender, config.EmailPassword)
|
ctx context.Context,
|
||||||
|
wg *errgroup.Group,
|
||||||
|
redisOpt asynq.RedisClientOpt,
|
||||||
|
store db.Store,
|
||||||
|
config config.Config,
|
||||||
|
) {
|
||||||
|
mailer := mailer.NewSMTPSender(
|
||||||
|
config.EmailSenderName,
|
||||||
|
config.EmailSender,
|
||||||
|
config.EmailPassword,
|
||||||
|
config.SMTPAddr,
|
||||||
|
)
|
||||||
taskProcessor := worker.NewRedisTaskProcessor(redisOpt, store, mailer, config)
|
taskProcessor := worker.NewRedisTaskProcessor(redisOpt, store, mailer, config)
|
||||||
log.Info().Msg("start task processor")
|
log.Info().Msg("start task processor")
|
||||||
if err := taskProcessor.Start(); err != nil {
|
if err := taskProcessor.Start(); err != nil {
|
||||||
@@ -164,8 +178,16 @@ func runDBMigration(migrationURL, dbSource string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{{ if not .UseGRPC }}
|
{{ if not .UseGRPC }}
|
||||||
func runGinServer(ctx context.Context, wg *errgroup.Group, config config.Config, store db.Store, td worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) {
|
func runGinServer(
|
||||||
s, err := api.NewServer(config, store, td, tokenMaker, userService, authService)
|
ctx context.Context,
|
||||||
|
wg *errgroup.Group,
|
||||||
|
config config.Config,
|
||||||
|
store db.Store,
|
||||||
|
td worker.TaskDistributor,
|
||||||
|
tokenMaker auth.Maker,
|
||||||
|
services api.Services,
|
||||||
|
) {
|
||||||
|
s, err := api.NewServer(config, store, td, tokenMaker, services)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("cannot create server")
|
log.Fatal().Err(err).Msg("cannot create server")
|
||||||
}
|
}
|
||||||
@@ -194,8 +216,18 @@ func runGinServer(ctx context.Context, wg *errgroup.Group, config config.Config,
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ if .UseGRPC }}
|
{{ if .UseGRPC }}
|
||||||
func runGrpcServer(ctx context.Context, wg *errgroup.Group, config config.Config, store db.Store, taskDistributor worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) {
|
func runGrpcServer(
|
||||||
server, err := gapi.NewServer(config, store, taskDistributor, tokenMaker, userService, authService)
|
ctx context.Context,
|
||||||
|
wg *errgroup.Group,
|
||||||
|
config config.Config,
|
||||||
|
store db.Store,
|
||||||
|
td worker.TaskDistributor,
|
||||||
|
tokenMaker auth.Maker,
|
||||||
|
userService service.UserService,
|
||||||
|
authService service.AuthService,
|
||||||
|
organizationService service.OrganizationService,
|
||||||
|
) {
|
||||||
|
server, err := gapi.NewServer(config, store, td, tokenMaker, userService, authService, organizationService)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("cannot create server:")
|
log.Fatal().Err(err).Msg("cannot create server:")
|
||||||
}
|
}
|
||||||
@@ -203,7 +235,7 @@ func runGrpcServer(ctx context.Context, wg *errgroup.Group, config config.Config
|
|||||||
grpcLogger := grpc.UnaryInterceptor(gapi.GrpcLogger)
|
grpcLogger := grpc.UnaryInterceptor(gapi.GrpcLogger)
|
||||||
|
|
||||||
grpcServer := grpc.NewServer(grpcLogger)
|
grpcServer := grpc.NewServer(grpcLogger)
|
||||||
pb.RegisterSimpleBankServer(grpcServer, server)
|
pb.Register{{ .ServiceName }}Server(grpcServer, server)
|
||||||
reflection.Register(grpcServer)
|
reflection.Register(grpcServer)
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", config.GrpcServerAddr)
|
listener, err := net.Listen("tcp", config.GrpcServerAddr)
|
||||||
@@ -233,8 +265,18 @@ func runGrpcServer(ctx context.Context, wg *errgroup.Group, config config.Config
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Config, store db.Store, taskDistributor worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) {
|
func runGatewayServer(
|
||||||
server, err := gapi.NewServer(config, store, taskDistributor, tokenMaker, userService, authService)
|
ctx context.Context,
|
||||||
|
wg *errgroup.Group,
|
||||||
|
config config.Config,
|
||||||
|
store db.Store,
|
||||||
|
td worker.TaskDistributor,
|
||||||
|
tokenMaker auth.Maker,
|
||||||
|
userService service.UserService,
|
||||||
|
authService service.AuthService,
|
||||||
|
organizationService service.OrganizationService,
|
||||||
|
) {
|
||||||
|
server, err := gapi.NewServer(config, store, td, tokenMaker, userService, authService, organizationService)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("cannot create server:")
|
log.Fatal().Err(err).Msg("cannot create server:")
|
||||||
}
|
}
|
||||||
@@ -250,7 +292,7 @@ func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Con
|
|||||||
|
|
||||||
grpcMux := runtime.NewServeMux(opts)
|
grpcMux := runtime.NewServeMux(opts)
|
||||||
|
|
||||||
err = pb.RegisterSimpleBankHandlerServer(ctx, grpcMux, server)
|
err = pb.Register{{ .ServiceName }}HandlerServer(ctx, grpcMux, server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("cannot register handler server")
|
log.Fatal().Err(err).Msg("cannot register handler server")
|
||||||
}
|
}
|
||||||
@@ -318,6 +360,5 @@ func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Con
|
|||||||
log.Info().Msg("HTTP gateway server shut down")
|
log.Info().Msg("HTTP gateway server shut down")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
Reference in new issue
Block a user