Skip to content

Commit

Permalink
refactor: rewrite pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
vaayne committed Jul 15, 2024
1 parent 8b63b22 commit 36347cc
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 22 deletions.
6 changes: 4 additions & 2 deletions internal/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
)

// var instance *cache.Cache
var instance *cache.Cache
var once sync.Once
var (
instance *cache.Cache
once sync.Once
)

// New creates a new cache instance
func New() *cache.Cache {
Expand Down
44 changes: 44 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"vibrain/internal/pkg/logger"

"github.com/caarlos0/env/v11"
)

var Settings = &Config{}

// type DatabaseConfig struct {
// Driver string `json:"driver"` // postgres, mysql, sqlite3
// Host string `json:"host"`
// Port int `json:"port"`
// User string `json:"user"`
// Password string `json:"password"`
// Name string `json:"name"`
// }

// func (d DatabaseConfig) DSN() string {
// switch d.Driver {
// case "postgres":
// return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.User, d.Password, d.Name)
// case "mysql":
// return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", d.User, d.Password, d.Host, d.Port, d.Name)
// case "sqlite3":
// return d.Name
// }
// return ""
// }

type Config struct {
Debug bool `env:"DEBUG" envDefault:"false"`
Port int `env:"PORT" envDefault:"1323"`
DatabaseURL string `env:"DATABASE_URL,required"`
QueueDatabaseURL string `env:"QUEUE_DATABASE_URL,expand" envDefault:"${DATABASE_URL}"`
}

func init() {
if err := env.Parse(Settings); err != nil {
logger.Default.Fatal("failed to load settings", "err", err)
}
logger.Default.Info("settings loaded")
}
8 changes: 0 additions & 8 deletions internal/pkg/config/constants.go

This file was deleted.

9 changes: 9 additions & 0 deletions internal/pkg/constant/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package constant

type ContextKey string

const (
ContextKeyRequestID = "request_id"
ContextKeyLogger = "logger"
ContextKeyUserID = "user_id"
)
44 changes: 32 additions & 12 deletions internal/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ import (
"log/slog"
"os"
"strconv"
"vibrain/internal/pkg/config"
"vibrain/internal/pkg/constant"
)

var defaultLogAttrs = []string{config.ContextKeyRequestID, config.ContextKeyUserID}
var defaultLogAttrs = []string{constant.ContextKeyRequestID, constant.ContextKeyUserID}

func New() *slog.Logger {
// Default logger
var Default = New()

// Logger is a wrapper around slog.Logger
type Logger struct {
*slog.Logger
}

// Debug logs a message at level Fatal on the standard logger.
// it will exit the program after logging
func (l Logger) Fatal(msg string, args ...interface{}) {
l.Error(msg, args...)
os.Exit(1)
}

// New creates a new logger
func New() Logger {
debug, err := strconv.ParseBool(os.Getenv("DEBUG"))
if err != nil {
debug = false
Expand All @@ -21,22 +37,26 @@ func New() *slog.Logger {
} else {
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
}
return logger
return Logger{
Logger: logger,
}
}

func FromContext(ctx context.Context) *slog.Logger {
logger := getValFromContext(ctx, config.ContextKeyLogger)
// FromContext returns a logger from context
func FromContext(ctx context.Context) Logger {
logger := getValFromContext(ctx, constant.ContextKeyLogger)
if logger != nil {
return logger.(*slog.Logger)
return logger.(Logger)
}
sLogger := New()
handler := sLogger.Handler().WithAttrs(buildLogAttrs(ctx))
return Logger{
Logger: slog.New(handler),
}
newLogger := New()
handler := newLogger.Handler().WithAttrs(buildLogAttrs(ctx))
newLogger = slog.New(handler)
return newLogger
}

func getValFromContext(ctx context.Context, key string) interface{} {
return ctx.Value(config.ContextKey(key))
return ctx.Value(constant.ContextKey(key))
}

func buildLogAttrs(ctx context.Context) []slog.Attr {
Expand Down

0 comments on commit 36347cc

Please sign in to comment.