srv

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 25 Imported by: 1

README

Srv - Api server framework

Srv works over standart net/http lib.

  • query param parsing
  • body parsing
  • response serialization
  • error handling
  • match method/path
  • add "help" method optional
  • automaticly generate form params method
  • routers

How to use

type handler struct { srv.BaseGetHandler }

type request struct {
    Name string `srv:"query" name:"name"`
}

func (handler) Path() string {
    return "/path/to/handler/"
}

func (handler) Handle(req request) (string, error) {
    return "hello " + req.Name + "!", nil
}

srv.Srv{
	Addr: ":8080",
	HelpPath: "/help",
	Handlers: []srv.IRoutable{
        srv.MakeHandler(handler{})
    }
}.Run()

Documentation

Tests

Link to integrations tests

Documentation

Index

Constants

View Source
const MethodDelete = consts.MethodDelete
View Source
const MethodGet = consts.MethodGet
View Source
const MethodPatch = consts.MethodPatch
View Source
const MethodPost = consts.MethodPost
View Source
const MethodPut = consts.MethodPut

Variables

View Source
var Err = &tErr{
	EmptyHandlers:           errs.ErrEmptyHandlers{},
	ErrCanNotUseTypeInQuery: errs.ErrCanNotUseTypeInQuery{},
	ErrRestMustBeLast:       errs.ErrRestMustBeLast{},
	NotFound:                errs.ErrNotFound{},
	MethodNotAllowed:        errs.ErrMethodNotAllowed{},
	Internal:                errs.ErrInternal{},
	InvalidBody:             errs.ErrInvalidBody{},
	InvalidQuery:            errs.ErrInvalidQuery{},
	InvalidForm:             errs.ErrInvalidForm{},
	InvalidHeader:           errs.ErrInvalidHeader{},
}
View Source
var Static = t_static{}

Functions

func RegisterEngine added in v0.13.0

func RegisterEngine(name string, engine IEngine)

Types

type BaseDeleteHandler

type BaseDeleteHandler = base.BaseDeleteHandler

type BaseGetHandler

type BaseGetHandler = base.BaseGetHandler

type BaseMiddleware added in v0.13.0

type BaseMiddleware = base.BaseMiddleware

type BasePatchHandler

type BasePatchHandler = base.BasePatchHandler

type BasePostHandler

type BasePostHandler = base.BasePostHandler

type BasePutHandler

type BasePutHandler = base.BasePutHandler

type Body added in v0.6.0

type Body[T any] struct {
	Body T `srv:"body"`
}

type FormFile added in v0.11.0

type FormFile = types.FormFile

type IApiHandler

type IApiHandler[Request, Response any] interface {
	FormParams() any
	Errors() []error
	Method() string
	Path() string
	Description() string
	Handle(Request) (Response, error)
}

type IDispatcher added in v0.13.0

type IDispatcher = proto.IDispatcher

type IEngine added in v0.13.0

type IEngine = proto.IEngine

type IHeaderParam added in v0.6.0

type IHeaderParam = proto.IHeaderParam

type IMiddleware added in v0.13.0

type IMiddleware = proto.IMiddleware

type IMiddlewareErrArg added in v0.10.0

type IMiddlewareErrArg = proto.IMiddlewareErrArg

type IMiddlewareReqArg added in v0.10.0

type IMiddlewareReqArg = proto.IMiddlewareReqArg

type IMiddlewareRespArg added in v0.10.0

type IMiddlewareRespArg = proto.IMiddlewareRespArg

type IQueryParam added in v0.6.0

type IQueryParam = proto.IQueryParam

type IRoutable added in v0.5.0

type IRoutable = proto.IRoutable

func GenericHandler added in v0.10.2

func GenericHandler[Req, Resp any](
	method string,
	path string,
	f func(Req) (Resp, error),
) IRoutable

func MakeHandler

func MakeHandler[
	Req, Resp any,
	Handler IApiHandler[Req, Resp],
](handler Handler) IRoutable

func Router added in v0.5.0

func Router(path string, children ...IRoutable) IRoutable

type IToApiError added in v0.6.0

type IToApiError = errs.IToApiError

type InternalResponse added in v0.13.0

type InternalResponse = types.InternalResponse

type None

type None = types.None

type RawRequest added in v0.13.0

type RawRequest = proto.IRawRequest

type Response added in v0.8.0

type Response[T any] struct {
	Data T
	// contains filtered or unexported fields
}

func (*Response[T]) AddHeader added in v0.8.0

func (self *Response[T]) AddHeader(key string, val string)

func (*Response[T]) DelHeader added in v0.8.0

func (self *Response[T]) DelHeader(key string)

func (Response[T]) IntoResponse added in v0.8.0

func (self Response[T]) IntoResponse() types.Response

func (Response[T]) ResponseType added in v0.8.0

func (self Response[T]) ResponseType() reflect.Type

func (*Response[T]) SetHeaders added in v0.8.0

func (self *Response[T]) SetHeaders(headers map[string]string)

func (*Response[T]) SetStatus added in v0.8.0

func (self *Response[T]) SetStatus(status int)

type Srv

type Srv struct {
	Addr           string
	HelpPath       string
	Handlers       []IRoutable
	DefaultHeaders map[string]string
	Middleware     []IMiddleware
	TlsCertPath    string
	TlsKeyPath     string
	Engine         string
}

func (Srv) Run

func (self Srv) Run() error

Jump to

Keyboard shortcuts

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