init server

This commit is contained in:
2021-11-03 14:10:58 +01:00
commit 4f9782785d
1603 changed files with 519678 additions and 0 deletions

8
internal/utils/headers.go Executable file
View File

@@ -0,0 +1,8 @@
package utils
const (
HeaderNameContentType = "content-type"
HeaderNameCorrelationID = "correlationID"
HeaderValueApplicationJSONUTF8 = "application/json; charset=UTF-8"
)

74
internal/utils/logger.go Executable file
View 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
}

View 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
View File

@@ -0,0 +1,31 @@
package utils
import (
"encoding/json"
"net/http"
"nos-comptes/internal/storage/model"
)
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)
}