Skip to content

Commit

Permalink
Merge pull request #3 from khalil-farashiani/features/add-logger
Browse files Browse the repository at this point in the history
⚡ add logger to project
  • Loading branch information
khalil-farashiani authored Apr 5, 2024
2 parents bb1f74e + 145a466 commit 722ab65
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
6 changes: 6 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ Golim help:
- golim remove -i{--id} <role id> [remove specific role]
- golim remove-limiter -l{--limiter} <limiter id> [remove specific limiter]`
)

const (
basePath = "/.golim/"
loggerFileName = "/logs/golim.log"
dbPath = "/db/"
)
4 changes: 2 additions & 2 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"sync"

"github.com/robfig/cron/v3"
Expand All @@ -16,6 +15,7 @@ func runCronTasks(ctx context.Context, g *golim) {
scheduleIncreaseCap(ctx, g)
})
if err != nil {
g.logger.errLog.Println(err)
fmt.Println("Error scheduling task:", err)
}
cr.Start()
Expand All @@ -24,7 +24,7 @@ func runCronTasks(ctx context.Context, g *golim) {
func scheduleIncreaseCap(ctx context.Context, g *golim) {
roles, err := g.getRoles(ctx)
if err != nil {
log.Println("Error getting roles:", err)
g.logger.errLog.Println(err)
return
}
var wg sync.WaitGroup
Expand Down
4 changes: 3 additions & 1 deletion golim.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type golim struct {
limiterRole *limiterRole
port int64
skip bool
*logger

Check failure on line 41 in golim.go

View workflow job for this annotation

GitHub Actions / build

undefined: logger
Store
}

Expand Down Expand Up @@ -126,8 +127,9 @@ func handleLimiterRoleOperation(g *golim, ctx context.Context) (interface{}, err
return nil, errors.New(unknownLimiterRoleError)
}

func newLimiter(db *sql.DB, cache *cache) *golim {
func newLimiter(db *sql.DB, cache *cache, logger *logger) *golim {

Check failure on line 130 in golim.go

View workflow job for this annotation

GitHub Actions / build

undefined: logger
return &golim{
logger: logger,
Store: Store{
db: role.New(db),
cache: cache,
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ func initDB(ctx context.Context) *sql.DB {
// everything start from main
func main() {
ctx := context.Background()
logger := initLogger()

Check failure on line 35 in main.go

View workflow job for this annotation

GitHub Actions / build

undefined: initLogger
db := initDB(ctx)
cache := initRedis()
limiter, err := initFlags(ctx, db, cache)

limiter, err := initFlags(ctx, db, cache, logger)
if err != nil {
logger.errLog.Println(err.Error())
log.Fatalf("Error initializing limiter: %v", err)
}

data, err := limiter.ExecCMD(ctx)
if err != nil {
logger.errLog.Println(err.Error())
log.Fatalf("Error executing command: %v", err)
}
if data != nil {
Expand All @@ -50,8 +54,8 @@ func main() {
}

// initFlags get command and flags from std input to create a golim or role
func initFlags(ctx context.Context, db *sql.DB, cache *cache) (*golim, error) {
golim := newLimiter(db, cache)
func initFlags(ctx context.Context, db *sql.DB, cache *cache, logger *logger) (*golim, error) {

Check failure on line 57 in main.go

View workflow job for this annotation

GitHub Actions / build

undefined: logger
golim := newLimiter(db, cache, logger)

rootCmd := createRootCommand(golim)
if err := rootCmd.ParseAndRun(ctx, os.Args[1:]); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func runProxy(g *golim) http.HandlerFunc {
}
currentUserRole, needToCheckRequest, err := g.getRole(r.Context())
if err != nil {
g.logger.errLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if needToCheckRequest && !isOkRequest(r, g, currentUserRole) {
g.logger.errLog.Println(err)
http.Error(w, slowDownError, http.StatusTooManyRequests)
return
}
Expand Down

0 comments on commit 722ab65

Please sign in to comment.