goapi

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 14 Imported by: 2

README

Overview

A lightweight opinionated router to bridge function call and http api. It helps to standardize the way to define input and output of a service.

Features

  • Auto generate openapi doc from code reflection
  • Use function signature to define input and output
  • Type safe without struct tags

Usage

Check the examples folder.

Read the tests for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingParam = errors.New("missing parameter in request")

Functions

func Vary

func Vary(i any) *vary.Interface

Types

type ConfigOperation

type ConfigOperation func(op *Operation)

type Description

type Description interface {
	Description() string
}

type Error

type Error struct {
	// Code is a machine-readable error code.
	Code string `json:"code,omitempty"`
	// Message is a human-readable error message.
	Message string `json:"message,omitempty"`
	// Target is a human-readable description of the target of the error.
	Target string `json:"target,omitempty"`
	// Details is an array of structured error details objects.
	Details []Error `json:"details,omitempty"`
	// InnerError is a generic error object that is used by the service developer for debugging.
	InnerError any `json:"innererror,omitempty"`
}

Error is an error object that contains information about a failed request. Reference: https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md#error--object

type Group

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

func New

func New() *Group

New is a shortcut for:

NewRouter().Group("")

func (*Group) Add

func (g *Group) Add(
	method openapi.Method, path string, handler any, opts ...ConfigOperation,
)

func (*Group) DELETE

func (g *Group) DELETE(path string, handler any, opts ...ConfigOperation)

func (*Group) GET

func (g *Group) GET(path string, handler any, opts ...ConfigOperation)

func (*Group) Group

func (g *Group) Group(prefix string) *Group

Group creates a sub group of current group.

func (*Group) HEAD

func (g *Group) HEAD(path string, handler any, opts ...ConfigOperation)

func (*Group) Meta

func (g *Group) Meta(meta OperationMeta) ConfigOperation

Meta is a type of option for Group.Add to set the meta info of an operation.

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, handler any, opts ...ConfigOperation)

func (*Group) OpenAPI

func (r *Group) OpenAPI(schemas *jschema.Schemas) *openapi.Document

func (*Group) PATCH

func (g *Group) PATCH(path string, handler any, opts ...ConfigOperation)

func (*Group) POST

func (g *Group) POST(path string, handler any, opts ...ConfigOperation)

func (*Group) PUT

func (g *Group) PUT(path string, handler any, opts ...ConfigOperation)

func (*Group) Server

func (g *Group) Server() http.Handler

Handler is a shortcut for Router.Handler.

func (*Group) Use

func (g *Group) Use(m Middleware)

Use is a shortcut for Router.Use.

type InBody

type InBody struct{}

type InHeader

type InHeader struct{}

type InURL

type InURL struct{}

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

func (MiddlewareFunc) Handler

func (fn MiddlewareFunc) Handler(h http.Handler) http.Handler

type Operation

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

Operation is a handler for a specific HTTP method and path. We use reflection to constrain the handler function signature, to make it follow the openapi spec.

func (*Operation) Handler

func (op *Operation) Handler(next http.Handler) http.Handler

type OperationMeta

type OperationMeta struct {
	// Summary is used for display in the openapi UI.
	Summary string
	// Description is used for display in the openapi UI.
	Description string
	// OperationID is a unique string used to identify an individual operation.
	// This can be used by tools and libraries to provide functionality for
	// referencing and calling the operation from different parts of your application.
	OperationID string
	// Tags are used for grouping operations together for display in the openapi UI.
	Tags []string
}

type Params

type Params interface {
	// contains filtered or unexported methods
}

type Path

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

Path helps to handle openapi path pattern.

type Response

type Response interface {
	// contains filtered or unexported methods
}

type Router

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

Router itself is a middleware.

func NewRouter

func NewRouter() *Router

func (*Router) Group

func (r *Router) Group(prefix string) *Group

Group creates a new group with the given prefix.

func (*Router) Handler

func (r *Router) Handler(h http.Handler) http.Handler

func (*Router) OpenAPI

func (r *Router) OpenAPI(schemas *jschema.Schemas) *openapi.Document

OpenAPI returns the OpenAPI doc of the router. You can use json.Marshal to convert it to a JSON string.

func (*Router) Server

func (r *Router) Server() http.Handler

func (*Router) Use

func (r *Router) Use(middlewares ...Middleware)

Use a middleware to the router.

type StatusAccepted

type StatusAccepted struct{}

