Documentation ¶
Overview ¶
Package ehttp implements a HTTP RESTful API framework. It's make Web API Design easy, and auto-generate OpenAPI (formerly known as Swagger) documentation from the code.Base on gin (https://github.com/gin-gonic/gin) and swagger (https://swagger.io/).
ehttp has the following features:
- Keep advantages of gin (performance and good productivity).
- It's make Web API Design easy.
- Auto-generate OpenAPI (formerly known as Swagger) documentation from the code. (base on Package reflect).
- Auto-Check APIs document format.
- Check parameters Automatically when receiving an http request.
- Support plug-ins to be extended functionality.
Index ¶
- Constants
- type APIDoc
- type APIDocCommon
- type Config
- type Engine
- func (e *Engine) DELETE(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
- func (e *Engine) GET(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
- func (e Engine) GetSwaggerJSONDocument() (string, error)
- func (e Engine) GetSwaggerYAMLDocument() (string, error)
- func (e *Engine) GinEngine() *gin.Engine
- func (e *Engine) OPTIONS(relativePath string, handlers ...HandlerFunc) error
- func (e *Engine) PATCH(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
- func (e *Engine) POST(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
- func (e *Engine) PUT(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
- func (e *Engine) Run(addr ...string)
- func (e *Engine) SetGinEngine(router *gin.Engine)
- func (e *Engine) SetGlobalParameters(parameters map[string]Parameter) error
- type HandlerFunc
- type Parameter
- type Request
- type Response
- type Scheme
- type StructDoc
- type StructDocCreater
- type StructField
- type ValueInfo
Constants ¶
const ( GET = "GET" POST = "POST" PUT = "PUT" DELETE = "DELETE" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH" )
const ( InHeader = "header" InPath = "path" InQuery = "query" InFormData = "formData" InBody = "body" )
const ( Application_ActiveMessage = "application/activemessage" Application_AppleFile = "application/applefile" Application_AtomicMail = "application/atomicmail" Application_MsWord = "application/msword" Application_OctetStream = "application/octet-stream" Application_Oda = "application/oda" Application_Pdf = "application/pdf" Application_PostScript = "application/postscript" Application_Rtf = "application/rtf" Application_Xml = "application/xml" Application_Json = "application/json" Application_Json_utf8 = "application/json;charset=utf-8" Application_Zip = "application/zip" Application_Siren_Json = "application/vnd.siren+json" Application_Hal_Json = "application/hal+json" Audio_Xaiff = "audio/x-aiff" Audio_Xwav = "audio/x-wav" Image_Cgm = "image/cgm" Image_G3Fax = "image/g3fax" Image_Gif = "image/gif" Image_Ief = "image/ief" Image_Jpeg = "image/jpeg" Image_Naplps = "image/naplps" Image_Png = "image/png" Image_Tiff = "image/tiff" Multipart_Alternative = "multipart/alternative" Multipart_AppleDouble = "multipart/appledouble" Multipart_Digest = "multipart/digest" Multipart_FormData = "multipart/form-data" Multipart_HeaderSet = "multipart/header-set" Multipart_Mixed = "multipart/mixed" Multipart_Parallel = "multipart/parallel" Multipart_Related = "multipart/related" Multipart_Report = "multipart/report" Multipart_VoiceMessage = "multipart/voice-message" Text_Enriched = "text/enriched" Text_Html = "text/html" Text_Plain = "text/plain" Text_RichText = "text/richtext" Text_Sgml = "text/sgml" Text_TabSeparatedValues = "text/tab-separated-values" Text_Xml = "text/xml" Text_X_SeText = "text/x-setext" Video_Mpeg = "video/mpeg" Video_Quicktime = "video/quicktime" Video_VndVivo = "video/vnd.vivo" Video_VndMotorolaVideo = "video/vnd.motorola.video" Video_VndMotorolaVideoP = "video/vnd.motorola.videop" Video_X_MS_Video = "video/x-msvideo" Video_X_SgiMovie = "video/x-sgi-movie" )
const DefalutAPIDocumentUrl = "/docs/swagger.json"
const DefalutYAMLAPIDocumentUrl = "/docs/swagger.yaml"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIDoc ¶
type APIDoc interface { ToSwaggerOperation() (*swagger.Operation, error) ToSwaggerDefinitions() (map[string]*swagger.Schema, error) GetParameters() map[string]Parameter SetMethod(string) }
APIDoc is a interface to create Swagger.Operation
type APIDocCommon ¶
type APIDocCommon struct { Tags []string Summary string Description string Produces []string Consumes []string GlobalParameterNames []string Parameters map[string]Parameter Request *Request Responses map[int]Response // contains filtered or unexported fields }
APIDocCommon methods (POST,PUT,PATCH,DELETE,...) API document info Fields:
Tags -- A list of tags for API documentation control. Tags can be used for logical grouping of operations by resources or any other qualifier. Summary -- Summary of this api Description -- Detail info of this api Produces -- A list of MIME types the operation can produce. Consumes -- A list of MIME types the operation can consume. Parameters -- A list of parameters that are applicable for all the operations described under this path. These parameters can be overridden at the operation level, but can't be removed there. The list MUST NOT include duplicated parameters. A unique parameter is defined by a combination of a name and location. The list can use the Reference Object to link to parameters that are defined at the Swagger Object's parameters. There can be one "body" parameter at most. Request -- the request in http body. Responses -- An object to hold responses that can be used across operations
func (APIDocCommon) GetParameters ¶
func (doc APIDocCommon) GetParameters() map[string]Parameter
GetParameters Get Parameters
func (*APIDocCommon) SetMethod ¶
func (doc *APIDocCommon) SetMethod(method string)
func (APIDocCommon) ToSwaggerDefinitions ¶
func (doc APIDocCommon) ToSwaggerDefinitions() (map[string]*swagger.Schema, error)
ToSwaggerDefinitions to map[string]*swagger.Schema (swagger Definitions)
func (APIDocCommon) ToSwaggerOperation ¶
func (doc APIDocCommon) ToSwaggerOperation() (*swagger.Operation, error)
ToSwaggerOperation to swagger.Operation document
type Config ¶
type Config struct { Schemes []Scheme BasePath string Version string Title string Description string AllowOrigin bool Origins []string OpenAPIDocumentURL bool APIDocumentURL string YAMLAPIDocumentURL string DomainName string }
Config config for server Fields:
Schemes -- such as SCHEMES_HTTP, SCHEMES_HTTPS, SCHEMES_HTTP_AND_HTTPS BasePath -- such as "/apis" Version -- such as "v1.0.0" Title -- the title of server Description -- the description of server AllowOrigin -- Allow Origin Origins -- ( Cross-Origin Resource Sharing ) Access-Control-Allow-Origin OpenAPIDocumentURL -- open the url /docs/swagger.json APIDocumentURL -- the url to get openAPI(swagger) document, default value is /docs/swagger.json
type Engine ¶
type Engine struct { Conf *Config Swagger *swagger.Swagger // contains filtered or unexported fields }
Engine is the framework's instance, it contains the configuration settings and *gin.Engine. Create an instance of Engine, by using NewEngine(*rest.config)
func NewEngineByGin ¶
NewEngineByGin new an Engine from the config and the *gin.Engin
func (*Engine) DELETE ¶
func (e *Engine) DELETE(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
DELETE is a shortcut for gin router.Handle("DELETE", path, handle).
func (*Engine) GET ¶
func (e *Engine) GET(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
GET is a shortcut for gin router.Handle("GET", path, handle).
func (Engine) GetSwaggerJSONDocument ¶
GetSwaggerJSONDocument get the swagger JSON document
func (Engine) GetSwaggerYAMLDocument ¶
GetSwaggerYAMLDocument get the swagger YAML document
func (*Engine) OPTIONS ¶
func (e *Engine) OPTIONS(relativePath string, handlers ...HandlerFunc) error
OPTIONS is a shortcut for gin router.Handle("OPTIONS", path, handle).
func (*Engine) PATCH ¶
func (e *Engine) PATCH(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
PATCH is a shortcut for gin router.Handle("PATCH", path, handle).
func (*Engine) POST ¶
func (e *Engine) POST(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
POST is a shortcut for gin router.Handle("POST", path, handle).
func (*Engine) PUT ¶
func (e *Engine) PUT(relativePath string, doc APIDoc, handlers ...HandlerFunc) error
PUT is a shortcut for gin router.Handle("PUT", path, handle).
func (*Engine) SetGinEngine ¶
SetGinEngine set gin.Engine, Engine is the gin framework's instance You can use SetGinEngine to pass in the gin's Engine and extend your service with the gin framework.
type HandlerFunc ¶
HandlerFunc the callback func to handler HTTP Request
error -- the result of checking the parameter in HTTP Request (if has no error, error == nil)
type Parameter ¶
type Parameter struct { InPath *ValueInfo InHeader *ValueInfo InQuery *ValueInfo InFormData *ValueInfo }
Parameter info Fields:
InPath -- the parameter in HTTP path, like /zoos/{id} , id in HTTP path InHeader -- the parameter in HTTP header InQuery -- the parameter in HTTP query, like /zoos?id=001, id in HTTP query InFormData -- the parameter in HTTP formData
type Request ¶
type Request struct { Description string Model interface{} }
Request of the api Fields
Description -- Description of the Request model Model -- The Request Model (nil, struct, or []string )
type Response ¶
Response of the api Fields
Description -- Description of the response model Model -- The Response Model (nil, struct, or []string ) Headers -- The Response info in the HTTP header
type StructDoc ¶
type StructDoc struct { UUID string StructName string GoPkgPath string StructFields []*StructField }
StructDoc document of a struct Fields:
UUID -- UUID(Universally Unique Identifier) of the struct, UUID = GoPkgPath + StructNamethe. (like: github.com/enjoy-web/ehttp.StructDoc) StructName -- the name of struct. (like: StructDoc) GoPkgPath -- the package path of the struct. (like: github.com/enjoy-web/ehttp.StructDoc) StructFields -- StructFields in the struct.
type StructDocCreater ¶
type StructDocCreater struct {
// contains filtered or unexported fields
}
StructDocCreater is a creator specifically responsible for getting documents from objects
func (*StructDocCreater) GetStructDoc ¶
func (sc *StructDocCreater) GetStructDoc(obj interface{}) (*StructDoc, error)
GetStructDoc get StructDoc by a struct object
func (*StructDocCreater) GetStructDocMap ¶
func (sc *StructDocCreater) GetStructDocMap(obj interface{}) (map[string]*StructDoc, error)
GetStructDocMap return struct documents map from the object
type StructField ¶
type StructField struct { IsArray bool IsStruct bool RefStructUUID string ValueType string Name string Description string Enum []interface{} Min *float64 Max *float64 MinLen *int64 MaxLen *int64 Required bool Default interface{} }
StructField A structure that can display all the information in the field. Fields:
IsArray -- Whether the field type is an array IsStruct -- Whether the field is a struct(like: Filed StructField, Fields []StructField, FiledPtr *StructField , Fields []*StructField) RefStructUUID -- if the field is a struct, RefStructUUID is the StructUUID(like: github.com/enjoy-web/ehttp/StructField) ValueType -- type of the value Name -- the filed name. If not be set the tag(like: FieldName string), the filed name is FieldName, else(like: FieldName string `json:"fieldName" xml:"fieldName"`) the filed name is the value(fieldName) from the tag. note! the json tag must be equal to the xml tag Description -- description for the struct field, the description from the tag in the filed(like: IsArray bool `desc:"is array"`, the Description = "is array"")
type ValueInfo ¶
type ValueInfo struct { Type string Enum string Min string Max string Desc string MinLen string MaxLen string Default string Required bool }
ValueInfo the parameter value type info
Type -- (required) type of the parmeter value. Supports the following types: int, int32, int64, uint, uint32, uint64, float32, float64, string, bool Enum -- Enumerations of the value. (Separated by spaces. Only supports the following types: string, int, int32, int64, uint, uint32, uint64) Min -- Minimum of the value. (Only supports the following types: int, int32, int64, uint, uint32, uint64, float32, float64) Max -- Maximum of the value. (Only supports the following types: int, int32, int64, uint, uint32, uint64, float32, float64) Desc -- Description of the value