lite

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT Imports: 21 Imported by: 0

README

Lite: A Typed Wrapper for GoFiber

Lite is a typed wrapper for GoFiber, a web framework for Go. It is designed to be lightweight and easy to use, while still providing a powerful API for building web applications. Lite is built on top of GoFiber, so it inherits all of its features and performance benefits.

Features

  • Typed Requests: Define request types to ensure correct data handling.
  • Typed Responses: Define response types to ensure correct data serialization.
  • Error Handling: Simplify error management with typed responses.
  • Middleware: Use middleware to add functionality to your routes.
  • OpenAPI Specification: Generate OpenAPI specs from your routes.

Installation

To install Lite, use go get:

go get github.com/disco07/lite

Usage

Here is a simple example of how to use Lite:

package main

import (
	"github.com/disco07/lite"
	"log"
)

type Response struct {
	Message string `json:"message"`
}

func main() {
	app := lite.New()

	lite.Get(app, "/", func(c *lite.ContextNoRequest) (Response, error) {
		return Response{Message: "Hello, world!"}, nil
	})

	log.Fatal(app.Listen(":3000"))
}

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

Lite is licensed under the MIT License. See LICENSE for more information. []: # (END)

Documentation

Index

Constants

View Source
const (
	StatusContinue           = 100 // RFC 7231, 6.2.1
	StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
	StatusProcessing         = 102 // RFC 2518, 10.1
	StatusEarlyHints         = 103 // RFC 8297

	StatusOK                   = 200 // RFC 7231, 6.3.1
	StatusCreated              = 201 // RFC 7231, 6.3.2
	StatusAccepted             = 202 // RFC 7231, 6.3.3
	StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4
	StatusNoContent            = 204 // RFC 7231, 6.3.5
	StatusResetContent         = 205 // RFC 7231, 6.3.6
	StatusPartialContent       = 206 // RFC 7233, 4.1
	StatusMultiStatus          = 207 // RFC 4918, 11.1
	StatusAlreadyReported      = 208 // RFC 5842, 7.1
	StatusIMUsed               = 226 // RFC 3229, 10.4.1

	StatusMultipleChoices  = 300 // RFC 7231, 6.4.1
	StatusMovedPermanently = 301 // RFC 7231, 6.4.2
	StatusFound            = 302 // RFC 7231, 6.4.3
	StatusSeeOther         = 303 // RFC 7231, 6.4.4
	StatusNotModified      = 304 // RFC 7232, 4.1
	StatusUseProxy         = 305 // RFC 7231, 6.4.5

	StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
	StatusPermanentRedirect = 308 // RFC 7538, 3

	StatusBadRequest                   = 400 // RFC 7231, 6.5.1
	StatusUnauthorized                 = 401 // RFC 7235, 3.1
	StatusPaymentRequired              = 402 // RFC 7231, 6.5.2
	StatusForbidden                    = 403 // RFC 7231, 6.5.3
	StatusNotFound                     = 404 // RFC 7231, 6.5.4
	StatusMethodNotAllowed             = 405 // RFC 7231, 6.5.5
	StatusNotAcceptable                = 406 // RFC 7231, 6.5.6
	StatusProxyAuthRequired            = 407 // RFC 7235, 3.2
	StatusRequestTimeout               = 408 // RFC 7231, 6.5.7
	StatusConflict                     = 409 // RFC 7231, 6.5.8
	StatusGone                         = 410 // RFC 7231, 6.5.9
	StatusLengthRequired               = 411 // RFC 7231, 6.5.10
	StatusPreconditionFailed           = 412 // RFC 7232, 4.2
	StatusRequestEntityTooLarge        = 413 // RFC 7231, 6.5.11
	StatusRequestURITooLong            = 414 // RFC 7231, 6.5.12
	StatusUnsupportedMediaType         = 415 // RFC 7231, 6.5.13
	StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4
	StatusExpectationFailed            = 417 // RFC 7231, 6.5.14
	StatusTeapot                       = 418 // RFC 7168, 2.3.3
	StatusMisdirectedRequest           = 421 // RFC 7540, 9.1.2
	StatusUnprocessableEntity          = 422 // RFC 4918, 11.2
	StatusLocked                       = 423 // RFC 4918, 11.3
	StatusFailedDependency             = 424 // RFC 4918, 11.4
	StatusUpgradeRequired              = 426 // RFC 7231, 6.5.15
	StatusPreconditionRequired         = 428 // RFC 6585, 3
	StatusTooManyRequests              = 429 // RFC 6585, 4
	StatusRequestHeaderFieldsTooLarge  = 431 // RFC 6585, 5
	StatusUnavailableForLegalReasons   = 451 // RFC 7725, 3

	StatusInternalServerError           = 500 // RFC 7231, 6.6.1
	StatusNotImplemented                = 501 // RFC 7231, 6.6.2
	StatusBadGateway                    = 502 // RFC 7231, 6.6.3
	StatusServiceUnavailable            = 503 // RFC 7231, 6.6.4
	StatusGatewayTimeout                = 504 // RFC 7231, 6.6.5
	StatusHTTPVersionNotSupported       = 505 // RFC 7231, 6.6.6
	StatusVariantAlsoNegotiates         = 506 // RFC 2295, 8.1
	StatusInsufficientStorage           = 507 // RFC 4918, 11.5
	StatusLoopDetected                  = 508 // RFC 5842, 7.2
	StatusNotExtended                   = 510 // RFC 2774, 7
	StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6
)

