core

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 8 Imported by: 0

README

gogitsu/core

Documentation

Overview

Package core implements core components of gogitsu lib.

controller.go defines the Controller interface and the base controller implementation struct from which derive concrete controllers into apps.

Package core implements core components of gogitsu lib.

controller.go defines the Controller interface and the base controller implementation struct from which derive concrete controllers into apps.

Package core implements core components of gogitsu lib.

error.go defines Error interface and HTTPError specific error type.

Package core implements core components of gogitsu lib.

controller.go defines the Controller interface and the base controller implementation struct from which derive concrete controllers into apps.

Package core implements core components of gogitsu lib.

router.go implements Router interface and concrete implementations. Actually only Gorilla based concrete Router is implemented.

Index

Constants

View Source
const (

	// MySQLType to create a connection.
	MySQLType = "mysql"
	// PostgresType to create a connection.
	PostgresType = "postgres"
	// Sqlite3Type to create a connection.
	Sqlite3Type = "sqlite3"
	// MSsqlType to create a connection.
	MSsqlType = "mssql"

	// MySQLConnectionStringFormat to create a connection string for MySQL.
	MySQLConnectionStringFormat = "%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local"

	// PostresConnectionStringFormat to create a connection string for Postgres.
	// "host=myhost port=myport user=gorm dbname=gorm password=mypassword"
	PostresConnectionStringFormat = "host=%s port=%d user=%s dbname=%s password=%s"

	// MSsqlConnectionStringFormat to create a connection string for Microsoft SQL Server.
	MSsqlConnectionStringFormat = "sqlserver://%s:%s@%s:%d?database=%s"
)
View Source
const (
	// Gorilla is the string identifier for Gorilla Mux implemenetation.
	Gorilla string = "gorilla"
)

Variables

This section is empty.

Functions

func BadGateway

func BadGateway(err error, detail interface{}, requestID string) error

BadGateway http error.

func BadRequest

func BadRequest(err error, detail interface{}, requestID string) error

BadRequest http error.

func Conflict

func Conflict(err error, detail interface{}, requestID string) error

Conflict http error.

func Forbidden

func Forbidden(err error, detail interface{}, requestID string) error

Forbidden http error.

func GatewayTimeout

func GatewayTimeout(err error, detail interface{}, requestID string) error

GatewayTimeout http error.

func InternalServerError

func InternalServerError(err error, detail interface{}, requestID string) error

InternalServerError http error.

func MethodNotAllowed

func MethodNotAllowed(err error, detail interface{}, requestID string) error

MethodNotAllowed http error.

func NetworkAuthenticationRequired

func NetworkAuthenticationRequired(err error, detail interface{}, requestID string) error

NetworkAuthenticationRequired http error.

func NewHTTPError

func NewHTTPError(err error, status int, detail interface{}, requestID string) error

NewHTTPError returns a new HTTPError instance

func NoContent

func NoContent(err error, detail interface{}, requestID string) error

NoContent http error.

func NotAcceptable

func NotAcceptable(err error, detail interface{}, requestID string) error

NotAcceptable http error.

func NotFound

func NotFound(err error, detail interface{}, requestID string) error

NotFound http error.

func NotImplemented

func NotImplemented(err error, detail interface{}, requestID string) error

NotImplemented http error.

func RenderError

func RenderError(w http.ResponseWriter, err error)

RenderError exported to be used in this lib (a.e. in middlewares) returns error to the client.

func RequestTimeout

func RequestTimeout(err error, detail interface{}, requestID string) error

RequestTimeout http error.

func ServiceUnavailable

func ServiceUnavailable(err error, detail interface{}, requestID string) error

ServiceUnavailable http error.

func Unauthorized

func Unauthorized(err error, detail interface{}, requestID string) error

Unauthorized http error.

func UnprocessableEntity

func UnprocessableEntity(err error, detail interface{}, requestID string) error

UnprocessableEntity http error.

Types

type BaseController

type BaseController struct {
	Path string
}

BaseController .

func (*BaseController) BasePath

func (bc *BaseController) BasePath() string

BasePath .

func (*BaseController) Route

func (bc *BaseController) Route(r Router)

Route .

type Controller

type Controller interface {
	BasePath() string
	Route(r Router)
}

Controller .

