lever

package module
v0.0.0-...-3df29f9 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 26 Imported by: 0

README

lever

A Go framework that supersets Iris and bring Go web development to the next level

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MultiErrorsSlice

func MultiErrorsSlice(err error) []string

MultiErrorsSlice returns a representation of the stacked errors as a slice of string. It is made to generate a slice of string from an error either it is a basic error or a MultiErrors.

MultiErrorsSlice is usefull when returning a list of error in the API response without relying on the MultiErrorResponse response format. Typically, it can be used to generate a list of validation error in a Problem response (rfc 7807 : https://datatracker.ietf.org/doc/html/rfc7807)

func NewDirFS

func NewDirFS(rootDir string, fsys fs.FS) *dirFS

func NewPostgresConnection

func NewPostgresConnection(param *ConnectionParams) *sql.DB

NewPostgresConnection creates a new sql.DB compatible Postgres connection After having opened the connection, NewPostgresConnection tries to ping the database and wait for a timeout.

NewPostgresConnection uses *ConnectionParams to open the connection.

func UseSchema

func UseSchema(schema string) iris.Handler

UseSchema is a per-route middleware that validates the input payload following the provided Json Schema The aim of this middleware is to avoid data basic validation in your own middleware.

example :

 app.Post("/entity", lever.UseSchema(`{"type": "object"}`), postEntityHandler)
 ...
 func postEntityHandler(ctx iris.Context) {
	 ctx.Application().Logger().Info("payload is valid")
	 //put your business logic here
 }

Types

type App

type App struct {
	*iris.Application
	// contains filtered or unexported fields
}

func NewApp

func NewApp(options *AppOptions) *App

NewApp creates a new App with the given options

func NewDefaultApp

func NewDefaultApp() *App

NewDefaultApp creates a new App with the following default parameters * CORS are disabled

func (*App) Run

func (app *App) Run()

Run runs the app with TLS (https). Serving http request through https is the default mode. When the app is ran locally or on a remote server that doesn't have a public domain name, a certificate is generated automatically

func (*App) RunNoTLS

func (app *App) RunNoTLS(hostPort string)

RunNoTLS runs the app on http, without TLS enable. This mode should be only used in particular contexts such as running the app on a server that doesn't have any domain name or if the incoming http requests are catch by a proxy which already handle TLS.

type AppOptions

type AppOptions struct {
	Cors bool
}

AppOptions handles the App configuration. AppOptions should be used when creating a new App with custom parameters like * Cors enabled/disabled

type ConnectionParams

type ConnectionParams struct {
	Host     string
	Port     string
	User     string
	Password string
	DBName   string
	TimeOut  time.Duration
}

ConnectionParams represents the database connection parameters The following parameters are needed : Host, Port, User, Password, DBName

type MultiErrorResponse

type MultiErrorResponse struct {
	Err []string `json:"errors,omitempty"`
}

MultiErrorResponse represents a typical api response in case of error. In this case, the response looks like :

 {
	 "errors": [
	 	"first error",
	 	"second error"
	 ]
 }

and should be associated with the right status code (ex : 400, bad request)

func MultiErrorsJSON

func MultiErrorsJSON(err error) MultiErrorResponse

MultiErrorsJSON generates a MultiErrorResponse ready to be marshalled as JSON It should typically be used when an API returns a error code (ex : 400 bad request) to list all the errors in the server response.

MultiErrorsJSON supports both error and MultiErrors type. Thus, it can be used right before returning the HTTP response without any consideration of either there are multiple errors to return or not. Example : err := Validate(myVar) ctx.StopWithJSON(400, lever.MultiErrorsJSON(err))

type MultiErrors

type MultiErrors struct {
	// contains filtered or unexported fields
}

MultiErrors helps handling multiple errors. It aims to be used especially when validating some input that may contains multiple errors One may choose to return an error (bad request) when the first validation error occurs. But Multierrors helps stacking the validation errors and return them all at once.

func NewMultiErrors

func NewMultiErrors() *MultiErrors

NewMultiErrors creates a Multierrors

func (*MultiErrors) Append

func (merr *MultiErrors) Append(errs ...error) *MultiErrors

Append append a new error to the error stack already contained in the Multierror. A typical use of the Append function may look likes this : errs := lever.NewMultiErrors().Append(errors.New("an error"))

func (*MultiErrors) Error

func (merr *MultiErrors) Error() string

Error return a concatenation of the error stack as a string.

func (*MultiErrors) ListAsString

func (merr *MultiErrors) ListAsString() []string

ListAsString returns the list of stacked error as a slice of strings.

type WebApp

type WebApp struct {
	*App
	// contains filtered or unexported fields
}

func NewDefaultWebApp

func NewDefaultWebApp(assetsFS *dirFS, templatesFS *dirFS) *WebApp

NewDefaultWebApp creates a new WebApp with the following default parameters * CORS are disabled * Assets are served on the default /assets path

func NewWebApp

func NewWebApp(assetsFS *dirFS, templatesFS *dirFS, options *WebAppOptions) *WebApp

NewWebApp creates a new WebApp with the given options

func (*WebApp) FuncMap

func (wa *WebApp) FuncMap() template.FuncMap

type WebAppOptions

type WebAppOptions struct {
	AppOptions
	// contains filtered or unexported fields
}

WebAppOptions handles the WebApp configuration. WebAppOptions should be used when creating a new WebApp with custom parameters.

Directories

Path Synopsis
doc

Jump to

Keyboard shortcuts

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