HTTP status codes were stolen from net/http.

Variables

This section is empty.

Functions

func DefaultOpenAPIHandler

func DefaultOpenAPIHandler(specURL string) fiber.Handler

func NewOpenAPISpec

func NewOpenAPISpec() openapi3.T

func StatusMessage

func StatusMessage(statusCode int) string

StatusMessage returns HTTP status message for the given status code.

Types

type App

type App struct {
	*fiber.App

	OpenAPISpec   openapi3.T
	OpenAPIConfig OpenAPIConfig

	Serializer func(ctx *fasthttp.RequestCtx, response any) error
	// contains filtered or unexported fields
}

func New

func New() *App

func (*App) AddServer

func (s *App) AddServer(url, description string)

AddServer adds a server to the OpenAPI spec

func (*App) AddTags

func (s *App) AddTags(tags ...string) *App

AddTags adds tags from the Server (i.e Group) Tags from the parent Groups will be respected

func (*App) Description

func (s *App) Description(description string) *App

Description sets the description of the OpenAPI spec

func (*App) Listen

func (s *App) Listen(address string) error

func (*App) SaveOpenAPISpec

func (s *App) SaveOpenAPISpec() ([]byte, error)

SaveOpenAPISpec saves the OpenAPI spec to a file in YAML format

func (*App) Shutdown

func (s *App) Shutdown() error

func (*App) Title

func (s *App) Title(title string) *App

Title sets the title of the OpenAPI spec

func (*App) Version

func (s *App) Version(version string) *App

Version sets the version of the OpenAPI spec

type Context

type Context[Request any] interface {
	Context() context.Context
	Requests() (Request, error)
	Accepts(offers ...string) string
	AcceptsCharsets(offers ...string) string
	AcceptsEncodings(offers ...string) string
	AcceptsLanguages(offers ...string) string
	App() *App
	Append(field string, values ...string)
	Attachment(filename ...string)
	BaseURL() string
	BodyRaw() []byte
	ClearCookie(key ...string)
	RequestContext() *fasthttp.RequestCtx
	SetUserContext(ctx context.Context)
	Cookie(cookie *fiber.Cookie)
	Cookies(key string, defaultValue ...string) string
	Download(file string, filename ...string) error
	Request() *fasthttp.Request
	Response() *fasthttp.Response
	Format(body interface{}) error
	FormFile(key string) (*multipart.FileHeader, error)
	FormValue(key string, defaultValue ...string) string
	Fresh() bool
	Get(key string, defaultValue ...string) string
	GetReqHeaders() map[string][]string
	GetRespHeaders() map[string][]string
	Hostname() string
	Port() string
	IP() string
	IPs() []string
	Is(extension string) bool
	Links(link ...string)
	Locals(key interface{}, value ...interface{}) interface{}
	Location(path string)
	Method(override ...string) string
	MultipartForm() (*multipart.Form, error)
	ClientHelloInfo() *tls.ClientHelloInfo
	Next() error
	RestartRouting() error
	OriginalURL() string
	AllParams() map[string]string
	Path(override ...string) string
	Protocol() string
	Queries() map[string]string
	Range(size int) (fiber.Range, error)
	Redirect(location string, status ...int) error
	Bind(vars fiber.Map) error
	GetRouteURL(routeName string, params fiber.Map) (string, error)
	RedirectToRoute(routeName string, params fiber.Map, status ...int) error
	RedirectBack(fallback string, status ...int) error
	Render(name string, bind interface{}, layouts ...string) error
	SaveFile(fileheader *multipart.FileHeader, path string) error
	SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage fiber.Storage) error
	Secure() bool
	Set(key string, val string)
	Subdomains(offset ...int) []string
	Stale() bool
	Status(status int) Context[Request]
	String() string
	// Type sets the Content-Type response header with the given type and charset.
	Type(extension string, charset ...string) Context[Request]
	Vary(fields ...string)
	XHR() bool
	IsProxyTrusted() bool
	IsFromLocal() bool
}

type ContextNoRequest

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

func (*ContextNoRequest) Accepts

func (c *ContextNoRequest) Accepts(offers ...string) string

