lib

package
v0.0.0-...-0daaea0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// InternalServerErrorPage is used when some internal code execution has failed.
	InternalServerErrorPage = ErrorPageContent{
		StatusCode:  fasthttp.StatusInternalServerError,
		MainMessage: fasthttp.StatusMessage(fasthttp.StatusInternalServerError),
		Description: "There was a server error while processing your request, this is due to the server.",
	}

	// BadRequestErrorPage is used when the user's request had bad properties.
	BadRequestErrorPage = ErrorPageContent{
		StatusCode:  fasthttp.StatusBadRequest,
		MainMessage: fasthttp.StatusMessage(fasthttp.StatusBadRequest),
		Description: "There was an error without request input.",
	}

	// NotFoundErrorPage is used when something the user is searching for is not found.
	NotFoundErrorPage = ErrorPageContent{
		StatusCode:  fasthttp.StatusNotFound,
		MainMessage: fasthttp.StatusMessage(fasthttp.StatusNotFound),
		Description: "The thing you're searching for has not been found",
	}

	// ForbiddenErrorPage is used when the user is trying to do something without permission.
	ForbiddenErrorPage = ErrorPageContent{
		StatusCode:  fasthttp.StatusForbidden,
		MainMessage: fasthttp.StatusMessage(fasthttp.StatusForbidden),
		Description: "You're not allowed to view the content on this page.",
	}

	// ConflictErrorPage is used when the user tries to create information into the database that
	// already exists.
	ConflictErrorPage = ErrorPageContent{
		StatusCode:  fasthttp.StatusConflict,
		MainMessage: fasthttp.StatusMessage(fasthttp.StatusConflict),

		Description: "The username is already taken.",
	}
)

Functions

func AddRootToPath

func AddRootToPath(path string) string

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash checks if the given password's bcrypt hash matches the given hash.

func CreateToken

func CreateToken(username string) (string, error)

CreateToken creates a jwt token which stores a username and is valid for 24 hours.

func FindCookieInResponse

func FindCookieInResponse(cookie string, resp *http.Response) error

FindCookieInResponse loops over the cookies in a request and checks if those cookies include the given cookie.

func GenerateUUID

func GenerateUUID() string

GenerateUUID uses the 'github.com/satori/go.uuid' package to construct a new V4 unique id.

func GetDatabase

func GetDatabase() *gorm.DB

GetDatabase returns a pointer, which points to the database instance.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword creates a bcrypt hash from a given password.

func IsAuth

func IsAuth(r *http.Request) bool

IsAuth takes in a request pointer and returns a boolean telling if a user has a valid token in their cookies.

func IsPasswordValid

func IsPasswordValid(password string) bool

IsPasswordValid checks if the password a user has chosen is valid.

func IsUsernameValid

func IsUsernameValid(username string) bool

IsUsernameValid checks if the username a user has chosen is valid.

func SetDatabase

func SetDatabase(database *gorm.DB)

SetDatabase sets the global variable in this file to database instance created in the main function.

func ValidatePasswordStrength

func ValidatePasswordStrength(password string) bool

func ValidateToken

func ValidateToken(tokenString string) (string, error)

ValidateToken takes a token as an argument and checks if that token is valid. If the token is valid, then the function returns the usernanem stored in the token.

func WriteResponseJSON

func WriteResponseJSON(ctx *fasthttp.RequestCtx, code int, obj interface{})

WriteResponseJSON takes in a request context, status code, and a golang interface. The functions sets the request's response with the given status code and content type. And then golang's json package encodes the data from the interface into json format.

Types

type C

type C struct {
	Username string `json:"username"`
	jwt.StandardClaims
}

C is a simple struct to keep the username and the standard claims of a jwt, such as the expiration time

type ErrorPageContent

type ErrorPageContent struct {
	StatusCode  int    `json:"status"`
	MainMessage string `json:"message"`
	Description string `json:"description"`
}

ErrorPageContent defines the fields that are on the error site.

func CreateDetailedErrorContent

func CreateDetailedErrorContent(err error, title string, code int) *ErrorPageContent

CreateDetailedErrorContent makes a more detailed error page with fully custom content

func CreateSimpleErrorContent

func CreateSimpleErrorContent(statusCode int) *ErrorPageContent

CreateSimpleErrorContent makes a simpler and more common version of the above constants. This is used for status codes that are not as common as the ones above.

type JSON

type JSON = map[string]interface{}

JSON is a type alias for defining the json format. Only used to clear up some code!

Jump to

Keyboard shortcuts

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