init
This commit is contained in:
8
internal/utils/headers.go
Executable file
8
internal/utils/headers.go
Executable file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
const (
|
||||
HeaderNameContentType = "content-type"
|
||||
HeaderNameCorrelationID = "correlationID"
|
||||
|
||||
HeaderValueApplicationJSONUTF8 = "application/json; charset=UTF-8"
|
||||
)
|
||||
29
internal/utils/injector.go
Normal file
29
internal/utils/injector.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Injector struct {
|
||||
content map[string]any
|
||||
}
|
||||
|
||||
func (i *Injector) Get(key string) any {
|
||||
val, ok := i.content[key]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Can't get key %s from injector", key))
|
||||
}
|
||||
return val
|
||||
}
|
||||
func Get[T any](i *Injector, key string) T {
|
||||
return i.Get(key).(T)
|
||||
}
|
||||
|
||||
func (i *Injector) Set(key string, content any) {
|
||||
if i.content == nil {
|
||||
i.content = map[string]any{}
|
||||
}
|
||||
_, ok := i.content[key]
|
||||
if ok {
|
||||
panic(fmt.Sprintf("Key %s already have content", key))
|
||||
}
|
||||
i.content[key] = content
|
||||
}
|
||||
74
internal/utils/logger.go
Executable file
74
internal/utils/logger.go
Executable file
@@ -0,0 +1,74 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
LogFormatText = "text"
|
||||
LogFormatJSON = "json"
|
||||
|
||||
ContextKeyLogger = "logger"
|
||||
)
|
||||
|
||||
var (
|
||||
logLevel = logrus.DebugLevel
|
||||
logFormat logrus.Formatter = &logrus.TextFormatter{}
|
||||
logOut io.Writer
|
||||
)
|
||||
|
||||
func InitLogger(ll, lf string) {
|
||||
logLevel = parseLogrusLevel(ll)
|
||||
logrus.SetLevel(logLevel)
|
||||
|
||||
logFormat = parseLogrusFormat(lf)
|
||||
logrus.SetFormatter(logFormat)
|
||||
|
||||
logOut = os.Stdout
|
||||
logrus.SetOutput(logOut)
|
||||
}
|
||||
|
||||
func GetLoggerFromCtx(c *gin.Context) *logrus.Entry {
|
||||
if logger, ok := c.Get(ContextKeyLogger); ok {
|
||||
logEntry, assertionOk := logger.(*logrus.Entry)
|
||||
if assertionOk {
|
||||
return logEntry
|
||||
}
|
||||
}
|
||||
return logrus.NewEntry(GetLogger())
|
||||
}
|
||||
|
||||
func GetLogger() *logrus.Logger {
|
||||
logger := logrus.New()
|
||||
logger.Formatter = logFormat
|
||||
logger.Level = logLevel
|
||||
logger.Out = logOut
|
||||
return logger
|
||||
}
|
||||
|
||||
func parseLogrusLevel(logLevelStr string) logrus.Level {
|
||||
logLevel, err := logrus.ParseLevel(logLevelStr)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("error while parsing log level. %v is set as default.", logLevel)
|
||||
logLevel = logrus.DebugLevel
|
||||
}
|
||||
return logLevel
|
||||
}
|
||||
|
||||
func parseLogrusFormat(logFormatStr string) logrus.Formatter {
|
||||
var formatter logrus.Formatter
|
||||
switch logFormatStr {
|
||||
case LogFormatText:
|
||||
formatter = &logrus.TextFormatter{ForceColors: true, FullTimestamp: true}
|
||||
case LogFormatJSON:
|
||||
formatter = &logrus.JSONFormatter{}
|
||||
default:
|
||||
logrus.Errorf("error while parsing log format. %v is set as default.", formatter)
|
||||
formatter = &logrus.TextFormatter{ForceColors: true, FullTimestamp: true}
|
||||
}
|
||||
return formatter
|
||||
}
|
||||
45
internal/utils/randomizer/rand.go
Normal file
45
internal/utils/randomizer/rand.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package randomizer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type Random interface {
|
||||
Int() int
|
||||
}
|
||||
|
||||
func init() {}
|
||||
|
||||
type rndGenerator func(n int) int
|
||||
|
||||
type Randomizer struct {
|
||||
fn rndGenerator
|
||||
}
|
||||
|
||||
func NewRandomizer(fn rndGenerator) *Randomizer {
|
||||
return &Randomizer{fn: fn}
|
||||
}
|
||||
|
||||
func (r *Randomizer) Int(n int) int {
|
||||
return r.fn(n)
|
||||
}
|
||||
|
||||
func Rand(rand int) int {
|
||||
return randGen(rand)
|
||||
}
|
||||
func realRand(n int) int { return int(rand.Intn(n)) }
|
||||
func fakeRand(n int) func(numb int) int {
|
||||
return func(numb int) int {
|
||||
if n >= numb {
|
||||
panic(fmt.Sprintf("%d Should not be superior of %d", n, numb))
|
||||
}
|
||||
return n
|
||||
}
|
||||
}
|
||||
|
||||
var randGen = NewRandomizer(realRand).Int
|
||||
|
||||
func FakeRandomizer(n int) {
|
||||
randGen = NewRandomizer(fakeRand(n)).Int
|
||||
}
|
||||
31
internal/utils/responses.go
Executable file
31
internal/utils/responses.go
Executable file
@@ -0,0 +1,31 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/kratisto/mam-contract/internal/storage/model"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func JSON(w http.ResponseWriter, status int, data interface{}) {
|
||||
w.Header().Set(HeaderNameContentType, HeaderValueApplicationJSONUTF8)
|
||||
w.WriteHeader(status)
|
||||
if data != nil {
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
}
|
||||
|
||||
func JSONError(w http.ResponseWriter, e model.APIError) {
|
||||
if e.Headers != nil {
|
||||
for k, headers := range e.Headers {
|
||||
for _, headerValue := range headers {
|
||||
w.Header().Add(k, headerValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
JSON(w, e.HTTPCode, e)
|
||||
}
|
||||
|
||||
func JSONErrorWithMessage(w http.ResponseWriter, e model.APIError, message string) {
|
||||
e.Description = message
|
||||
JSONError(w, e)
|
||||
}
|
||||
12
internal/utils/validator/setup.go
Normal file
12
internal/utils/validator/setup.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"github.com/kratisto/mam-contract/internal/utils"
|
||||
)
|
||||
|
||||
const ValidatorInjectorKey = "VALIDATOR"
|
||||
|
||||
func Setup(injector *utils.Injector) {
|
||||
|
||||
injector.Set(ValidatorInjectorKey, newValidator())
|
||||
}
|
||||
28
internal/utils/validator/validator.go
Normal file
28
internal/utils/validator/validator.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"github.com/kratisto/mam-contract/internal/storage/validators"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func newValidator() *validator.Validate {
|
||||
va := validator.New()
|
||||
|
||||
va.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
||||
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)
|
||||
if len(name) < 1 {
|
||||
return ""
|
||||
}
|
||||
return name[0]
|
||||
})
|
||||
|
||||
for k, v := range validators.CustomValidators {
|
||||
if v.Validator != nil {
|
||||
va.RegisterValidationCtx(k, v.Validator)
|
||||
}
|
||||
}
|
||||
|
||||
return va
|
||||
}
|
||||
Reference in New Issue
Block a user