Skip to content

Commit

Permalink
feat: settings, default logging and go-core support (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsalles authored Nov 18, 2021
1 parent 4f72111 commit 8c60cfe
Show file tree
Hide file tree
Showing 15 changed files with 849 additions and 340 deletions.
32 changes: 7 additions & 25 deletions cmd/server/application_configuration.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
package main

import (
"github.com/sirupsen/logrus"
"github.com/ydataai/aws-quota-provider/pkg/common"
"github.com/kelseyhightower/envconfig"
)

type configuration struct {
logLevel logrus.Level
awsRegion string
// Configuration defines all env vars required for the application
type Configuration struct {
AWSRegion string `envconfig:"REGION" required:"true"`
}

func (c *configuration) LoadEnvVars() error {
logLevel, err := common.VariableFromEnvironment("LOG_LEVEL")
if err != nil {
return err
}

awsRegion, err := common.VariableFromEnvironment("REGION")
if err != nil {
return err
}

level, err := logrus.ParseLevel(logLevel)
if err != nil {
return err
}

c.logLevel = level
c.awsRegion = awsRegion

return nil
// LoadEnvVars reads all env vars required for the server package
func (c *Configuration) LoadFromEnvVars() error {
return envconfig.Process("", c)
}
56 changes: 25 additions & 31 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,64 @@ import (
"fmt"
"os"

"github.com/ydataai/go-core/pkg/common/config"
"github.com/ydataai/go-core/pkg/common/logging"
"github.com/ydataai/go-core/pkg/common/server"

"github.com/ydataai/aws-quota-provider/pkg/clients"
"github.com/ydataai/aws-quota-provider/pkg/common"
"github.com/ydataai/aws-quota-provider/pkg/controller"
"github.com/ydataai/aws-quota-provider/pkg/server"
"github.com/ydataai/aws-quota-provider/pkg/service"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/servicequotas"
"github.com/sirupsen/logrus"
)

var (
errChan chan error
)

func main() {
restServiceConfiguration := service.RESTServiceConfiguration{}
serverConfiguration := server.Configuration{}
restControllerConfiguration := controller.RESTControllerConfiguration{}
applicationConfiguration := configuration{}
serverConfiguration := server.HTTPServerConfiguration{}
restControllerConfiguration := config.RESTControllerConfiguration{}
applicationConfiguration := Configuration{}
loggerConfiguration := logging.LoggerConfiguration{}

err := initConfigurationurationVariables([]common.ConfigurationVariables{
if err := config.InitConfigurationVariables([]config.ConfigurationVariables{
&restServiceConfiguration,
&serverConfiguration,
&restControllerConfiguration,
&applicationConfiguration,
})
if err != nil {
}); err != nil {
fmt.Println(fmt.Errorf("could not set configuration variables. Err: %v", err))
os.Exit(1)
}

var log = logrus.New()
log.SetLevel(applicationConfiguration.logLevel)
logger := logging.NewLogger(loggerConfiguration)

sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(applicationConfiguration.awsRegion),
Region: aws.String(applicationConfiguration.AWSRegion),
}))

ec2Service := ec2.New(sess)
serviceQuotaService := servicequotas.New(sess)

ec2Client := clients.NewEC2Client(log, ec2Service)
serviceQuotaClient := clients.NewServiceQuotaClient(log, serviceQuotaService)
ec2Client := clients.NewEC2Client(logger, ec2Service)
serviceQuotaClient := clients.NewServiceQuotaClient(logger, serviceQuotaService)

restService := service.NewRESTService(log, ec2Client, serviceQuotaClient, restServiceConfiguration)
restController := controller.NewRESTController(log, restService, restControllerConfiguration)
restService := service.NewRESTService(logger, ec2Client, serviceQuotaClient, restServiceConfiguration)
restController := controller.NewRESTController(logger, restService, restControllerConfiguration)

serverCtx := context.Background()

s := server.NewServer(log, serverConfiguration)
restController.Boot(s)
httpServer := server.NewServer(logger, serverConfiguration)
restController.Boot(httpServer)

s.Run(serverCtx)
httpServer.Run(serverCtx)

for err := range s.ErrCh {
log.Error(err)
for err := range errChan {
logger.Error(err)
}

}
func initConfigurationurationVariables(configurations []common.ConfigurationVariables) error {
for _, configuration := range configurations {
if err := configuration.LoadEnvVars(); err != nil {
return err
}
}

return nil
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ go 1.15

require (
github.com/aws/aws-sdk-go v1.37.28
github.com/gin-gonic/gin v1.6.3
github.com/gin-gonic/gin v1.7.4
github.com/golang/mock v1.5.0
github.com/google/go-cmp v0.5.5
github.com/google/uuid v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/google/uuid v1.2.0 // indirect
github.com/kelseyhightower/envconfig v1.4.0
github.com/ydataai/go-core v0.1.0
)
Loading

0 comments on commit 8c60cfe

Please sign in to comment.