type DBConfig added in v0.1.2

type DBConfig struct {
	Type       string `env:"DB_TYPE"`
	Host       string `env:"DB_HOST"`
	Port       int    `env:"DB_PORT"`
	User       string `env:"DB_USER"`
	Password   string `env:"DB_PWD"`
	Database   string `env:"DB_NAME"`
	Log        bool   `env:"DB_LOG_MODE"`
	Migrations struct {
		Enabled            bool `env:"DB_MIG_ENABLED"`
		Drop               bool `env:"DB_MIG_DROP"`
		SingularTableNames bool `env:"DB_MIG_SINGULAR"`
	}
}

DBConfig is the configuration struct to set database info for the DBManager initialization. Note that the struct fields has gogitsu/conf tags.

type DBConfigurator added in v0.1.2

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

DBConfigurator .

func NewDBConfigurator added in v0.1.2

func NewDBConfigurator(config DBConfig) DBConfigurator

NewDBConfigurator .

func (DBConfigurator) Configure added in v0.1.2

func (dbc DBConfigurator) Configure(config DBConfig)

Configure .

type DBManager added in v0.1.2

type DBManager interface {
	DB() interface{}
	Configure(config DBConfig)
	Connect() error
}

DBManager is the contract intrface for a db manager implementation. Each implementation will get a config struct and initialize a db connection (pool) accordind to the underline database lib used.

func NewDefaultGormDBManager added in v0.1.2

func NewDefaultGormDBManager() DBManager

NewDefaultGormDBManager return a new empty gorm db manager instance.

func NewGormDBManager added in v0.1.2

func NewGormDBManager(config DBConfig) DBManager

NewGormDBManager return a new gorm db manager instance configured and ready to connect.

type Error

type Error interface {
	// Error method to make Error implement error interface.
	Error() string
	// ResponseBody returns response body.
	ResponseBody() ([]byte, error)
	// ResponseHeaders returns http status code and headers.
	ResponseHeaders() (int, map[string]string)
}

Error is an error whose details to be shared with client. Check also this https://github.com/google/jsonapi/blob/master/errors.go

type GorillaRouter

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

GorillaRouter is the main routing structure. It holds a Gorilla mux to be used for routing configuration.

func NewGorillaRouter

func NewGorillaRouter() *GorillaRouter

NewGorillaRouter return a new GorillaRouter instance.

func NewGorillaRouterWithRoot

func NewGorillaRouterWithRoot(path string) *GorillaRouter

NewGorillaRouterWithRoot return a new GorillaRouter with root path instance.

func (*GorillaRouter) Delete

func (r *GorillaRouter) Delete(path string, fn http.HandlerFunc) Router

Delete .

func (*GorillaRouter) Get

func (r *GorillaRouter) Get(path string, fn http.HandlerFunc) Router

Get .

func (*GorillaRouter) HandleFunc

func (r *GorillaRouter) HandleFunc(path string, fn http.HandlerFunc) interface{}

HandleFunc .

func (*GorillaRouter) LogRoute

func (r *GorillaRouter) LogRoute(i interface{})

