Skip to content

Commit

Permalink
made it so that it tries to connect with ssl and non ssl when connect…
Browse files Browse the repository at this point in the history
…ing to a postgres database
  • Loading branch information
akeemphilbert committed Apr 18, 2024
1 parent 54d4173 commit f222f11
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions controllers/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

var InvalidAWSDriver = errors.New("invalid aws driver specified, must be postgres or mysql")

//RESTAPI is used to manage the API
// RESTAPI is used to manage the API
type RESTAPI struct {
Application model.Service
Log model.Log
Expand Down Expand Up @@ -72,7 +72,7 @@ type RESTAPI struct {
defaultProjection model.Projection
}

//define an interface that all plugins must implement
// define an interface that all plugins must implement
type APIInterface interface {
AddPathConfig(path string, config *PathConfig) error
AddConfig(config *APIConfig) error
Expand All @@ -81,14 +81,14 @@ type APIInterface interface {
SetEchoInstance(e *echo.Echo)
}

//Deprecated: 02/13/2022 made Config public
// Deprecated: 02/13/2022 made Config public
func (p *RESTAPI) AddConfig(config *APIConfig) error {
p.Config = config
return nil
}

//Deprecated: 02/13/2022 This should not but actively used
//AddPathConfig add path Config
// Deprecated: 02/13/2022 This should not but actively used
// AddPathConfig add path Config
func (p *RESTAPI) AddPathConfig(path string, config *PathConfig) error {
if p.PathConfigs == nil {
p.PathConfigs = make(map[string]*PathConfig)
Expand All @@ -105,23 +105,23 @@ func (p *RESTAPI) SetEchoInstance(e *echo.Echo) {
p.e = e
}

//RegisterMiddleware Add middleware so that it can be referenced in the OpenAPI spec
// RegisterMiddleware Add middleware so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterMiddleware(name string, middleware Middleware) {
if p.middlewares == nil {
p.middlewares = make(map[string]Middleware)
}
p.middlewares[name] = middleware
}

//RegisterController Add controller so that it can be referenced in the OpenAPI spec
// RegisterController Add controller so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterController(name string, controller Controller) {
if p.controllers == nil {
p.controllers = make(map[string]Controller)
}
p.controllers[name] = controller
}

//RegisterEventStore Add event store so that it can be referenced in the OpenAPI spec
// RegisterEventStore Add event store so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterEventStore(name string, repository model.EventRepository) {
p.defaultEventStore = repository
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (p *RESTAPI) RegisterInitializer(key string, initializer GlobalInitializer)
}
}

