httpzen

package module
v0.0.0-...-60c54c2 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: MIT Imports: 6 Imported by: 0

README

HTTPZen Logo

under development

HTTPZen

HTTPZen is a Go library that simplifies HTTP operations with an easy-to-use abstraction layer. Designed according to Zalando API guidelines, it enables high-quality, standardized APIs that are easy to maintain and scale.

Features

  • OpenAPI documentation: HTTPZen generates an OpenAPI web page that documents your API endpoints, without requiring any additional tools or setup.
  • Simplicity: HTTPZen provides a powerful and easy-to-use abstraction layer for working with HTTP and REST servers.
  • Standardization: Designed according to Zalando API guidelines, HTTPZen enables high-quality, standardized APIs that are easy to maintain and scale.
  • Flexibility: HTTPZen is designed to be flexible and customizable, allowing you to easily adapt it to your specific needs.
  • Performance: HTTPZen is built for performance, ensuring that your HTTP operations are fast and reliable.

Installation

To use HTTPZen in your Go project, simply run the following command:

go get github.com/nullexp/httpzen

Usage

Using HTTPZen is easy. For examples and documentation, please visit the HTTPZen Wiki.

Contributing

Contributions to HTTPZen are welcome and encouraged! To contribute code, documentation, or bug reports, please submit a pull request or issue to the GitHub repository.

License

HTTPZen is licensed under the MIT License. Please see the LICENSE file for more information.

TODO

  • Add abstraction
  • Implement gin
  • Implement echo
  • Expand test coverage
  • Add support for additional HTTP methods and headers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(req Request)

An Action is a function that handles an HTTP request.

type Api

type Api interface {
	Run(ip string, port uint, mode string) error
	AppendModule(mod Module)
	AppendMiddleware(string, Action) // TODO: add middleware base on order. study it!
	GetRoute(url, method string) *RequestDefinition
	SetCors(cors []string)

	// For testing
	TestHandle(*httptest.ResponseRecorder, *http.Request) error

	// OpenAPI
	SetExternalDocs(ExternalDocs)
	SetInfo(Info)
	SetContact(Contact)
	SetServers([]Server)
	EnableOpenApi(route string) error
	SetErrors([]string)
}

The Api interface defines the methods required to run an HTTP server and handle incoming requests.

func NewGinZen

func NewGinZen() Api

type Contact

type Contact struct {
	Name  string
	Email string
	URL   string
}

type DescriptionGetter

type DescriptionGetter interface {
	GetDescription() string
}

type ExternalDocs

type ExternalDocs struct {
	Description string
	URL         string
}

The name is plural by open api definition

type File

type File interface {
	io.Reader
	io.Closer
	GetFilename() string
	GetMimeType() string
	GetLastModifiedDate() time.Time
}

type FileHeader

type FileHeader interface {
	GetHeader() textproto.MIMEHeader
	GetSize() int64
	GetFilename() string
	OpenFile() (File, error)
}

type FileMultiaprt

type FileMultiaprt interface {
	File
	Multipart
}

type HTTPMethod

type HTTPMethod string

HTTPMethod is a string representing an HTTP method (GET, POST, DELETE, etc.).

type Info

type Info struct {
	Version     string
	Description string
	Title       string
}

type Module

type Module interface {
	GetRequestHandlers() []*RequestDefinition
	GetBaseURL() string
	GetTag() Tag
}

The Module interface defines the methods required to handle a group of related request handlers.

type Multipart

type Multipart interface {
	io.ReadCloser
	GetPartName() string
	GetMimeType() string
}

type MultipartDefinition

type MultipartDefinition interface {
	IsOptional() bool
	GetPartName() string
	IsSingle() bool
	GetObject() interface{}
}

type MultipartFileDefinition

type MultipartFileDefinition interface {
	MultipartDefinition
	Verify(FileHeader) error
}

type MultipartValueDefinition

type MultipartValueDefinition interface {
	MultipartDefinition
	Verify() error
}

type NameGetter

type NameGetter interface {
	GetName() string
}

type Request

type Request interface {
	// GetJson and other stuff
	SetServerError(msg string)
	SetForbidden()
	SetUnauthorized(msg string, code string)
	SetBadRequest(msg string, code string)
	SetNotFound(msg string, code string)
	ReturnStatus(int, error)
	Set(key string, value interface{})
	Get(key string) (interface{}, bool)
	MustGet(key string) interface{}
	GetDto() (interface{}, bool)
	MustGetDto() interface{}
	Negotiate(stausCode int, err error, Dto interface{})
	RangeFile(status int, err error, file SeekerFile)
	WriteFile(status int, err error, file File)
	ReturnMultpartMixed(status int, err error, out ...Multipart)
}

The Request interface defines the methods available to an HTTP request handler.

type RequestDefinition

type RequestDefinition struct {
	Route   string
	Dto     Verifier
	Handler Action
	Method  HTTPMethod

	// For open api
	Summary             string
	Description         string
	OperationID         string // Tools use this UNIQUE id to idenitfy the action example: GetUserByID,GetUsers, ....
	Deprecated          bool   // Set true if this definition is deprecated
	ResponseDefinitions []ResponseDefinition
}

A RequestDefinition contains information about an HTTP route, including the URL pattern, expected data types, and HTTP method.

type ResponseDefinition

type ResponseDefinition struct {
	Description string // For example: success operation
	Status      int
	Dto         any // Might be basic dto,
}

type SeekerFile

type SeekerFile interface {
	io.Seeker
	File
}

type Server

type Server struct {
	URL         string
	Description string
}

type Tag

type Tag struct {
	Name         string
	Description  string
	ExternalDocs *ExternalDocs
}

type Verifier

type Verifier interface {
	Verify() error
}

The Verifier interface defines the method required to validate input data.

Jump to

Keyboard shortcuts

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