type StatusAlreadyReported

type StatusAlreadyReported struct{}

type StatusBadGateway

type StatusBadGateway struct{}

type StatusBadRequest

type StatusBadRequest struct{}

type StatusConflict

type StatusConflict struct{}

type StatusContinue

type StatusContinue struct{}

type StatusCreated

type StatusCreated struct{}

type StatusEarlyHints

type StatusEarlyHints struct{}

type StatusExpectationFailed

type StatusExpectationFailed struct{}

type StatusFailedDependency

type StatusFailedDependency struct{}

type StatusForbidden

type StatusForbidden struct{}

type StatusFound

type StatusFound struct{}

type StatusGatewayTimeout

type StatusGatewayTimeout struct{}

type StatusGone

type StatusGone struct{}

type StatusHTTPVersionNotSupported

type StatusHTTPVersionNotSupported struct{}

type StatusIMUsed

type StatusIMUsed struct{}

type StatusInsufficientStorage

type StatusInsufficientStorage struct{}

type StatusInternalServerError

type StatusInternalServerError struct{}

type StatusLengthRequired

type StatusLengthRequired struct{}

type StatusLocked

type StatusLocked struct{}

type StatusLoopDetected

type StatusLoopDetected struct{}

type StatusMethodNotAllowed

type StatusMethodNotAllowed struct{}

type StatusMisdirectedRequest

type StatusMisdirectedRequest struct{}

type StatusMovedPermanently

type StatusMovedPermanently struct{}

type StatusMultiStatus

type StatusMultiStatus struct{}

type StatusMultipleChoices

type StatusMultipleChoices struct{}

type StatusNetworkAuthenticationRequired

type StatusNetworkAuthenticationRequired struct{}

type StatusNoContent

type StatusNoContent struct{}

type StatusNonAuthoritativeInfo

type StatusNonAuthoritativeInfo struct{}

type StatusNotAcceptable

type StatusNotAcceptable struct{}

type StatusNotExtended

type StatusNotExtended struct{}

type StatusNotFound

type StatusNotFound struct{}

type StatusNotImplemented

type StatusNotImplemented struct{}

type StatusNotModified

type StatusNotModified struct{}

type StatusOK

type StatusOK struct{}

type StatusPartialContent

type StatusPartialContent struct{}

type StatusPaymentRequired

type StatusPaymentRequired struct{}

type StatusPermanentRedirect

type StatusPermanentRedirect struct{}

type StatusPreconditionFailed

type StatusPreconditionFailed struct{}

type StatusPreconditionRequired

type StatusPreconditionRequired struct{}

type StatusProcessing

type StatusProcessing struct{}

type StatusProxyAuthRequired

type StatusProxyAuthRequired struct{}

type StatusRequestEntityTooLarge

type StatusRequestEntityTooLarge struct{}

type StatusRequestHeaderFieldsTooLarge

type StatusRequestHeaderFieldsTooLarge struct{}

type StatusRequestTimeout

type StatusRequestTimeout struct{}

type StatusRequestURITooLong

type StatusRequestURITooLong struct{}

type StatusRequestedRangeNotSatisfiable

type StatusRequestedRangeNotSatisfiable struct{}

type StatusResetContent

type StatusResetContent struct{}

type StatusSeeOther

type StatusSeeOther struct{}

type StatusServiceUnavailable

type StatusServiceUnavailable struct{}

type StatusSwitchingProtocols

type StatusSwitchingProtocols struct{}

type StatusTeapot

type StatusTeapot struct{}

type StatusTemporaryRedirect

type StatusTemporaryRedirect struct{}

type StatusTooEarly

type StatusTooEarly struct{}

type StatusTooManyRequests

type StatusTooManyRequests struct{}

type StatusUnauthorized

type StatusUnauthorized struct{}

type StatusUnavailableForLegalReasons

type StatusUnavailableForLegalReasons struct{}

type StatusUnprocessableEntity

type StatusUnprocessableEntity struct{}

type StatusUnsupportedMediaType

type StatusUnsupportedMediaType struct{}

type StatusUpgradeRequired

type StatusUpgradeRequired struct{}

type StatusUseProxy

type StatusUseProxy struct{}

type StatusVariantAlsoNegotiates

type StatusVariantAlsoNegotiates struct{}

Directories

Path Synopsis
lib
examples/basic command
gen-status-code command

Jump to

Keyboard shortcuts

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