Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ add logger to project #3

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
// 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 @@
}

// 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
Loading