refactor:

This commit is contained in:
Vyacheslav1557 2024-10-09 23:55:16 +05:00
parent 81e75e5a9c
commit d62ae666d5
57 changed files with 656 additions and 310 deletions

1
pkg/external/aws/aws.go vendored Normal file
View file

@ -0,0 +1 @@
package aws

1
pkg/external/kafka/kafka.go vendored Normal file
View file

@ -0,0 +1 @@
package kafka

1
pkg/external/ms-auth/client.go vendored Normal file
View file

@ -0,0 +1 @@
package ms_auth

71
pkg/external/pandoc/pandoc.go vendored Normal file
View file

@ -0,0 +1,71 @@
package pandoc
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
)
type Client struct {
client *http.Client
address string
}
type PandocClient interface {
ConvertLatexToHtml5(ctx context.Context, text string) (string, error)
}
func NewPandocClient(client *http.Client, address string) *Client {
return &Client{
client: client,
address: address,
}
}
type convertRequest struct {
Text string `json:"text"`
From string `json:"from"`
To string `json:"to"`
}
func (client *Client) convert(ctx context.Context, text, from, to string) (string, error) {
body, err := json.Marshal(convertRequest{
Text: text,
From: from,
To: to,
})
if err != nil {
return "", err
}
buf := bytes.NewBuffer(body)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, client.address, buf)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", err
}
body, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func (client *Client) ConvertLatexToHtml5(ctx context.Context, text string) (string, error) {
return client.convert(ctx, text, "latex", "html5")
}

30
pkg/external/postgres/postgres.go vendored Normal file
View file

@ -0,0 +1,30 @@
package postgres
import (
"github.com/jmoiron/sqlx"
"time"
)
const (
maxOpenConns = 60
connMaxLifetime = 120
maxIdleConns = 30
connMaxIdleTime = 20
)
func NewPostgresDB(dsn string) (*sqlx.DB, error) {
db, err := sqlx.Open("pgx", dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(maxOpenConns)
db.SetConnMaxLifetime(connMaxLifetime * time.Second)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxIdleTime(connMaxIdleTime * time.Second)
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}

12
pkg/external/valkey/valkey.go vendored Normal file
View file

@ -0,0 +1,12 @@
package valkey
import "github.com/valkey-io/valkey-go"
func NewValkeyClient(dsn string) (valkey.Client, error) {
opts, err := valkey.ParseURL(dsn)
if err != nil {
return nil, err
}
return valkey.NewClient(opts)
}

132
pkg/utils/errors.go Normal file
View file

@ -0,0 +1,132 @@
package utils
import (
"errors"
"fmt"
"go.uber.org/zap/zapcore"
"runtime"
)
type code uint8
const (
ErrValidationFailed code = 1
ErrInternal code = 2
ErrExternal code = 3
ErrNoPermission code = 4
ErrUnknown code = 5
ErrDeadlineExceeded code = 6
ErrNotFound code = 7
ErrAlreadyExists code = 8
ErrConflict code = 9
ErrUnimplemented code = 10
ErrBadInput code = 11
ErrUnauthenticated code = 12
)
func (c code) String() string {
switch {
case errors.Is(c, ErrValidationFailed):
return "validation error"
case errors.Is(c, ErrInternal):
return "internal error"
case errors.Is(c, ErrExternal):
return "external error"
case errors.Is(c, ErrNoPermission):
return "permission error"
case errors.Is(c, ErrUnknown):
return "unknown error"
case errors.Is(c, ErrDeadlineExceeded):
return "deadline error"
case errors.Is(c, ErrNotFound):
return "not found error"
case errors.Is(c, ErrAlreadyExists):
return "already exists error"
case errors.Is(c, ErrConflict):
return "conflict error"
case errors.Is(c, ErrUnimplemented):
return "unimplemented error"
case errors.Is(c, ErrBadInput):
return "bad input error"
}
panic("unimplemented")
}
func (c code) Error() string {
return c.String()
}
type layer uint8
const (
LayerTransport layer = 1
LayerService layer = 2
LayerStorage layer = 3
)
func (l layer) String() string {
switch l {
case LayerTransport:
return "transport"
case LayerService:
return "service"
case LayerStorage:
return "storage"
}
panic("unimplemented")
}
func location(skip int) string {
_, file, line, _ := runtime.Caller(skip)
return fmt.Sprintf("%s:%d", file, line)
}
type Error struct {
src error
layer layer
code code
msg string
loc string
}
func wrap(src error, layer layer, class code, msg string, loc string) *Error {
return &Error{
src: src,
layer: layer,
code: class,
msg: msg,
loc: loc,
}
}
func (e *Error) Unwrap() []error {
return []error{e.src, e.code}
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.code.String(), e.msg)
}
func (e *Error) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if e.src != nil {
encoder.AddString("src", e.src.Error())
}
encoder.AddString("layer", e.layer.String())
encoder.AddString("code", e.code.String())
encoder.AddString("msg", e.msg)
return nil
}
func TransportError(src error, code code, msg string) error {
return wrap(src, LayerTransport, code, msg, location(2))
}
func ServiceError(src error, code code, msg string) error {
return wrap(src, LayerService, code, msg, location(2))
}
func StorageError(src error, code code, msg string) error {
return wrap(src, LayerStorage, code, msg, location(2))
}

33
pkg/utils/utils.go Normal file
View file

@ -0,0 +1,33 @@
package utils
import (
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)
func AsTimeP(t time.Time) *time.Time {
return &t
}
func AsInt32P(v int32) *int32 {
return &v
}
func AsStringP(str string) *string {
return &str
}
func TimeP(t *timestamppb.Timestamp) *time.Time {
if t == nil {
return nil
}
tt := t.AsTime()
return &tt
}
func TimestampP(t *time.Time) *timestamppb.Timestamp {
if t == nil {
return nil
}
return timestamppb.New(*t)
}