func (*ContextNoRequest) AcceptsCharsets

func (c *ContextNoRequest) AcceptsCharsets(offers ...string) string

func (*ContextNoRequest) AcceptsEncodings

func (c *ContextNoRequest) AcceptsEncodings(offers ...string) string

func (*ContextNoRequest) AcceptsLanguages

func (c *ContextNoRequest) AcceptsLanguages(offers ...string) string

func (*ContextNoRequest) AllParams

func (c *ContextNoRequest) AllParams() map[string]string

func (*ContextNoRequest) App

func (c *ContextNoRequest) App() *App

func (*ContextNoRequest) Append

func (c *ContextNoRequest) Append(field string, values ...string)

func (*ContextNoRequest) Attachment

func (c *ContextNoRequest) Attachment(filename ...string)

func (*ContextNoRequest) BaseURL

func (c *ContextNoRequest) BaseURL() string

func (*ContextNoRequest) Bind

func (c *ContextNoRequest) Bind(vars fiber.Map) error

func (*ContextNoRequest) BodyRaw

func (c *ContextNoRequest) BodyRaw() []byte

func (*ContextNoRequest) ClearCookie

func (c *ContextNoRequest) ClearCookie(key ...string)

func (*ContextNoRequest) ClientHelloInfo

func (c *ContextNoRequest) ClientHelloInfo() *tls.ClientHelloInfo

func (*ContextNoRequest) Context

func (c *ContextNoRequest) Context() context.Context

func (*ContextNoRequest) Cookie

func (c *ContextNoRequest) Cookie(cookie *fiber.Cookie)

func (*ContextNoRequest) Cookies

func (c *ContextNoRequest) Cookies(key string, defaultValue ...string) string

func (*ContextNoRequest) Download

func (c *ContextNoRequest) Download(file string, filename ...string) error

func (*ContextNoRequest) FormFile

func (c *ContextNoRequest) FormFile(key string) (*multipart.FileHeader, error)

func (*ContextNoRequest) FormValue

func (c *ContextNoRequest) FormValue(key string, defaultValue ...string) string

func (*ContextNoRequest) Format

func (c *ContextNoRequest) Format(body interface{}) error

func (*ContextNoRequest) Fresh

func (c *ContextNoRequest) Fresh() bool

func (*ContextNoRequest) Get

func (c *ContextNoRequest) Get(key string, defaultValue ...string) string

func (*ContextNoRequest) GetReqHeaders

func (c *ContextNoRequest) GetReqHeaders() map[string][]string

func (*ContextNoRequest) GetRespHeaders

func (c *ContextNoRequest) GetRespHeaders() map[string][]string

func (*ContextNoRequest) GetRouteURL

func (c *ContextNoRequest) GetRouteURL(routeName string, params fiber.Map) (string, error)

func (*ContextNoRequest) Hostname

func (c *ContextNoRequest) Hostname() string

func (*ContextNoRequest) IP

func (c *ContextNoRequest) IP() string

func (*ContextNoRequest) IPs

func (c *ContextNoRequest) IPs() []string

func (*ContextNoRequest) Is

func (c *ContextNoRequest) Is(extension string) bool

func (*ContextNoRequest) IsFromLocal

func (c *ContextNoRequest) IsFromLocal() bool

func (*ContextNoRequest) IsProxyTrusted

func (c *ContextNoRequest) IsProxyTrusted() bool
func (c *ContextNoRequest) Links(link ...string)

func (*ContextNoRequest) Locals

func (c *ContextNoRequest) Locals(key interface{}, value ...interface{}) interface{}

func (*ContextNoRequest) Location

func (c *ContextNoRequest) Location(path string)

func (*ContextNoRequest) Method

func (c *ContextNoRequest) Method(override ...string) string

func (*ContextNoRequest) MultipartForm

func (c *ContextNoRequest) MultipartForm() (*multipart.Form, error)

func (*ContextNoRequest) Next

func (c *ContextNoRequest) Next() error

func (*ContextNoRequest) OriginalURL

func (c *ContextNoRequest) OriginalURL() string

func (*ContextNoRequest) Params

func (c *ContextNoRequest) Params(key string, defaultValue ...string) string

func (*ContextNoRequest) Path

func (c *ContextNoRequest) Path(override ...string) string

func (*ContextNoRequest) Port

func (c *ContextNoRequest) Port() string

func (*ContextNoRequest) Protocol

func (c *ContextNoRequest) Protocol() string

func (*ContextNoRequest) Queries

func (c *ContextNoRequest) Queries() map[string]string

func (*ContextNoRequest) QueryInt

func (c *ContextNoRequest) QueryInt(key string, defaultValue ...int) int

func (*ContextNoRequest) Range

func (c *ContextNoRequest) Range(size int) (fiber.Range, error)

func (*ContextNoRequest) Redirect

