Skip to content

Commit

Permalink
Merge pull request #1 from navidshariaty/main
Browse files Browse the repository at this point in the history
fix typo and configmap parse
  • Loading branch information
navidshariaty authored Jun 26, 2023
2 parents ca1f711 + efb67b6 commit 04eccee
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
.idea/

# Test binary, built with `go test -c`
*.test
Expand All @@ -15,7 +16,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Go workspace file
go.work
12 changes: 6 additions & 6 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func (cmd Server) Command(trap chan os.Signal) *cobra.Command {
}

func (cmd *Server) main(cfg *config.Config, trap chan os.Signal) {
logger := logger.NewZap(cfg.Logger)
loggerObj := logger.NewZap(cfg.Logger)

querier := querier.New(cfg.Querier, logger)
go querier.Start()
querierObj := querier.New(cfg.Querier, loggerObj)
go querierObj.Start()

server := http.New(logger, querier)
server := http.New(loggerObj, querierObj)
go server.Serve(8080)

// Keep this at the bottom of the main function
field := zap.String("signal trap", (<-trap).String())
logger.Info("exiting by receiving a unix signal", field)
loggerObj.Info("exiting by receiving a unix signal", field)

querier.Stop()
querierObj.Stop()
}
10 changes: 5 additions & 5 deletions internal/api/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
"github.com/snapp-incubator/snappcloud-status-backend/internal/models"
)

func (handler *Server) liveness(c *gin.Context) {
func (server *Server) liveness(c *gin.Context) {
c.Status(http.StatusOK)
}

func (handler *Server) readiness(c *gin.Context) {
func (server *Server) readiness(c *gin.Context) {
c.Status(http.StatusOK)
}

func (handler *Server) services(c *gin.Context) {
func (server *Server) services(c *gin.Context) {
c.JSON(http.StatusOK, &struct {
Message string `json:"message"`
Services []models.Service `json:"services,omitempty"`
}{
Message: "All services retrieved successfuly.",
Services: handler.querier.GetServices(),
Message: "All services retrieved successfully.",
Services: server.querier.GetServices(),
})
}
2 changes: 1 addition & 1 deletion internal/api/http/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gin-gonic/gin"
)

func (s *Server) corsMiddleware() gin.HandlerFunc {
func (server *Server) corsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
Expand Down
8 changes: 4 additions & 4 deletions internal/api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func New(log *zap.Logger, querier querier.Querier) *Server {
// expose metrics to prometheus server
server.engine.GET("/metrics", gin.WrapH(promhttp.Handler()))

// healthness endpoints
healthz := server.engine.Group("healthz")
healthz.GET("/liveness", server.liveness)
healthz.GET("/readiness", server.readiness)
// health endpoints
healthZ := server.engine.Group("healthz")
healthZ.GET("/liveness", server.liveness)
healthZ.GET("/readiness", server.readiness)

v1 := server.engine.Group("api/v1")
v1.GET("/services", server.services)
Expand Down
9 changes: 4 additions & 5 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

const (
delimeter = "."
seperator = "__"
delimiter = "."

tagName = "koanf"

Expand All @@ -24,7 +23,7 @@ const (
)

func Load(print bool) *Config {
k := koanf.New(delimeter)
k := koanf.New(delimiter)

// load default configuration from file
if err := LoadValues(k); err != nil {
Expand Down Expand Up @@ -62,7 +61,7 @@ func LoadValues(k *koanf.Koanf) error {
}

func loadConfigmap(k *koanf.Koanf) error {
// this is a hack to check wheather we are in cluster or not
// this is a hack to check whether we are in cluster or not
if _, err := rest.InClusterConfig(); err != nil {
if err == rest.ErrNotInCluster {
return nil
Expand All @@ -75,7 +74,7 @@ func loadConfigmap(k *koanf.Koanf) error {
panic(fmt.Errorf("error reading currnet namespace: %v", err))
}

if err := k.Load(rawbytes.Provider(cm), nil); err != nil {
if err := k.Load(rawbytes.Provider(cm), yaml.Parser()); err != nil {
return fmt.Errorf("error loading values: %s", err)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Querier interface {
}

type querier struct {
config *Config
loggger *zap.Logger
config *Config
logger *zap.Logger

ticker *time.Ticker
done chan bool
Expand All @@ -33,7 +33,7 @@ type state struct {
}

func New(cfg *Config, lg *zap.Logger) Querier {
instance := &querier{config: cfg, loggger: lg}
instance := &querier{config: cfg, logger: lg}

instance.ticker = time.NewTicker(cfg.RequestInterval)
instance.done = make(chan bool)
Expand All @@ -59,7 +59,7 @@ func (q *querier) initializeState() {
}
}

// Start, run this function in a seperate goroutine
// Start run this function in a separate goroutine
func (q *querier) Start() {
for {
select {
Expand Down
6 changes: 3 additions & 3 deletions internal/querier/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ func (q *querier) queryThanos(query string, timestamp string) result {
}{}

if err = json.Unmarshal(rawBody, body); err != nil {
q.loggger.Error("Error unmarshaling query result", zap.Error(err))
q.logger.Error("Error unmarshalling query result", zap.Error(err))
return
}

for index := 0; index < len(body.Data.Result); index++ {
if len(body.Data.Result[index].Value) != 2 {
resultChannel <- badResult
q.loggger.Error("Invalid result value length")
q.logger.Error("Invalid result value length")
return
}

Expand All @@ -136,7 +136,7 @@ func (q *querier) queryThanos(query string, timestamp string) result {
}

resultChannel <- badResult
q.loggger.Error("Invalid result value type, it should be 0 or 1")
q.logger.Error("Invalid result value type, it should be 0 or 1")
return
}

Expand Down

0 comments on commit 04eccee

Please sign in to comment.