package utils

import (
	"crypto/rsa"
	"crypto/x509"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"time"
)

type TimeType = time.Time

func TimeNow() TimeType {
	return time.Now()
}

func Sleep(sec int)  {
	time.Sleep(time.Duration(sec) * time.Second)
}

func RandInt(n int) int {
	return rand.Intn(n)
}

func MarshalPublicKey(key *rsa.PublicKey) ([]byte, error) {
	return x509.MarshalPKIXPublicKey(key)
}

func ErrorHandler(ctx *gin.Context) {
	defer func() {
		if recover() != nil {
			ctx.JSON(HttpError, Fail("oops! Something bad happened."))
		}
	}()
	ctx.Next()
}

func CheckOrigin(r *http.Request) bool {
	return true
}