foxkit

package module
v0.0.1-alpha.24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 23 Imported by: 4

README

foxkit.go

FoxKit - A simple kit with common API functionality

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoMigrateSQL

func AutoMigrateSQL(pg_conn *gorm.DB, inf ...interface{})

migrates all tables, panics on error

func BindJson

func BindJson(c *gin.Context, obj interface{}, appName string) bool

bind the received json to the given struct, sets the status to 400 if false, 406 if no json was requested

func BindURI

func BindURI(c *gin.Context, obj interface{}, appName string) bool

Binds an URI to the given struct, sets 400 on false

func CORS

func CORS(c *gin.Context)

returns 204 for CORS

func CheckEmail

func CheckEmail(value string) bool

returns true if the given string is an email

func CheckError

func CheckError(c *gin.Context, err *error, appName string) bool

returns true if an error happened, sets the status to 500 if true

func CheckPassword

func CheckPassword(c *gin.Context, hash, password *string) bool

checks if the password matches the hash, sets the HTTP code to 500 on DB failure / 401 if the password is wrong

func CheckSession

func CheckSession(ctx *context.Context, c *gin.Context, userID, token *string, redisClient *redis.Client, sessionTime time.Duration) bool

checks if the session is valid, sets the HTTP code to 500 on DB error / 401 on non-valid if false

func CheckString

func CheckString(testString string, minLength, maxLength uint32, asciiOnly bool) bool

returns true if all parameters are true

func CheckStringFull

func CheckStringFull(testString string, minLength, maxLength uint32) (minSize bool, maxSize bool, ascii bool)

returns false if a given parameter is false, minSize, maxSize, ascii TODO: Test

func CheckStringR

func CheckStringR(parameter *string, paramName string, minLength, maxLength uint32, asciiOnly bool) (bool, string)

checks the string and generates an error message

func CheckToken

func CheckToken(c *gin.Context, stringOne, stringTwo *string) bool

checks if both token match, sets the HTTP code to 400 on encoding failure / 401 on token miss match

func CoffeOut

func CoffeOut(c *gin.Context)

returns that the combined coffee/tea pot is temporarily out of coffee

func ComparePasswordAndHash

func ComparePasswordAndHash(password, encodedHash *string) (match bool, err error)

func ConfigRouter

func ConfigRouter(router *gin.Engine, trustedProxy []string)

configures the Trusted proxies list

func ConnectRedis

func ConnectRedis(ctx context.Context, dbNumber int) *redis.Client

connects to the redis server specified in ENV, tests the connection

func ConnectSQL

func ConnectSQL() *gorm.DB

creates a connection to Postgres using ENV

func CountDB

func CountDB(c *gin.Context, pg_conn *gorm.DB, model interface{}, condition interface{}) (int64, error)

returns the number of rows with the given condition

func CreateHash

func CreateHash(password *string) (string, error)

func CreateSession

func CreateSession(ctx *context.Context, userID *string, redisClient *redis.Client, keyLength uint32, maxSessions int, sessionDuration time.Duration) (string, string, error)

creates a new session in the given redis DB adds the session number to the UserID to keep the session ID unique returns sessionID, sessionKey, err

func DecodeHash

func DecodeHash(encodedHash *string) (params usedHashParams, err error)

func DeleteDB

func DeleteDB(c *gin.Context, pg_conn *gorm.DB, condition interface{}, inf interface{}) bool

Deletes Data Entry with condition, sets the status to 500 if false

func ErrorFatal

func ErrorFatal(name string, err error)

func ErrorPanic

func ErrorPanic(err error)

func ExistsDB

func ExistsDB(c *gin.Context, pg_conn *gorm.DB, model interface{}, condition interface{}) (bool, error)

returns true if an entry for the given condition exists

func GetDB

func GetDB(c *gin.Context, pg_conn *gorm.DB, condition interface{}, inf interface{}) bool

Finds and Returns true with the Data Entry or false if not found, sets status to 500 (error) or 404 (not found) if false

func GetHealth

func GetHealth(c *gin.Context)

returns a static health message

func GetUUID

func GetUUID() string

returns a randomly generated UUID (v4) without hyphen

func ImTeaPot

func ImTeaPot(c *gin.Context)

returns that the server is not a teapot

func IsASCII

func IsASCII(s string) bool

returns true if the string only contains ascii characters

func JsonRequested

func JsonRequested(c *gin.Context, appName string) bool

returns true, if the client requested json format, also sets the response to 406, if not

func LogError

func LogError(name string, err error)

func LogEvent

func LogEvent(name string, message string)

func RandomBytes

func RandomBytes(length uint32) ([]byte, error)

func RandomString

func RandomString(length uint32) (string, error)

func RandomStringCompare

func RandomStringCompare(stringOne, stringTwo *string) (bool, error)

decodes the strings and cryptographically compares them, returns true if they match

func Redirect

func Redirect(url string) gin.HandlerFunc

redirects to the given url

func SetupUUID

func SetupUUID()

enables using the random pool, might be insecure and isn't thread safe

func StartRouter

func StartRouter(router *gin.Engine, bind string)

starts the router on the given IP and port

func StoreDB

func StoreDB(c *gin.Context, pg_conn *gorm.DB, inf interface{}) bool

tries to push the data into the DB table, sets status to 500 if false

func UpdateDB

func UpdateDB(c *gin.Context, pg_conn *gorm.DB, condition interface{}, inf interface{}) bool

Finds Data Entry with condition and updates it with new data, sets status to 500 if false

func ValidateCaptcha

func ValidateCaptcha(c *gin.Context, secret string, response *string, hostname string, maxAge time.Duration) (bool, bool)

validates the captcha and returns (valid, error) when error is true, http code is set to 500

func ValidateSession

func ValidateSession(ctx *context.Context, uid, token *string, redisClient *redis.Client, sessionDuration time.Duration) (bool, error)

checks if the given session is valid

Types

type HCaptchaResponse

type HCaptchaResponse struct {
	Success     bool      `json:"success"`
	ChallengeTS time.Time `json:"challenge_ts"`
	Hostname    string    `json:"hostname"`
	Credit      bool      `json:"credit"`
	ErrorCode   []string  `json:"error-codes"`
}

func (*HCaptchaResponse) Get

func (hresp *HCaptchaResponse) Get(secret, response *string) error

retrieves the HCaptcha response

func (*HCaptchaResponse) Valid

func (captcha *HCaptchaResponse) Valid(hostname string, validFor time.Duration) (bool, error)

checks if the hcaptcha response is valid, returns error if the response is invalid

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL