apix

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 30 Imported by: 0

README

APIX

Apix is a concise API development framework, which aim to develep a http api service fastly.

It can easily integrated with grpc-gateway, integrating the interface of grpc into apix.

Examples


// simplest hello world sample.
apix.GET("/hello", func(ctx *apix.Context) (any, error) { return "Hello World", nil } )

// custom the response status code
apix.GET("/hello", func(ctx *apix.Context) (any, int, error) { return "Hello World", 200, nil } )

// bind with http.HandlerFunc
apix.GET("/hello", func(w ResponseWriter, r *Request) { w.Write("Hello World") } )

genService.RegisterYourServiceHandlerServer(context.Background(), apix.GRPCGatewayMux(), &ServiceImplements{})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ANY

func ANY(path string, h any)

func CONNECT

func CONNECT(path string, h any)

func DELETE

func DELETE(path string, h any)

func Fail added in v0.0.6

func Fail(w http.ResponseWriter, status int, err error)

func Failf added in v0.0.6

func Failf(w http.ResponseWriter, status int, msg string, args ...any)

func GET

func GET(path string, h any)

func GRPCGatewayMux added in v0.0.3

func GRPCGatewayMux() *runtime.ServeMux
func HEAD(path string, h any)

func ListenAndServe

func ListenAndServe(addr string) error

func MarshalText added in v0.0.5

func MarshalText(data any) ([]byte, error)

func OPTION

func OPTION(path string, h any)

func PATCH

func PATCH(path string, h any)

func POST

func POST(path string, h any)

func PUT

func PUT(path string, h any)

func Return added in v0.0.6

func Return(w http.ResponseWriter, status int, data any, marshaler func(any) ([]byte, error))

Return write the result and code into ResponseWriter

func ReturnJSON added in v0.0.6

func ReturnJSON(w http.ResponseWriter, status int, data any)

func ReturnText added in v0.0.6

func ReturnText(w http.ResponseWriter, status int, data any)

func TRACE

func TRACE(path string, h any)

Types

type Context added in v0.0.5

type Context struct {
	Request *http.Request
	Writer  http.ResponseWriter

	Keys map[string]any
	// contains filtered or unexported fields
}

Context is the most important part of gin. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.

func Ctx added in v0.0.6

func Ctx(ctx context.Context) *Context

Ctx peek *apix.Context from the given context, it return nil if *apix.Context not exist

func (*Context) Body added in v0.0.5

func (c *Context) Body() []byte

Body return the body bytes

func (*Context) Deadline added in v0.0.5

func (c *Context) Deadline() (deadline time.Time, ok bool)

Deadline returns that there is no deadline (ok==false) when c.Request has no Context.

func (*Context) Done added in v0.0.5

func (c *Context) Done() <-chan struct{}

Done returns nil (chan which will wait forever) when c.Request has no Context.

func (*Context) Err added in v0.0.5

func (c *Context) Err() error

Err returns nil when c.Request has no Context.

func (*Context) Fail added in v0.0.5

func (c *Context) Fail(status int, err error)

func (*Context) Failf added in v0.0.5

func (c *Context) Failf(status int, msg string, args ...any)

func (*Context) Get added in v0.0.5

func (c *Context) Get(key string) (value any, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exist it returns (nil, false)

func (*Context) Return added in v0.0.5

func (c *Context) Return(status int, data any, marshaler func(any) ([]byte, error))

Return write the result and code into ResponseWriter

func (*Context) ReturnJSON added in v0.0.6

func (c *Context) ReturnJSON(status int, data any)

func (*Context) ReturnText added in v0.0.6

func (c *Context) ReturnText(status int, data any)

func (*Context) Set added in v0.0.5

func (c *Context) Set(key string, value any)

Set is used to store a new key/value pair exclusively for this context. It also lazy initializes c.Keys if it was not used previously.

func (*Context) SetContentType added in v0.0.5

func (c *Context) SetContentType(s string)

func (*Context) Value added in v0.0.5

func (c *Context) Value(key any) any

Value returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Value with the same key returns the same result.

func (*Context) With added in v0.0.6

func (c *Context) With(ctx context.Context) context.Context

With add self into given ctx by context.WithValue, and return the new context

type Group added in v0.0.2

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

Group represents a api group

func GROUP added in v0.0.3

func GROUP(path string, middlewares ...Middleware) *Group

func (*Group) ANY added in v0.0.2

func (g *Group) ANY(p string, h any)

func (*Group) CONNECT added in v0.0.2

func (g *Group) CONNECT(p string, h any)

func (*Group) DELETE added in v0.0.2

func (g *Group) DELETE(p string, h any)

func (*Group) GET added in v0.0.2

func (g *Group) GET(p string, h any)

func (*Group) GROUP added in v0.0.2

func (g *Group) GROUP(p string, middlewares ...Middleware) *Group

GROUP create a sub group base on this group. The url path and middlewares in arguments will append to the parent group's path and middlewares

func (*Group) HEAD added in v0.0.2

func (g *Group) HEAD(p string, h any)

func (*Group) OPTION added in v0.0.2

func (g *Group) OPTION(p string, h any)

func (*Group) PATCH added in v0.0.2

func (g *Group) PATCH(p string, h any)

func (*Group) POST added in v0.0.2

func (g *Group) POST(p string, h any)

func (*Group) PUT added in v0.0.2

func (g *Group) PUT(p string, h any)

func (*Group) TRACE added in v0.0.2

func (g *Group) TRACE(p string, h any)

type Handler

type Handler interface {
	Execute(*Context) (any, error)
}

Handler is a function type for handling http.Request, the return value will be marshaled into json before writing into response.

type HandlerCode

type HandlerCode interface {
	ExecuteCode(*Context) (any, int, error)
}

HandlerCode is similar with apix.Handler, but can customze the http status code in response by the second return value.

type Middleware added in v0.0.2

type Middleware func(http.HandlerFunc) http.HandlerFunc

Middleware wrap the http.HandlerFunc, so that it can handle the http.Request in advance and intercept the request if required(eg. authorization, logging)

type ResponseBody

type ResponseBody struct {
	Code    int    `json:"code"`
	Data    any    `json:"data,omitempty"`
	Message string `json:"message,omitempty"`
}

ResponseBody represents data type in response body

func (ResponseBody) Error

func (reb ResponseBody) Error() string

Error implements the error interface, it return empty string if ResponseBody.Code == 0

type Service

type Service struct {
	// contains filtered or unexported fields
}
var (
	DefaultService *Service
)

func New

func New(opts ...ServiceOption) *Service

func (*Service) ANY

func (srv *Service) ANY(path string, h any)

func (*Service) CONNECT

func (srv *Service) CONNECT(path string, h any)

func (*Service) DELETE

func (srv *Service) DELETE(path string, h any)

func (*Service) GET

func (srv *Service) GET(path string, h any)

func (*Service) GROUP added in v0.0.2

func (srv *Service) GROUP(path string, middlewares ...Middleware) *Group

GROUP create a api group with custom url prefix and middlewares, the middlewares only works on handlers registerd on this group

func (*Service) GRPCGatewayMux

func (srv *Service) GRPCGatewayMux() *runtime.ServeMux

GRPCGatewayMux return the grpcgateway servemux, use it to register grpc service

func (*Service) HEAD

func (srv *Service) HEAD(path string, h any)

func (*Service) ListenAndServe

func (srv *Service) ListenAndServe(addr string) error

func (*Service) OPTION

func (srv *Service) OPTION(path string, h any)

func (*Service) PATCH

func (srv *Service) PATCH(path string, h any)

func (*Service) POST

func (srv *Service) POST(path string, h any)

func (*Service) PUT

func (srv *Service) PUT(path string, h any)

func (*Service) ServeHTTP

func (srv *Service) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface

func (*Service) TRACE

func (srv *Service) TRACE(path string, h any)

type ServiceOption

type ServiceOption func(*Service)

func UseGRPCHeaders

func UseGRPCHeaders(patterns ...string) ServiceOption

UseGRPCHeaders extends the http headers whould to be forward to grpc service. By default, only headers with 'grpcgateway-' key prefix, and permanent HTTP header(as specified by the IANA, e.g: Accept, Cookie, Host) will be forward.

func WithMiddleware added in v0.0.2

func WithMiddleware(middlewares ...Middleware) ServiceOption

WithMiddleware specifics middlewares for all the service handlers.

func WithNotFoundHandler

func WithNotFoundHandler(h http.Handler) ServiceOption

WithNotFoundHandler specifics a http handler for 404 case.

func WithResponseMarshaler added in v0.0.5

func WithResponseMarshaler(marshaler func(any) ([]byte, error)) ServiceOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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