LogRoute . func LogRoute(route *mux.Route) {

func (*GorillaRouter) Mux

func (r *GorillaRouter) Mux() http.Handler

Mux return the http.Handler implementation.

func (*GorillaRouter) Patch

func (r *GorillaRouter) Patch(path string, fn http.HandlerFunc) Router

Patch .

func (*GorillaRouter) Post

func (r *GorillaRouter) Post(path string, fn http.HandlerFunc) Router

Post .

func (*GorillaRouter) PrintRoute

func (r *GorillaRouter) PrintRoute(i interface{})

PrintRoute . func PrintRoute(route *mux.Route) {

func (*GorillaRouter) Put

func (r *GorillaRouter) Put(path string, fn http.HandlerFunc) Router

Put .

func (*GorillaRouter) Walk

func (r *GorillaRouter) Walk(wfn WalkFn)

Walk walks on routes and execute the input callback.

func (*GorillaRouter) WithControllers

func (r *GorillaRouter) WithControllers(ctrls ...Controller) Router

WithControllers . Here each controller's base path will define the relative Group into the root path.

func (*GorillaRouter) WithRoot

func (r *GorillaRouter) WithRoot(path string) Router

WithRoot . Here the Subrouter is the root for Groups defined by the controller paths.

type GormDBManager added in v0.1.2

type GormDBManager struct {
	DBConfigurator
	// contains filtered or unexported fields
}

GormDBManager is the GORM based implementation for DBManager.

func (*GormDBManager) Connect added in v0.1.2

func (gbm *GormDBManager) Connect() error

Connect establish a connection (pool) to the db.

func (*GormDBManager) DB added in v0.1.2

func (gbm *GormDBManager) DB() interface{}

DB return the internal gorm DB.

type GormRepository added in v0.1.2

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

GormRepository is the base repository implemented with gorm. This should not be instantiated but composed in your concrete model repository.

func (*GormRepository) DB added in v0.1.2

func (r *GormRepository) DB() interface{}

DB returns the internal, lib specific, db manager pointer.

func (*GormRepository) InTransaction added in v0.1.2

func (r *GormRepository) InTransaction(tf TxFunction) error

InTransaction executes the tf function in a db transaction. The tf function param will receive a tx instance that will have to be casted to a *gorm.DB instance. All of the db operation in the tf function will have to be executed against the tx passed in. Then, here, the transaction will be committed or rolled back for you.

func (*GormRepository) SetDB added in v0.1.2

func (r *GormRepository) SetDB(db interface{})

SetDB sets into the repository the lib specific db manager pointer. The input db param is casted to a *gorm.DB.

type HTTPError

type HTTPError struct {
	Code      int         `json:"code"`
	Err       string      `json:"error"`
	Detail    interface{} `json:"detail"`
	RequestID string      `json:"requestId"`
	Timestamp time.Time   `json:"timestamp"`
}

HTTPError implements Error interface.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error return a formatted description of the error.

func (*HTTPError) ResponseBody

func (e *HTTPError) ResponseBody() ([]byte, error)

ResponseBody returns JSON response body.

func (*HTTPError) ResponseHeaders

func (e *HTTPError) ResponseHeaders() (int, map[string]string)

ResponseHeaders returns http status code and headers.

type Repository added in v0.1.2

type Repository interface {
	DB() interface{}
	SetDB(db interface{})
	InTransaction(tf TxFunction) error
}

Repository is the contract interface for a db repository. This interface can be implemented with any db library. According to the specific db library, the DB type will be the library db type. A.e. for a gorm implementation it will be a *gorm.DB, or for a database/sql implementation it will be a *sql.DB.ù

func NewGormRepository added in v0.1.2

func NewGormRepository(db *gorm.DB) Repository

NewGormRepository returns a new gorm repository instance. This is useful in struct composition to create specific repository into apps. You can compose the GormRepository struct into your repository and use the NewGormRepository func to initializ the composition.

type Router

type Router interface {
	Mux() http.Handler
	WithRoot(path string) Router
	WithControllers(ctrls ...Controller) Router
	Walk(wfn WalkFn)
	HandleFunc(path string, fn http.HandlerFunc) interface{}
	Get(path string, fn http.HandlerFunc) Router
	Post(path string, fn http.HandlerFunc) Router
	Put(path string, fn http.HandlerFunc) Router
	Patch(path string, fn http.HandlerFunc) Router
	Delete(path string, fn http.HandlerFunc) Router
	PrintRoute(interface{})
	LogRoute(interface{})
}

Router defines interface to work with concrete routers.

func NewRouter

func NewRouter(rt string) Router

NewRouter is the factory function to instantiate a new Router according to the input router type. Default is the Gorilla mux instance.

Actually only Gorilla based concrete Router.

func NewRouterWithRootPath

func NewRouterWithRootPath(rt string, path string) Router

NewRouterWithRootPath is the factory function to instantiate a new Router according to the input router type and root path. Default is the Gorilla mux instance.

type TxFunction added in v0.1.2

type TxFunction func(tx interface{}) error

TxFunction is a function to be executed in a transaction. Depending on the repository implementation, the tx param will be the transaction object specific for the db library used in the implementation.

type WalkFn

type WalkFn func(interface{})

WalkFn is the type for function to be called from the Walk function. Heach concrete router implementation will cast the input interfcae{} to thw right input param type (a.e. for Gorilla mux will be a Route).

Jump to

Keyboard shortcuts

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