func (c *ContextNoRequest) Redirect(location string, status ...int) error

func (*ContextNoRequest) RedirectBack

func (c *ContextNoRequest) RedirectBack(fallback string, status ...int) error

func (*ContextNoRequest) RedirectToRoute

func (c *ContextNoRequest) RedirectToRoute(routeName string, params fiber.Map, status ...int) error

func (*ContextNoRequest) Render

func (c *ContextNoRequest) Render(name string, bind interface{}, layouts ...string) error

func (*ContextNoRequest) Request

func (c *ContextNoRequest) Request() *fasthttp.Request

func (*ContextNoRequest) RequestContext

func (c *ContextNoRequest) RequestContext() *fasthttp.RequestCtx

func (*ContextNoRequest) Requests

func (c *ContextNoRequest) Requests() (any, error)

func (*ContextNoRequest) Response

func (c *ContextNoRequest) Response() *fasthttp.Response

func (*ContextNoRequest) RestartRouting

func (c *ContextNoRequest) RestartRouting() error

func (*ContextNoRequest) SaveFile

func (c *ContextNoRequest) SaveFile(file *multipart.FileHeader, path string) error

func (*ContextNoRequest) SaveFileToStorage

func (c *ContextNoRequest) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage fiber.Storage) error

func (*ContextNoRequest) Secure

func (c *ContextNoRequest) Secure() bool

func (*ContextNoRequest) Set

func (c *ContextNoRequest) Set(key string, val string)

Set sets the response's HTTP header field to the specified key, value.

func (*ContextNoRequest) SetUserContext

func (c *ContextNoRequest) SetUserContext(ctx context.Context)

func (*ContextNoRequest) Stale

func (c *ContextNoRequest) Stale() bool

func (*ContextNoRequest) Status

func (c *ContextNoRequest) Status(status int) Context[any]

func (*ContextNoRequest) String

func (c *ContextNoRequest) String() string

func (*ContextNoRequest) Subdomains

func (c *ContextNoRequest) Subdomains(offset ...int) []string

func (*ContextNoRequest) Type

func (c *ContextNoRequest) Type(extension string, charset ...string) Context[any]

func (*ContextNoRequest) Vary

func (c *ContextNoRequest) Vary(fields ...string)

func (*ContextNoRequest) XHR

func (c *ContextNoRequest) XHR() bool

type ContextWithRequest

type ContextWithRequest[Request any] struct {
	ContextNoRequest
}

func (*ContextWithRequest[Request]) Requests

func (c *ContextWithRequest[Request]) Requests() (Request, error)

func (*ContextWithRequest[Request]) Status

func (c *ContextWithRequest[Request]) Status(status int) Context[Request]

func (*ContextWithRequest[Request]) Type

func (c *ContextWithRequest[Request]) Type(extension string, charset ...string) Context[Request]

type OpenAPIConfig

type OpenAPIConfig struct {
	DisableSwagger   bool                               // If true, the server will not serve the swagger ui nor the openapi json spec
	DisableLocalSave bool                               // If true, the server will not save the openapi json spec locally
	SwaggerURL       string                             // URL to serve the swagger ui
	UIHandler        func(specURL string) fiber.Handler // Handler to serve the openapi ui from spec url
	YamlURL          string                             // Local path to save the openapi json spec
}

type Route

type Route[T, B any] struct {
	// contains filtered or unexported fields
}

func Connect

func Connect[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Delete

func Delete[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Get

func Get[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]
func Head[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Options

func Options[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Patch

func Patch[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Post

func Post[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Put

func Put[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func Trace

func Trace[ResponseBody, Request any, Contexted Context[Request]](
	app *App,
	path string,
	controller func(Contexted) (ResponseBody, error),
	middleware ...fiber.Handler,
) Route[ResponseBody, Request]

func (Route[ResponseBody, Request]) AddTags

func (r Route[ResponseBody, Request]) AddTags(tags ...string) Route[ResponseBody, Request]

func (Route[ResponseBody, Request]) Deprecated

func (r Route[ResponseBody, Request]) Deprecated() Route[ResponseBody, Request]

func (Route[ResponseBody, Request]) Description

func (r Route[ResponseBody, Request]) Description(description string) Route[ResponseBody, Request]

func (Route[ResponseBody, Request]) OperationID

func (r Route[ResponseBody, Request]) OperationID(operationID string) Route[ResponseBody, Request]

func (Route[ResponseBody, Request]) SetResponseContentType

func (r Route[ResponseBody, Request]) SetResponseContentType(contentType string) Route[ResponseBody, Request]

func (Route[ResponseBody, Request]) Summary

func (r Route[ResponseBody, Request]) Summary(summary string) Route[ResponseBody, Request]

Directories

Path Synopsis
examples
basic command
file command

Jump to

Keyboard shortcuts

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