Skip to content

Commit

Permalink
#11 Added config provider & loading a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Dec 24, 2023
1 parent 3625a4a commit ae80099
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ install-checkers: ## Install static checkers
@GOBIN=$(CHECKER_BIN) go install github.com/securego/gosec/v2/cmd/gosec@latest

lint: install-checkers ## Lint the source code
@echo "🧹 Cleaning go.mod.."
@go mod tidy
@echo "🧹 Formatting files.."
@go fmt ./...
@$(CHECKER_BIN)/gofumpt -l -w .
@echo "🧹 Vetting go.mod.."
@go vet ./...
@echo "🧹 Cleaning go.mod.."
@go mod tidy


static-checks: install-checkers ## Static Analysis
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/nyaruka/phonenumbers v1.0.55 h1:bj0nTO88Y68KeUQ/n3Lo2KgK7lM1hF7L9NFuwcCl3yg=
Expand Down Expand Up @@ -116,6 +120,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
16 changes: 14 additions & 2 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ package cmd

import (
"glide/pkg"
"glide/pkg/config"

"github.com/spf13/cobra"
)

var cfgFile string

// NewCLI Create a Glide CLI
func NewCLI() *cobra.Command {
// TODO: Chances are we could use the build in flags module in this is all we need from CLI
cli := &cobra.Command{
Use: "",
Use: "glide",
Short: "🐦Glide is an open source lightweight high-performant model gateway",
Long: "TODO",
Version: pkg.GetVersion(),
RunE: func(cmd *cobra.Command, args []string) error {
gateway, err := pkg.NewGateway()
configProvider, err := config.NewProvider().Load(cfgFile)
if err != nil {
return err
}

gateway, err := pkg.NewGateway(configProvider)
if err != nil {
return err
}
Expand All @@ -23,5 +33,7 @@ func NewCLI() *cobra.Command {
// SilenceUsage: true,
}

cli.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")

return cli
}
45 changes: 45 additions & 0 deletions pkg/config/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package config

import (
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
)

// Provider reads, collects, validates and process config files
type Provider struct {
Config *Config
}

// NewProvider creates a instance of Config Provider
func NewProvider() *Provider {
return &Provider{
Config: nil,
}
}

func (p *Provider) Load(configPath string) (*Provider, error) {
rawConfig, err := os.ReadFile(filepath.Clean(configPath))
if err != nil {
return p, fmt.Errorf("unable to read the file %v: %w", configPath, err)
}

var cfg *Config

if err := yaml.Unmarshal(rawConfig, &cfg); err != nil {
return p, fmt.Errorf("unable to serialize the file %v: %w", configPath, err)
}

p.Config = cfg

return p, nil
}

func (p *Provider) Get() *Config {
return p.Config
}

func (p *Provider) Start() {
}
26 changes: 14 additions & 12 deletions pkg/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os/signal"
"syscall"

"glide/pkg/config"

"glide/pkg/pools"

"glide/pkg/telemetry"
Expand All @@ -21,6 +23,8 @@ import (
// Gateway represents an instance of running Glide gateway.
// It loads configs, start API server(s), and listen to termination signals to shut down
type Gateway struct {
// configProvider holds all configurations
configProvider *config.Provider
// telemetry holds logger, meter, and tracer
telemetry *telemetry.Telemetry
// serverManager controls API over different protocols
Expand All @@ -31,13 +35,10 @@ type Gateway struct {
shutdownC chan struct{}
}

func NewGateway() (*Gateway, error) {
// TODO: gonna be read from a config file
logConfig := telemetry.NewLogConfig()
logConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
logConfig.Encoding = "console"
func NewGateway(configProvider *config.Provider) (*Gateway, error) {
cfg := configProvider.Get()

tel, err := telemetry.NewTelemetry(&telemetry.Config{LogConfig: logConfig})
tel, err := telemetry.NewTelemetry(&telemetry.Config{LogConfig: cfg.Telemetry.LogConfig})
if err != nil {
return nil, err
}
Expand All @@ -53,16 +54,17 @@ func NewGateway() (*Gateway, error) {
}

return &Gateway{
telemetry: tel,
serverManager: serverManager,
signalC: make(chan os.Signal, 3), // equal to number of signal types we expect to receive
shutdownC: make(chan struct{}),
configProvider: configProvider,
telemetry: tel,
serverManager: serverManager,
signalC: make(chan os.Signal, 3), // equal to number of signal types we expect to receive
shutdownC: make(chan struct{}),
}, nil
}

// Run starts and runs the gateway according to given configuration
func (gw *Gateway) Run(ctx context.Context) error {
// TODO: init configs
gw.configProvider.Start()
gw.serverManager.Start()

signal.Notify(gw.signalC, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
Expand All @@ -73,7 +75,7 @@ LOOP:
select {
// TODO: Watch for config updates
case sig := <-gw.signalC:
gw.telemetry.Logger.Info("Received signal from OS", zap.String("signal", sig.String()))
gw.telemetry.Logger.Info("received signal from os", zap.String("signal", sig.String()))
break LOOP
case <-gw.shutdownC:
gw.telemetry.Logger.Info("received shutdown request")
Expand Down

0 comments on commit ae80099

Please sign in to comment.