Categorygithub.com/NaturalSelectionLabs/goapi
modulepackage
0.9.3
Repository: https://github.com/naturalselectionlabs/goapi.git
Documentation: pkg.go.dev

# 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

Hello world example: main.go.

RPC style example: main.go.

Check the examples folder.

Read the tests for details.

Benchmark

Without any optimization, goapi is about 7% slower than echo for the simplest usage. This benchmark is only for avoiding drastic performance changes, the real performance depends on the complexity of the service.

go test -bench=. -benchmem ./lib/bench
goos: darwin
goarch: arm64
pkg: github.com/NaturalSelectionLabs/goapi/lib/bench
Benchmark_goapi-12         34472             33856 ns/op            8448 B/op        114 allocs/op
Benchmark_echo-12          36729             31175 ns/op            6776 B/op         82 allocs/op
PASS
ok      github.com/NaturalSelectionLabs/goapi/lib/bench 5.711s

# Packages

No description provided by the author

# Functions

Add is a shortcut for [Group.Add] for a random golang function.
AddInterfaces to the global interface set.
Interface create a interface set of i.
New is a shortcut for: NewRouter().Group("").
NewRouter creates a new router.

# Constants

TagResponse is one tag name for Data field.
TagResponseDirect is one value for [TagResponse].

# Variables

Interfaces is the global interface set.

# Structs

Group of handlers with the same url path prefix.
InHeader is a flag that can be embedded into a struct to mark it as a container for request header parameters.
InURL is a flag that can be embedded into a struct to mark it as a container for request url parameters.
Operation is a handler for a specific HTTP method and path.
Path helps to handle openapi path pattern.
Router for routing http requests to handlers.
StatusAccepted represents http status code 202.
StatusAlreadyReported represents http status code 208.
StatusBadGateway represents http status code 502.
StatusBadRequest represents http status code 400.
StatusConflict represents http status code 409.
StatusContinue represents http status code 100.
StatusCreated represents http status code 201.
StatusEarlyHints represents http status code 103.
StatusExpectationFailed represents http status code 417.
StatusFailedDependency represents http status code 424.
StatusForbidden represents http status code 403.
StatusFound represents http status code 302.
StatusGatewayTimeout represents http status code 504.
StatusGone represents http status code 410.
StatusHTTPVersionNotSupported represents http status code 505.
StatusIMUsed represents http status code 226.
StatusInsufficientStorage represents http status code 507.
StatusInternalServerError represents http status code 500.
StatusLengthRequired represents http status code 411.
StatusLocked represents http status code 423.
StatusLoopDetected represents http status code 508.
StatusMethodNotAllowed represents http status code 405.
StatusMisdirectedRequest represents http status code 421.
StatusMovedPermanently represents http status code 301.
StatusMultipleChoices represents http status code 300.
StatusMultiStatus represents http status code 207.
StatusNetworkAuthenticationRequired represents http status code 511.
StatusNoContent represents http status code 204.
StatusNonAuthoritativeInfo represents http status code 203.
StatusNotAcceptable represents http status code 406.
StatusNotExtended represents http status code 510.
StatusNotFound represents http status code 404.
StatusNotImplemented represents http status code 501.
StatusNotModified represents http status code 304.
StatusOK represents http status code 200.
StatusPartialContent represents http status code 206.
StatusPaymentRequired represents http status code 402.
StatusPermanentRedirect represents http status code 308.
StatusPreconditionFailed represents http status code 412.
StatusPreconditionRequired represents http status code 428.
StatusProcessing represents http status code 102.
StatusProxyAuthRequired represents http status code 407.
StatusRequestedRangeNotSatisfiable represents http status code 416.
StatusRequestEntityTooLarge represents http status code 413.
StatusRequestHeaderFieldsTooLarge represents http status code 431.
StatusRequestTimeout represents http status code 408.
StatusRequestURITooLong represents http status code 414.
StatusResetContent represents http status code 205.
StatusSeeOther represents http status code 303.
StatusServiceUnavailable represents http status code 503.
StatusSwitchingProtocols represents http status code 101.
StatusTeapot represents http status code 418.
StatusTemporaryRedirect represents http status code 307.
StatusTooEarly represents http status code 425.
StatusTooManyRequests represents http status code 429.
StatusUnauthorized represents http status code 401.
StatusUnavailableForLegalReasons represents http status code 451.
StatusUnprocessableEntity represents http status code 422.
StatusUnsupportedMediaType represents http status code 415.
StatusUpgradeRequired represents http status code 426.
StatusUseProxy represents http status code 305.
StatusVariantAlsoNegotiates represents http status code 506.

# Interfaces

ContentTyper is an interface that is use to specify the http Content-Type in openapi.
Descriptioner is an interface that is use to specify the description in openapi.
Response is an interface that represents a response object.

# Type aliases

ConfigOpenAPI is a function to modify the generated OpenAPI doc.
DataStream is a flag for binary response body.
OperationHandler is a function to handle input and output of a http operation.