snakepit

package module
v0.0.0-...-bc2b6bc Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2016 License: MIT Imports: 21 Imported by: 10

README

snakepit is a cobra/viper commands repository and a productivity oriented toolbox.

API example seed: snakepit-seed

Commands

Root

The root command is meant to be the entrypoint of your cobra based app. It exports a viper instance allowing other commands to build on it.

Run

The run command is a simple graceful server building and running a HTTP handler. The Builder is typically set from a local run command in your app.

Toolbox

Besides the cobra commands, snakepit offers utils to build expressive web APIs:

  • A standardized API error format.
  • A suite of net/context based middlewares:
    • swagger to expose Swagger documentation on /swagger.
    • requestID, inspired by the one from Goji, to uniquely tag each request.
    • logger using logrus setting a requestID tagged logger (if existing) in the request context.
    • recoverer recovering from panics, logging the error if logger is present and sending standardized 500 errors.
    • timer mesuring the middleware stack processing time and logging it if logger is present.
  • A ffjson based JSON marshaller/unmarshaller that automatically log processing times if the logger middleware is present in the middleware stack and returns standardized 400 errors when unmarshallings fails. Also supports bulk requests unmarshalling.

TODOs

  • Write tests
  • Add a lot of documentation

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	APIJsonRendering = APIError{
		Description: "The JSON rendering failed.",
		ErrorCode:   "JSON_RENDERING_ERROR",
	}
	APIBodyDecoding = APIError{
		Description: "Could not decode the JSON request.",
		ErrorCode:   "BODY_DECODING_ERROR",
	}
)
View Source
var APIInternal = APIError{
	Description: "An internal error occured. Please retry later.",
	ErrorCode:   "INTERNAL_ERROR",
}

Functions

func GetLogger

func GetLogger(ctx context.Context) (*logrus.Entry, error)

func GetRequestID

func GetRequestID(ctx context.Context) (string, error)

GetRequestID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.

func GetResLogEntry

func GetResLogEntry(ctx context.Context) (*logrus.Entry, error)

func LogTime

func LogTime(log *logrus.Entry, name string, start time.Time)

func NewLogger

func NewLogger(log *logrus.Logger) func(next chi.Handler) chi.Handler

func NewRecoverer

func NewRecoverer(j *JSON) func(next chi.Handler) chi.Handler

func NewRequestID

func NewRequestID() func(next chi.Handler) chi.Handler

func NewSwagger

func NewSwagger(basePath, scheme string) func(next chi.Handler) chi.Handler

func NewValidationError

func NewValidationError(field, err string) error

Types

type APIError

type APIError struct {
	// The status code.
	Status int `json:"status"`
	// The description of the API error.
	Description string `json:"description"`
	// The token uniquely identifying the API error.
	ErrorCode string `json:"errorCode"`
	// Additional infos.
	Params map[string]interface{} `json:"params,omitempty"`
}

APIError defines a standard format for API errors.

func (APIError) Error

func (e APIError) Error() string

type ArangoDBManager

type ArangoDBManager struct {
	URL, Name          string
	User, UserPassword string
	// contains filtered or unexported fields
}

func NewArangoDBManager

func NewArangoDBManager(localSeed, distantSeed Seed) *ArangoDBManager

func (*ArangoDBManager) Connect

func (d *ArangoDBManager) Connect(url, name, user, userPassword string) *ArangoDBManager

func (*ArangoDBManager) Create

func (d *ArangoDBManager) Create(rootUser, rootPassword string) error

func (*ArangoDBManager) Drop

func (d *ArangoDBManager) Drop(rootUser, rootPassword string) error

func (*ArangoDBManager) LoadDistantSeed

func (d *ArangoDBManager) LoadDistantSeed() error

func (*ArangoDBManager) LoggerOptions

func (d *ArangoDBManager) LoggerOptions(enabled, printQuery, printResult bool) *ArangoDBManager

func (*ArangoDBManager) Migrate

func (d *ArangoDBManager) Migrate() error

func (*ArangoDBManager) Run

func (d *ArangoDBManager) Run(q arangolite.Runnable) ([]byte, error)

func (*ArangoDBManager) SyncSeeds

func (d *ArangoDBManager) SyncSeeds() error

type Controller

type Controller struct {
	Constants *viper.Viper
	Logger    *logrus.Entry
	JSON      *JSON
}

func NewController

func NewController(
	c *viper.Viper,
	l *logrus.Entry,
	j *JSON,
) *Controller

type CtxKey

type CtxKey string

type Handler

type Handler struct {
	Constants *viper.Viper
	JSON      *JSON
}

func NewHandler

func NewHandler(
	c *viper.Viper,
	j *JSON,
) *Handler

func (*Handler) LogTime

func (h *Handler) LogTime(logger *logrus.Entry, start time.Time)

type Interactor

type Interactor struct {
	Constants *viper.Viper
	Logger    *logrus.Entry
}

func NewInteractor

func NewInteractor(
	c *viper.Viper,
	l *logrus.Entry,
) *Interactor

type JSON

type JSON struct{}

func NewJSON

func NewJSON() *JSON

func (*JSON) Marshal

func (j *JSON) Marshal(
	l *logrus.Entry,
	name string,
	obj interface{},
) ([]byte, error)

func (*JSON) Render

func (j *JSON) Render(
	ctx context.Context,
	w http.ResponseWriter,
	status int,
	object interface{},
)

func (*JSON) RenderError

func (j *JSON) RenderError(
	ctx context.Context,
	w http.ResponseWriter,
	status int,
	apiError APIError,
	e error,
)

func (*JSON) Unmarshal

func (j *JSON) Unmarshal(
	l *logrus.Entry,
	name string,
	raw []byte,
	obj interface{},
) error

func (*JSON) UnmarshalBody

func (j *JSON) UnmarshalBody(
	ctx context.Context,
	w http.ResponseWriter,
	body io.ReadCloser,
	obj interface{},
) bool

func (*JSON) UnmarshalBodyBulk

func (j *JSON) UnmarshalBodyBulk(
	ctx context.Context,
	w http.ResponseWriter,
	body io.ReadCloser,
	objSlice interface{},
) (bool, bool)

type Logger

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

type Recoverer

type Recoverer struct {
	JSON *JSON
}

type Repository

type Repository struct {
	Constants *viper.Viper
	Logger    *logrus.Entry
	JSON      *JSON
}

func NewRepository

func NewRepository(
	c *viper.Viper,
	l *logrus.Entry,
	j *JSON,
) *Repository

type RequestID

type RequestID struct{}

RequestID is a middleware that injects a request ID into the context of each request. A request ID is a string of the form "host.example.com/random-0001", where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.

type Seed

type Seed interface{}

type Swagger

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

type Timer

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

func NewTimer

func NewTimer(description string) *Timer

func (*Timer) End

func (t *Timer) End(next chi.Handler) chi.Handler

func (*Timer) Start

func (t *Timer) Start(next chi.Handler) chi.Handler

type Validator

type Validator struct {
	Logger *logrus.Entry
}

func NewValidator

func NewValidator(
	l *logrus.Entry,
) *Validator

func (*Validator) LogTime

func (v *Validator) LogTime(start time.Time)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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