Skip to content

Commit

Permalink
Merge pull request #101 from isaqueveras/100-choose-which-servers-to-…
Browse files Browse the repository at this point in the history
…start-http-grpc

🚀 choose which servers to start (http, gRPC)
  • Loading branch information
isaqueveras committed Jan 30, 2023
2 parents 8da60c2 + 17d2f72 commit 332a94d
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cmd/cmd

# Binaries for programs and plugins
*.exe
*.exe~
Expand All @@ -18,4 +16,4 @@ vendor/
pgdata/
.vscode

__debug_bin
__debug_bin
2 changes: 2 additions & 0 deletions config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ server:
WriteTimeout: 5
CSRF: true
Debug: false
StartHTTP: true
StartGRPC: true
AccessLogDirectory: /var/log/power-sso/access.log
ErrorLogDirectory: /var/log/power-sso/error.log
PermissionBase: github.com/isaqueveras/power-sso
Expand Down
20 changes: 16 additions & 4 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

const (
// ModeDevelopment represents the development environment mode
ModeDevelopment string = "development"
// ModeDevelopment represents the production environment mode
ModeProduction string = "production"
// modeDevelopment represents the development environment mode
modeDevelopment string = "development"
// modeDevelopment represents the production environment mode
modeProduction string = "production"
)

const (
Expand Down Expand Up @@ -56,6 +56,8 @@ type (
SSL bool
CSRF bool
Debug bool
StartHTTP bool
StartGRPC bool
CtxDefaultTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
Expand Down Expand Up @@ -115,3 +117,13 @@ type (
Duration int64
}
)

// IsModeDevelopment returns if in development mode
func (sc *ServerConfig) IsModeDevelopment() bool {
return sc.Mode == modeDevelopment
}

// IsModeProduction returns if in production mode
func (sc *ServerConfig) IsModeProduction() bool {
return sc.Mode == modeProduction
}
2 changes: 1 addition & 1 deletion internal/application/auth/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (rr *RegisterRequest) GeneratePassword() error {
rr.RefreshTokenKey()

cost := auth.CostHashPasswordDevelopment
if config.Get().Server.Mode == config.ModeProduction {
if config.Get().Server.IsModeProduction() {
cost = auth.CostHashPasswordProduction
}

Expand Down
8 changes: 5 additions & 3 deletions internal/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
gogrpc "google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/isaqueveras/power-sso/config"
"github.com/isaqueveras/power-sso/internal/interface/grpc/auth"
"github.com/isaqueveras/power-sso/internal/middleware"
"github.com/isaqueveras/power-sso/pkg/logger"
"github.com/isaqueveras/power-sso/pkg/oops"
)

func (s *Server) ServerGRPC() (err error) {
defer s.logg.Info("Server GRPC is running")
if !s.cfg.Server.StartGRPC {
return
}

var (
listen net.Listener
Expand All @@ -40,7 +41,8 @@ func (s *Server) ServerGRPC() (err error) {
)
)

if s.cfg.Server.Mode == config.ModeDevelopment {
s.logg.Info("Server GRPC is running")
if s.cfg.Server.IsModeDevelopment() {
s.logg.Debug("RUNNING IN DEVELOPMENT: REFLECTION ON")
reflection.Register(serverGRPC)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ import (
"github.com/isaqueveras/endless"
gopowersso "github.com/isaqueveras/go-powersso"

"github.com/isaqueveras/power-sso/config"
"github.com/isaqueveras/power-sso/internal/interface/http/auth"
"github.com/isaqueveras/power-sso/internal/interface/project"
"github.com/isaqueveras/power-sso/internal/middleware"
"github.com/isaqueveras/power-sso/pkg/i18n"
)

func (s *Server) ServerHTTP() (err error) {
defer s.logg.Info("Server HTTP is running")
if !s.cfg.Server.StartHTTP {
return
}

if s.cfg.Server.Mode == config.ModeProduction {
s.logg.Info("Server HTTP is running")
if s.cfg.Server.IsModeProduction() {
gin.SetMode(gin.ReleaseMode)
}

Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func main() {
server = server.NewServer(cfg, logg, group)
)

// TODO: add in the configuration if it is to run http server
if err = server.ServerHTTP(); err != nil {
logg.Fatal("Error while serving the server HTTP: ", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewLogger(cfg *config.Config) *Logger {
func (l *Logger) InitLogger() {
var cfg zap.Config

if l.cfg.Server.Mode != config.ModeDevelopment {
if !l.cfg.Server.IsModeDevelopment() {
cfg = zap.NewProductionConfig()
cfg.DisableStacktrace = true
cfg.EncoderConfig = zap.NewProductionEncoderConfig()
Expand Down
Binary file removed power-sso
Binary file not shown.

1 comment on commit 332a94d

@vercel
Copy link

@vercel vercel bot commented on 332a94d Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

power-sso – ./

power-sso-git-main-isaqueveras.vercel.app
power-sso.vercel.app
power-sso-isaqueveras.vercel.app

Please sign in to comment.