Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

badaas as library #61

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
dc408d6
update mockery version
FrancoLiberali Jul 27, 2023
e2c0776
fix typos in Authentication, resource and constructor
FrancoLiberali Jul 27, 2023
83e1446
fix route for auth unit tests
FrancoLiberali Jul 27, 2023
33e371c
remove error that is always nil
FrancoLiberali Jul 27, 2023
03c876c
add TODO to the changes to be done in the session service
FrancoLiberali Jul 27, 2023
960f598
add error management to middlewareJSON
FrancoLiberali Jul 27, 2023
1f7761f
add CommandInitializer to init configuration keys
FrancoLiberali Jul 27, 2023
28e3ecb
init database configuration keys
FrancoLiberali Jul 27, 2023
63de335
init session configuration keys
FrancoLiberali Jul 27, 2023
807e598
init initialization configuration keys
FrancoLiberali Jul 27, 2023
2de14ef
init server configuration keys
FrancoLiberali Jul 27, 2023
2dc5907
init logger configuration keys
FrancoLiberali Jul 27, 2023
cbb6f5c
remove unused r.md file in commands
FrancoLiberali Jul 27, 2023
d165eec
move time to utils
FrancoLiberali Jul 27, 2023
b80f2e4
create super user when adding the auth controller
FrancoLiberali Jul 27, 2023
88db618
update info controller and routes creation
FrancoLiberali Jul 27, 2023
1730a33
change the way badaas server is created
FrancoLiberali Jul 27, 2023
25f8e57
move test e2e to test_e2e/ and docker to docker/
FrancoLiberali Jul 28, 2023
f1bed03
do not run auto migration on test e2e execution + automigration refactor
FrancoLiberali Jul 28, 2023
f523952
update docs and developers utils
FrancoLiberali Jul 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add CommandInitializer to init configuration keys
  • Loading branch information
FrancoLiberali committed Aug 1, 2023
commit 1f7761ff28d87678516e57c9226a58c40bba3a4f
54 changes: 51 additions & 3 deletions badaas.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
// Package main :
package main

import (
"github.com/ditrit/badaas/commands"
"net/http"

"github.com/spf13/cobra"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"

"github.com/ditrit/badaas/configuration"
"github.com/ditrit/badaas/controllers"
"github.com/ditrit/badaas/logger"
"github.com/ditrit/badaas/persistence"
"github.com/ditrit/badaas/router"
"github.com/ditrit/badaas/services/sessionservice"
"github.com/ditrit/badaas/services/userservice"
"github.com/ditrit/verdeter"
)

// Badaas application, run a http-server on 8000.
func main() {
commands.Execute()
rootCommand := verdeter.BuildVerdeterCommand(verdeter.VerdeterConfig{
Use: "badaas",
Short: "BaDaaS",
Run: runHTTPServer,
})

err := configuration.NewCommandInitializer(configuration.NewKeySetter()).Init(rootCommand)
if err != nil {
panic(err)
}

rootCommand.Execute()
}

// Run the http server for badaas
func runHTTPServer(cmd *cobra.Command, args []string) {
fx.New(
// Modules
configuration.ConfigurationModule,
router.RouterModule,
controllers.ControllerModule,
logger.LoggerModule,
persistence.PersistanceModule,

fx.Provide(userservice.NewUserService),
fx.Provide(sessionservice.NewSessionService),
// logger for fx
fx.WithLogger(func(logger *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: logger}
}),

fx.Provide(newHTTPServer),

// Finally: we invoke the newly created server
fx.Invoke(func(*http.Server) { /* we need this function to be empty*/ }),
).Run()
}
70 changes: 0 additions & 70 deletions commands/rootCmd.go

This file was deleted.

46 changes: 46 additions & 0 deletions configuration/CommandInitializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package configuration

import (
"github.com/ditrit/verdeter"
)

type CommandInitializer interface {
// Inits VerdeterCommand "command" with the all the keys that are configurable in badaas
Init(command *verdeter.VerdeterCommand) error
}

