This commit is contained in:
Jeffrey Duroyon
2019-04-04 14:29:48 +02:00
committed by Jeffrey Duroyon
parent 0df6d64c35
commit 33db360b03
38 changed files with 1476 additions and 1 deletions

71
internal/ginserver/logger.go Executable file
View File

@@ -0,0 +1,71 @@
package ginserver
import (
"github.com/kratisto/mam-contract/internal/utils"
"math/rand"
"time"
"github.com/gin-gonic/gin"
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano())
func randStringBytesMaskImprSrc(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func GetLoggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
correlationID := c.Request.Header.Get(utils.HeaderNameCorrelationID)
if correlationID == "" {
correlationID = randStringBytesMaskImprSrc(30)
c.Writer.Header().Set(utils.HeaderNameCorrelationID, correlationID)
}
logger := utils.GetLogger()
logEntry := logger.WithField(utils.HeaderNameCorrelationID, correlationID)
c.Set(utils.ContextKeyLogger, logEntry)
}
}
func GetHTTPLoggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
utils.GetLoggerFromCtx(c).
WithField("method", c.Request.Method).
WithField("url", c.Request.RequestURI).
WithField("from", c.ClientIP()).
Info("start handling HTTP request")
c.Next()
d := time.Since(start)
utils.GetLoggerFromCtx(c).
WithField("status", c.Writer.Status()).
WithField("duration", d.String()).
Info("end handling HTTP request")
}
}

View File

@@ -0,0 +1,40 @@
package ginserver
import (
"fmt"
"github.com/kratisto/mam-contract/internal/storage/model"
"github.com/kratisto/mam-contract/internal/utils"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"google.golang.org/api/oauth2/v1"
)
func ValidateOAuthToken(c *gin.Context) {
authorizationHeader := c.GetHeader("Authorization")
authorizationHeaderSplitted := strings.Split(authorizationHeader, " ")
if len(authorizationHeaderSplitted) != 2 {
utils.JSONError(c.Writer, model.ErrBadRequestFormat)
c.Abort()
return
}
oauth2Service, err := oauth2.New(&http.Client{})
if oauth2Service == nil {
fmt.Println(err)
utils.JSONError(c.Writer, model.ErrInternalServer)
c.Abort()
return
}
tokenInfoCall := oauth2Service.Tokeninfo()
tokenInfoCall.IdToken(authorizationHeaderSplitted[1])
token, err := tokenInfoCall.Do()
if err != nil {
utils.GetLogger().WithError(err).Error(err)
utils.JSONError(c.Writer, model.ErrBadRequestFormat)
c.Abort()
return
}
c.Set("googleUserId", token.UserId)
}

View File

@@ -0,0 +1,45 @@
package ginserver
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/kratisto/mam-contract/configuration"
"github.com/kratisto/mam-contract/internal/utils"
"time"
)
var (
routerInjectorKey = "ROUTER"
SecuredRouterInjectorKey = "SECURED_ROUTER"
UnsecuredRouterInjectorKey = "UNSECURED_ROUTER"
)
func Setup(injector *utils.Injector, config *configuration.Config) {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.HandleMethodNotAllowed = true
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:8080/", "http://localhost:8080", "http://localhost:19006"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
router.Use(gin.Recovery())
router.Use(GetLoggerMiddleware())
router.Use(GetHTTPLoggerMiddleware())
public := router.Group("/")
injector.Set(UnsecuredRouterInjectorKey, public)
securedUserRoute := public.Group("/users")
securedUserRoute.Use(ValidateOAuthToken)
injector.Set(SecuredRouterInjectorKey, securedUserRoute)
injector.Set(routerInjectorKey, router)
}

View File

