-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,183 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
i18nstore "github.com/satont/twitch-notifier/internal/i18n/store" | ||
"github.com/satont/twitch-notifier/internal/pgx" | ||
repositories "github.com/satont/twitch-notifier/internal/repository/fx" | ||
"github.com/satont/twitch-notifier/pkg/logger" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func main() { | ||
fx.New().Run() | ||
fx.New( | ||
// fx.NopLogger, | ||
fx.Provide( | ||
fx.Annotate( | ||
logger.NewFx(), | ||
fx.As(new(logger.Logger)), | ||
), | ||
fx.Annotate( | ||
i18nstore.New, | ||
fx.As(new(i18nstore.I18nStore)), | ||
), | ||
pgx.New, | ||
), | ||
repositories.Module, | ||
fx.Invoke(pgx.New), | ||
).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package store | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/satont/twitch-notifier/internal/domain" | ||
"github.com/satont/twitch-notifier/pkg/logger" | ||
) | ||
|
||
type translation map[string]any | ||
|
||
func New(l logger.Logger) (*Store, error) { | ||
store := &Store{ | ||
locales: make(map[domain.Language]translation), | ||
} | ||
|
||
err := store.readLocales() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
supported := store.GetSupportedLanguages() | ||
l.Info("Locales loaded", slog.Any("locales", supported)) | ||
|
||
return store, nil | ||
} | ||
|
||
type Store struct { | ||
locales map[domain.Language]translation | ||
} | ||
|
||
var _ I18nStore = (*Store)(nil) | ||
|
||
func (c *Store) readLocales() error { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
files, err := os.ReadDir(filepath.Join(pwd, "locales")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
if file.IsDir() { | ||
panic("locales directory should not contain directories") | ||
} | ||
|
||
data := make(map[string]any) | ||
|
||
content, err := os.ReadFile(filepath.Join(pwd, "locales", file.Name())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := json.Unmarshal(content, &data); err != nil { | ||
return err | ||
} | ||
|
||
name := strings.Replace(file.Name(), ".json", "", 1) | ||
|
||
c.locales[domain.Language(name)] = data | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var ErrLocaleNotFound = errors.New("locale not found") | ||
var ErrKeyNotFound = errors.New("key not found") | ||
|
||
func (c *Store) GetKey(language domain.Language, key string) (string, error) { | ||
lang := c.locales[language] | ||
if lang == nil { | ||
return "", ErrLocaleNotFound | ||
} | ||
|
||
value, ok := lang[key] | ||
if !ok { | ||
return "", ErrKeyNotFound | ||
} | ||
|
||
return value.(string), nil | ||
} | ||
|
||
func (c *Store) GetSupportedLanguages() []domain.Language { | ||
languages := make([]domain.Language, 0, len(c.locales)) | ||
|
||
for language := range c.locales { | ||
languages = append(languages, language) | ||
} | ||
|
||
return languages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pgx | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/satont/twitch-notifier/pkg/logger" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type Opts struct { | ||
fx.In | ||
LC fx.Lifecycle | ||
|
||
Logger logger.Logger | ||
} | ||
|
||
func New(opts Opts) (*pgxpool.Pool, error) { | ||
pgx, err := pgxpool.New( | ||
context.Background(), | ||
"postgres://postgres:postgres@localhost:5432/twitch_notifier", | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts.LC.Append( | ||
fx.Hook{ | ||
OnStop: func(ctx context.Context) error { | ||
pgx.Close() | ||
return nil | ||
}, | ||
OnStart: func(ctx context.Context) error { | ||
// return pgx.Ping(ctx) | ||
opts.Logger.Info("Connected to postgres") | ||
return nil | ||
}, | ||
}, | ||
) | ||
|
||
return pgx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package channel | ||
|
||
import ( | ||
"go.uber.org/fx" | ||
) | ||
|
||
var Module = fx.Provide( | ||
fx.Annotate( | ||
NewPgx, | ||
fx.As(new(Repository)), | ||
), | ||
) |
Oops, something went wrong.