// Implementation of the CommandInitializer
type commandInitializerImpl struct {
KeySetter KeySetter
Keys []KeyDefinition
}

// Constructor of CommandInitializer with the keys for badaas
// it uses the keySetter to set the configuration keys in the VerdeterCommand
func NewCommandInitializer(keySetter KeySetter) CommandInitializer {
keys := []KeyDefinition{
{
Name: "config_path",
ValType: verdeter.IsStr,
Usage: "Path to the config file/directory",
DefaultV: ".",
},
}

return commandInitializerImpl{
KeySetter: keySetter,
Keys: keys,
}
}

// Inits VerdeterCommand "cmd" with the all the keys in the Keys of the initializer
func (initializer commandInitializerImpl) Init(command *verdeter.VerdeterCommand) error {
for _, key := range initializer.Keys {
err := initializer.KeySetter.Set(command, key)
if err != nil {
return err
}
}

return nil
}
39 changes: 39 additions & 0 deletions configuration/CommandInitializer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package configuration_test

import (
"errors"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/ditrit/badaas/configuration"
configurationMocks "github.com/ditrit/badaas/mocks/configuration"
"github.com/ditrit/verdeter"
)

var rootCommand = verdeter.BuildVerdeterCommand(verdeter.VerdeterConfig{
Use: "badaas",
Short: "Backend and Distribution as a Service",
Run: doNothing,
})

func doNothing(_ *cobra.Command, _ []string) {}

func TestInitCommandsInitializerSetsAllKeysWithoutError(t *testing.T) {
err := configuration.NewCommandInitializer(
configuration.NewKeySetter(),
).Init(rootCommand)
assert.Nil(t, err)
}

func TestInitCommandsInitializerReturnsErrorWhenErrorOnKeySet(t *testing.T) {
mockKeySetter := configurationMocks.NewKeySetter(t)
mockKeySetter.On("Set", mock.Anything, mock.Anything).Return(errors.New("error setting key"))

commandInitializer := configuration.NewCommandInitializer(mockKeySetter)

err := commandInitializer.Init(rootCommand)
assert.ErrorContains(t, err, "error setting key")
}
47 changes: 47 additions & 0 deletions configuration/KeySetter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package configuration

import (
"github.com/ditrit/verdeter"
"github.com/ditrit/verdeter/models"
)

type KeySetter interface {
// Configures the VerdeterCommand "command" with the information contained in "key"
Set(command *verdeter.VerdeterCommand, key KeyDefinition) error
}

type KeyDefinition struct {
Name string
ValType models.ConfigType
Usage string
Required bool
DefaultV any
Validator *models.Validator
}

type keySetterImpl struct{}

func NewKeySetter() KeySetter {
return keySetterImpl{}
}

// Configures the VerdeterCommand "command" with the information contained in "key"
func (ks keySetterImpl) Set(command *verdeter.VerdeterCommand, key KeyDefinition) error {
if err := command.GKey(key.Name, key.ValType, "", key.Usage); err != nil {
return err
}

if key.Required {
command.SetRequired(key.Name)
}

if key.DefaultV != nil {
command.SetDefault(key.Name, key.DefaultV)
}

if key.Validator != nil {
command.AddValidator(key.Name, *key.Validator)
}

return nil
}
42 changes: 42 additions & 0 deletions mocks/configuration/CommandInitializer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions mocks/configuration/KeySetter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions commands/server.go → server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands
package main

// This file holds functions needed by the badaas rootCommand, thoses functions help in creating the http.Server.
// This file holds functions needed by the badaas rootCommand,
// those functions help in creating the http.Server.

import (
"context"
@@ -44,7 +45,7 @@ func addrFromConf(host string, port int) string {
return address
}

func NewHTTPServer(
func newHTTPServer(
lc fx.Lifecycle,
logger *zap.Logger,
router http.Handler,
Loading