@@ -0,0 +1,38 @@
package ginserver
import (
"github.com/gin-gonic/gin"
"github.com/kratisto/mam-contract/internal/utils"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func Start(injector *utils.Injector) {
router := utils.Get[*gin.Engine](injector, routerInjectorKey)
srv := &http.Server{
Addr: ":8080",
Handler: router,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
}

View File

@@ -0,0 +1,27 @@
package health
import (
"net/http"
"github.com/gin-gonic/gin"
)
// @openapi:path
// /_health:
//
// get:
// tags:
// - "Monitoring"
// summary: Health check
// description: Health check
// responses:
// 200:
// description: "Health response"
// content:
// application/json:
// schema:
// $ref: "#/components/schemas/Health"
func GetHealth(c *gin.Context) {
health := &Health{Alive: true}
c.JSON(http.StatusOK, health)
}

8
internal/health/model.go Normal file
View File

@@ -0,0 +1,8 @@
package health
// Health struct
// @openapi:schema
type Health struct {
Alive bool `json:"alive"`
Version string `json:"version"`
}

15
internal/health/setup.go Normal file
View File

@@ -0,0 +1,15 @@
package health
import (
"github.com/gin-gonic/gin"
"github.com/kratisto/mam-contract/internal/ginserver"
"github.com/kratisto/mam-contract/internal/utils"
"net/http"
)
func Setup(injector *utils.Injector) {
publicRoute := utils.Get[*gin.RouterGroup](injector, ginserver.UnsecuredRouterInjectorKey)
//TODO add secure auth
publicRoute.Handle(http.MethodGet, "/health", GetHealth)
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"fmt"
)
type Type int
const (
ErrTypeNotFound Type = iota
ErrTypeDuplicate
ErrTypeForeignKeyViolation
)
type Error struct {
Cause error
Type Type
}
func NewDAOError(t Type, cause error) error {
return &Error{
Type: t,
Cause: cause,
}
}
func (e *Error) Error() string {
if e.Cause != nil {
return fmt.Sprintf("Type %d: %s", e.Type, e.Cause.Error())
}
return fmt.Sprintf("Type %d: no cause given", e.Type)
}

View File

@@ -0,0 +1,43 @@
package postgresql
import (
"database/sql"
"fmt"
"github.com/kratisto/mam-contract/internal/storage/dao"
"github.com/kratisto/mam-contract/internal/utils"
"github.com/lib/pq"
)
const (
pgCodeUniqueViolation = "23505"
pgCodeForeingKeyViolation = "23503"
)
func HandlePgError(e *pq.Error) error {
if e.Code == pgCodeUniqueViolation {
return dao.NewDAOError(dao.ErrTypeDuplicate, e)
}
if e.Code == pgCodeForeingKeyViolation {
return dao.NewDAOError(dao.ErrTypeForeignKeyViolation, e)
}
return e
}
type DatabasePostgreSQL struct {
Session *sql.DB
}
func NewDatabasePostgreSQL(connectionURI string) *DatabasePostgreSQL {
db, err := sql.Open("postgres", connectionURI)
if err != nil {
utils.GetLogger().WithError(err).Fatal("Unable to get a connection to the postgres db")
}
err = db.Ping()
if err != nil {
fmt.Println(err)
utils.GetLogger().WithError(err).Fatal("Unable to ping the postgres db")
}
return &DatabasePostgreSQL{Session: db}
}

View File

@@ -0,0 +1,13 @@
package postgresql
import (
"github.com/kratisto/mam-contract/internal/utils"
)
var DatabaseKey = "POSTGRES"
func Setup(injector *utils.Injector, connectionUri string) {
database := NewDatabasePostgreSQL(connectionUri)
injector.Set(DatabaseKey, database)
}

View File

@@ -0,0 +1,68 @@
package model
import (
"fmt"
"net/http"
"github.com/lib/pq"
)
var (
// 400
ErrBadRequestFormat = APIError{
Type: "bad_format",
HTTPCode: http.StatusBadRequest,
Description: "unable to read request body, please check that the json is valid",
}
ErrDataValidation = APIError{
Type: "data_validation",
HTTPCode: http.StatusBadRequest,
Description: "the data are not valid",
}
// 404
ErrNotFound = APIError{
Type: "not_found",
HTTPCode: http.StatusNotFound,
}
// 40x
ErrAlreadyExists = APIError{
Type: "already_exists",
HTTPCode: http.StatusConflict,
}
// 50x
ErrInternalServer = APIError{
Type: "internal_server_error",
HTTPCode: http.StatusInternalServerError,
}
)
type APIError struct {
HTTPCode int `json:"-"`
Type string `json:"error"`
Description string `json:"error_description"`
Details []FieldError `json:"error_details,omitempty"`
Headers map[string][]string `json:"-"`
}
type FieldError struct {
Field string `json:"field"`
Constraint string `json:"constraint"`
Description string `json:"description"`
}
func (e *APIError) Error() string {
return fmt.Sprintf("error : %d, %s, %s, %v", e.HTTPCode, e.Type, e.Description, e.Details)
}
func FromPostgresError(err *pq.Error) *APIError {
return &APIError{
HTTPCode: 0,
Type: "",
Description: "",
Details: nil,
Headers: nil,
}
}

View File

@@ -0,0 +1,44 @@
package validators
import (
"fmt"
"github.com/kratisto/mam-contract/internal/storage/model"
"github.com/kratisto/mam-contract/internal/utils"
"regexp"
"strings"
"gopkg.in/go-playground/validator.v9"
)
var regexpValidatorNamespacePrefix = regexp.MustCompile(`^\w+\.`)
func NewDataValidationAPIError(err error) model.APIError {
apiErr := model.ErrDataValidation
if err != nil {
if _, ok := err.(*validator.InvalidValidationError); ok {
utils.GetLogger().WithError(err).WithField("templateAPIErr", apiErr).Error("InvalidValidationError")
} else {
for _, e := range err.(validator.ValidationErrors) {
reason := e.Tag()
if _, ok := CustomValidators[e.Tag()]; ok {
reason = truncatingSprintf(CustomValidators[e.Tag()].Message, e.Param())
}
namespaceWithoutStructName := regexpValidatorNamespacePrefix.ReplaceAllString(e.Namespace(), "")
fe := model.FieldError{
Field: namespaceWithoutStructName,
Constraint: e.Tag(),
Description: reason,
}
apiErr.Details = append(apiErr.Details, fe)
}
}
}
return apiErr
}
// truncatingSprintf is used as fmt.Sprintf but allow to truncate the additional parameters given when there is more parameters than %v in str
func truncatingSprintf(str string, args ...interface{}) string {
n := strings.Count(str, "%v")
return fmt.Sprintf(str, args[:n]...)
}

View File

@@ -0,0 +1,34 @@
package validators
import (
"context"
"strings"
"gopkg.in/go-playground/validator.v9"
)
var (
CustomValidators = map[string]customValidator{
"enum": {
Message: "This field should be in: %v",
Validator: validateEnum,
},
"required": {
Message: "This field is required and cannot be empty",
},
}
)
type customValidator struct {
Message string
Validator validator.FuncCtx
}
func validateEnum(ctx context.Context, fl validator.FieldLevel) bool {
for _, v := range strings.Split(fl.Param(), " ") {
if v == fl.Field().String() {
return true
}
}
return false
}

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"
)

View 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
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"
"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)
}

View 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())
}

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