//RegisterOperationInitializer add operation initializer if it's not already there
// RegisterOperationInitializer add operation initializer if it's not already there
func (p *RESTAPI) RegisterOperationInitializer(initializer OperationInitializer) {
if p.registeredInitializers == nil {
p.registeredInitializers = make(map[string]int)
Expand All @@ -166,7 +166,7 @@ func (p *RESTAPI) RegisterOperationInitializer(initializer OperationInitializer)

}

//RegisterPrePathInitializer add path initializer that runs BEFORE operation initializers if it's not already there
// RegisterPrePathInitializer add path initializer that runs BEFORE operation initializers if it's not already there
func (p *RESTAPI) RegisterPrePathInitializer(initializer PathInitializer) {
if p.registeredPrePathInitializers == nil {
p.registeredPrePathInitializers = make(map[reflect.Value]int)
Expand All @@ -180,7 +180,7 @@ func (p *RESTAPI) RegisterPrePathInitializer(initializer PathInitializer) {

}

//RegisterPostPathInitializer add path initializer that runs AFTER operation initializers if it's not already there
// RegisterPostPathInitializer add path initializer that runs AFTER operation initializers if it's not already there
func (p *RESTAPI) RegisterPostPathInitializer(initializer PathInitializer) {
if p.registeredPostPathInitializers == nil {
p.registeredPostPathInitializers = make(map[reflect.Value]int)
Expand All @@ -194,36 +194,36 @@ func (p *RESTAPI) RegisterPostPathInitializer(initializer PathInitializer) {

}

//RegisterCommandDispatcher Add command dispatcher so that it can be referenced in the OpenAPI spec
// RegisterCommandDispatcher Add command dispatcher so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterCommandDispatcher(name string, dispatcher model.CommandDispatcher) {
if p.commandDispatchers == nil {
p.commandDispatchers = make(map[string]model.CommandDispatcher)
}
p.commandDispatchers[name] = dispatcher
}

//RegisterProjection Add command dispatcher so that it can be referenced in the OpenAPI spec
// RegisterProjection Add command dispatcher so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterProjection(name string, projection model.Projection) {
p.defaultProjection = projection
}

//RegisterEntityFactory Adds entity factory so that it can be referenced in the OpenAPI spec
// RegisterEntityFactory Adds entity factory so that it can be referenced in the OpenAPI spec
func (p *RESTAPI) RegisterEntityFactory(name string, factory model.EntityFactory) {
if p.entityFactories == nil {
p.entityFactories = make(map[string]model.EntityFactory)
}
p.entityFactories[name] = factory
}

//RegisterDBConnection save db connection
// RegisterDBConnection save db connection
func (p *RESTAPI) RegisterDBConnection(name string, connection *sql.DB) {
if p.dbConnections == nil {
p.dbConnections = make(map[string]*sql.DB)
}
p.dbConnections[name] = connection
}

//RegisterGORMDB save gorm connection
// RegisterGORMDB save gorm connection
func (p *RESTAPI) RegisterGORMDB(name string, connection *gorm.DB) {
p.gormConnection = connection
}
Expand All @@ -242,7 +242,7 @@ func (p *RESTAPI) GetPermissionEnforcer(name string) (*casbin.Enforcer, error) {
return nil, fmt.Errorf("permission enforcer '%s' not found", name)
}

//GetMiddleware get middleware by name
// GetMiddleware get middleware by name
func (p *RESTAPI) GetMiddleware(name string) (Middleware, error) {
if tmiddleware, ok := p.middlewares[name]; ok {
return tmiddleware, nil
Expand All @@ -259,7 +259,7 @@ func (p *RESTAPI) GetMiddleware(name string) (Middleware, error) {
return nil, fmt.Errorf("middleware '%s' not found", name)
}

//GetController get controller by name
// GetController get controller by name
func (p *RESTAPI) GetController(name string) (Controller, error) {
if tcontroller, ok := p.controllers[name]; ok {
return tcontroller, nil
Expand All @@ -276,20 +276,20 @@ func (p *RESTAPI) GetController(name string) (Controller, error) {
return nil, fmt.Errorf("controller '%s' not found", name)
}

//GetEventStore get event dispatcher by name
// GetEventStore get event dispatcher by name
func (p *RESTAPI) GetEventStore(name string) (model.EventRepository, error) {
return p.defaultEventStore, nil
}

//GetCommandDispatcher get event dispatcher by name
// GetCommandDispatcher get event dispatcher by name
func (p *RESTAPI) GetCommandDispatcher(name string) (model.CommandDispatcher, error) {
if tdispatcher, ok := p.commandDispatchers[name]; ok {
return tdispatcher, nil
}
return nil, fmt.Errorf("command disptacher '%s' not found", name)
}

//GetProjection get event dispatcher by name
// GetProjection get event dispatcher by name
func (p *RESTAPI) GetProjection(name string) (model.Projection, error) {
return p.defaultProjection, nil
}
Expand All @@ -308,22 +308,22 @@ func (p *RESTAPI) GetEntityRepository(name string) (model.EntityRepository, erro
return nil, fmt.Errorf("entity repository '%s' not found", name)
}

//GetGlobalInitializers get global intializers in the order they were registered
// GetGlobalInitializers get global intializers in the order they were registered
func (p *RESTAPI) GetGlobalInitializers() []GlobalInitializer {
return p.globalInitializers
}

//GetOperationInitializers get operation intializers in the order they were registered
// GetOperationInitializers get operation intializers in the order they were registered
func (p *RESTAPI) GetOperationInitializers() []OperationInitializer {
return p.operationInitializers
}

//GetPrePathInitializers get path intializers in the order they were registered that run BEFORE the operations are processed
// GetPrePathInitializers get path intializers in the order they were registered that run BEFORE the operations are processed
func (p *RESTAPI) GetPrePathInitializers() []PathInitializer {
return p.prePathInitializers
}

//GetPostPathInitializers get path intializers in the order they were registered that run AFTER the operations are processed
// GetPostPathInitializers get path intializers in the order they were registered that run AFTER the operations are processed
func (p *RESTAPI) GetPostPathInitializers() []PathInitializer {
return p.postPathInitializers
}
Expand All @@ -336,7 +336,7 @@ func (p *RESTAPI) GetSchemas() (map[string]interface{}, error) {
return schemes, nil
}

//GetEntityFactories get event factories
// GetEntityFactories get event factories
func (p *RESTAPI) GetEntityFactories() map[string]model.EntityFactory {
return p.entityFactories
}
Expand All @@ -348,15 +348,15 @@ func (p *RESTAPI) GetEntityFactory(name string) (model.EntityFactory, error) {
return nil, fmt.Errorf("entity factory '%s' not found", name)
}

//GetDBConnection get db connection by name
// GetDBConnection get db connection by name
func (p *RESTAPI) GetDBConnection(name string) (*sql.DB, error) {
if tconnection, ok := p.dbConnections[name]; ok {
return tconnection, nil
}
return nil, fmt.Errorf("database connection '%s' not found", name)
}

//GetGormDBConnection get gorm connection by name
// GetGormDBConnection get gorm connection by name
func (p *RESTAPI) GetGormDBConnection(name string) (*gorm.DB, error) {
return p.gormConnection, nil
}
Expand All @@ -369,7 +369,7 @@ func (p *RESTAPI) GetWeOSConfig() *APIConfig {
return p.Config
}

//RegisterLog setup a log
// RegisterLog setup a log
func (p *RESTAPI) RegisterLog(name string, logger model.Log) {
if p.logs == nil {
p.logs = make(map[string]model.Log)
Expand Down Expand Up @@ -409,7 +409,7 @@ func (p *RESTAPI) GetSecurityConfiguration() *SecurityConfiguration {
const SWAGGERUIENDPOINT = "/_discover/"
const SWAGGERJSONENDPOINT = "/_discover_json"

//RegisterSwaggerAPI creates default swagger api from binary
// RegisterSwaggerAPI creates default swagger api from binary
func (p *RESTAPI) RegisterDefaultSwaggerAPI(pathMiddleware []echo.MiddlewareFunc) error {
statikFS, err := fs.New()
if err != nil {
Expand All @@ -423,15 +423,15 @@ func (p *RESTAPI) RegisterDefaultSwaggerAPI(pathMiddleware []echo.MiddlewareFunc
return nil
}

//RegisterDefaultSwaggerJson registers a default swagger json response
// RegisterDefaultSwaggerJson registers a default swagger json response
func (p *RESTAPI) RegisterDefaultSwaggerJSON(pathMiddleware []echo.MiddlewareFunc) error {
p.e.GET(p.Config.BasePath+SWAGGERJSONENDPOINT, func(c echo.Context) error {
return c.JSON(http.StatusOK, p.Swagger)
}, pathMiddleware...)
return nil
}

//Initialize and setup configurations for RESTAPI
// Initialize and setup configurations for RESTAPI
func (p *RESTAPI) Initialize(ctxt context.Context) error {
//register logger
p.RegisterLog("Default", p.e.Logger)
Expand Down Expand Up @@ -563,7 +563,7 @@ func (p *RESTAPI) Initialize(ctxt context.Context) error {
return err
}

//SQLConnectionFromConfig get db connection based on a Config
// SQLConnectionFromConfig get db connection based on a Config
func (p *RESTAPI) SQLConnectionFromConfig(config *model.DBConfig) (*sql.DB, *gorm.DB, string, error) {
var connStr string
var err error
Expand Down Expand Up @@ -637,7 +637,7 @@ func (p *RESTAPI) SQLConnectionFromConfig(config *model.DBConfig) (*sql.DB, *gor
connStr = fmt.Sprintf("tcp://%s:%s?username=%s&password=%s&database=%s",
config.Host, strconv.Itoa(config.Port), config.User, config.Password, config.Database)
case "postgres":
connStr = fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
connStr = fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=prefer",
config.Host, strconv.Itoa(config.Port), config.User, config.Password, config.Database)
default:
return nil, nil, connStr, errors.New(fmt.Sprintf("db driver '%s' is not supported ", config.Driver))
Expand Down

0 comments on commit f222f11

Please sign in to comment.