Add missing cmd folders, change logrus to slog, update gitignore

This commit is contained in:
eleksir
2025-10-12 15:25:08 +03:00
parent 6cebe40018
commit 1330c512aa
40 changed files with 284 additions and 3149 deletions

View File

@@ -2,6 +2,7 @@ package config
import (
"aleesa-webapp-go/internal/defaults"
"aleesa-webapp-go/internal/log"
"bufio"
"encoding/json"
"fmt"
@@ -11,7 +12,6 @@ import (
"github.com/cockroachdb/pebble"
"github.com/go-redis/redis/v8"
"github.com/hjson/hjson-go"
log "github.com/sirupsen/logrus"
)
// MyConfig структурка, описывающая конфиг.

View File

@@ -5,6 +5,7 @@ import (
"aleesa-webapp-go/internal/bunicomic"
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/flickr"
"aleesa-webapp-go/internal/log"
"aleesa-webapp-go/internal/monkeyuser"
"aleesa-webapp-go/internal/oboobs"
"aleesa-webapp-go/internal/obutts"
@@ -20,8 +21,6 @@ import (
"math/rand/v2"
"regexp"
"time"
log "github.com/sirupsen/logrus"
)
// MsgParser горутинка, которая парсит json-чики прилетевшие из REDIS-ки.
@@ -46,7 +45,7 @@ func MsgParser(cfg *config.MyConfig, ctx context.Context, msg string) {
j, err = ValidateRmsg(cfg, j, msg)
if err != nil {
log.Warn(err)
log.Warnf("%s", err)
return
}
@@ -417,7 +416,7 @@ func MsgParser(cfg *config.MyConfig, ctx context.Context, msg string) {
city := openweathermap.QueryOwmUserCache(cfg, j.Chatid, j.Userid)
if city == "" {
j.Message = fmt.Sprint("Не припоминаю, какой город вас интересовал в прошлый раз.")
j.Message = "Не припоминаю, какой город вас интересовал в прошлый раз."
} else {
if answer, err := openweathermap.OwmClient(cfg, city, 0); err != nil {
log.Errorf("Unable to handle city %s in openweartermap api: %s", city, err)

106
internal/log/log.go Normal file
View File

@@ -0,0 +1,106 @@
package log
import (
"fmt"
"log/slog"
"os"
"time"
)
// Init setup logger stuff.
// level can be error, warn, info, debug if something other supplied info level selected.
// fileDescriptor should be opened before supplying it to Init().
func Init(level string, fileDescriptor *os.File) {
var loglevel slog.Level
// no panic, no trace.
switch level {
case "error":
loglevel = slog.LevelError
case "warn":
loglevel = slog.LevelWarn
case "info":
loglevel = slog.LevelInfo
case "debug":
loglevel = slog.LevelDebug
default:
loglevel = slog.LevelInfo
}
opts := &slog.HandlerOptions{
// Use the ReplaceAttr function on the handler options
// to be able to replace any single attribute in the log output
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// check that we are handling the time key
if a.Key != slog.TimeKey {
return a
}
t := a.Value.Time()
// change the value from a time.Time to a String
// where the string has the correct time format.
a.Value = slog.StringValue(t.Format(time.DateTime))
return a
},
Level: loglevel,
}
slog.SetDefault(
slog.New(
slog.NewTextHandler(
fileDescriptor,
opts,
),
),
)
}
// Error passes message directly to slog.Error().
func Error(message string) {
slog.Error(message)
}
// Errorf logs message on error log level and allows to supply arguments in printf() style.
func Errorf(format string, a ...any) {
slog.Error(fmt.Sprintf(format, a...))
}
// Warn passes message directly to slog.Warn().
func Warn(message string) {
slog.Warn(message)
}
// Warnf logs message on warn log level and allows to supply arguments in printf() style.
func Warnf(format string, a ...any) {
slog.Warn(fmt.Sprintf(format, a...))
}
// Info passes message directly to slog.Info().
func Info(message string) {
slog.Info(message)
}
// Infof logs message on info log level and allows to supply arguments in printf() style.
func Infof(format string, a ...any) {
slog.Info(fmt.Sprintf(format, a...))
}
// Debug passes message directly to slog.Debug().
func Debug(message string) {
slog.Debug(message)
}
// Debugf logs message on debug log level and allows to supply arguments in printf() style.
func Debugf(format string, a ...any) {
slog.Debug(fmt.Sprintf(format, a...))
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */

View File

@@ -2,6 +2,7 @@ package openweathermap
import (
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/log"
"aleesa-webapp-go/internal/pcachedb"
"crypto/sha256"
"fmt"
@@ -11,7 +12,6 @@ import (
"time"
owm "github.com/briandowns/openweathermap"
log "github.com/sirupsen/logrus"
)
// Структурка для передачи в обрабтчик запросов в openweathermap api.
@@ -319,7 +319,7 @@ func QueryOwmCache(cfg *config.MyConfig, item OwmItem) (string, error) {
// Если всё хорошо, надо обновить кэш.
if err := UpdateOwmCache(cfg, tsNow, key, answer); err != nil {
log.Error(err)
log.Errorf("%s", err)
}
return answer, err

View File

@@ -2,6 +2,7 @@ package pcachedb
import (
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/log"
"crypto/sha256"
"errors"
"fmt"
@@ -9,7 +10,6 @@ import (
"github.com/cockroachdb/errors/oserror"
"github.com/cockroachdb/pebble"
log "github.com/sirupsen/logrus"
)
// StoreKV сохраняет в указанной бд ключ и значение.

View File

@@ -2,6 +2,7 @@ package prazdnikisegodnyaru
import (
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/log"
"aleesa-webapp-go/internal/pcachedb"
"context"
"errors"
@@ -12,7 +13,6 @@ import (
"time"
"github.com/carlmjohnson/requests"
log "github.com/sirupsen/logrus"
"golang.org/x/net/html"
)
@@ -36,7 +36,7 @@ func PsrClient(cfg *config.MyConfig) (string, error) {
}
if err := UpdatePsrCache(cfg, eTime, answer); err != nil {
log.Error(err)
log.Errorf("%s", err)
}
return answer, nil
@@ -56,7 +56,7 @@ func PsrClient(cfg *config.MyConfig) (string, error) {
// Если всё хорошо, надо обновить кэш
if err := UpdatePsrCache(cfg, eTime, answer); err != nil {
log.Error(err)
log.Errorf("%s", err)
}
return answer, nil
@@ -78,7 +78,7 @@ func PsrClient(cfg *config.MyConfig) (string, error) {
// Если всё хорошо, надо обновить кэш
if err := UpdatePsrCache(cfg, eTime, answer); err != nil {
log.Error(err)
log.Errorf("%s", err)
}
return answer, nil

View File

@@ -2,13 +2,12 @@ package xkcdru
import (
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/log"
"fmt"
"io"
"math/rand/v2"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
func APIClient(cfg *config.MyConfig) (string, error) {