api

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: MIT Imports: 10 Imported by: 0

README

api

Custom http.Handler types which handles errors.AppError gracefully in oneplace.

type Handler func(w http.ResponseWriter, r *http.Request) *errors.AppError

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FormDecoder *schema.Decoder

FormDecoder gorrila schema decoder is used to decode form data & query params into structs. And it validates the data against the struct types. supported types: - bool, - float32, float64, - int, int8, int16, int32, int64, - string, []string, - uint, uint8, uint16, uint32, uint64, - struct and we can write custom decoders for custom types. example time.Time, uuid.UUID, etc..

View Source
var RequestIDHeader = "X-Request-Id"

RequestIDHeader is the name of the HTTP Header which contains the request id. Exported so that it can be changed by developers

View Source
var (
	ServiceName = ""
)

ServiceName holds the service which connected to

Functions

func Decode

func Decode(r *http.Request, v interface{}) *errors.AppError

Decode - decodes the request body and extends the validator interface with the Validate() method

EX:

type User struct {
	Email        string       `json:"email"`
	Name         string       `json:"name"`
}

func (c *Component) Validate() *errors.AppError {
	if c.Email == "" {
		return errors.IsRequiredErr("email")
	}
	return nil
}

func HealthHandeler

func HealthHandeler(w http.ResponseWriter, r *http.Request)

HealthHandeler return basic service info

func IndexHandeler

func IndexHandeler(w http.ResponseWriter, r *http.Request)

IndexHandeler common index handler for all the service

func InitService

func InitService(name, version string)

InitService sets the service name

func JustDecode

func JustDecode(r *http.Request, v interface{}) error

JustDecode just decodes the request body

Types

type Handler

type Handler func(w http.ResponseWriter, r *http.Request) *errors.AppError

Handler custom api handler help us to handle all the errors in one place

func (Handler) ServeHTTP

func (fn Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http handler interface

type ServiceInfo

type ServiceInfo struct {
	Name    string    `json:"name"`
	Version string    `json:"version"`
	Uptime  time.Time `json:"uptime"`
	Epoch   int64     `json:"epoch"`
}

ServiceInfo stores basic service information

Jump to

Keyboard shortcuts

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