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
- func BadGateway(err error, detail interface{}, requestID string) error
- func BadRequest(err error, detail interface{}, requestID string) error
- func Conflict(err error, detail interface{}, requestID string) error
- func Forbidden(err error, detail interface{}, requestID string) error
- func GatewayTimeout(err error, detail interface{}, requestID string) error
- func InternalServerError(err error, detail interface{}, requestID string) error
- func MethodNotAllowed(err error, detail interface{}, requestID string) error
- func NetworkAuthenticationRequired(err error, detail interface{}, requestID string) error
- func NewHTTPError(err error, status int, detail interface{}, requestID string) error
- func NoContent(err error, detail interface{}, requestID string) error
- func NotAcceptable(err error, detail interface{}, requestID string) error
- func NotFound(err error, detail interface{}, requestID string) error
- func NotImplemented(err error, detail interface{}, requestID string) error
- func RenderError(w http.ResponseWriter, err error)
- func RequestTimeout(err error, detail interface{}, requestID string) error
- func ServiceUnavailable(err error, detail interface{}, requestID string) error
- func Unauthorized(err error, detail interface{}, requestID string) error
- func UnprocessableEntity(err error, detail interface{}, requestID string) error
- type BaseController
- type Controller
- type DBConfig
- type DBConfigurator
- type DBManager
- type Error
- type GorillaRouter
- func (r *GorillaRouter) Delete(path string, fn http.HandlerFunc) Router
- func (r *GorillaRouter) Get(path string, fn http.HandlerFunc) Router
- func (r *GorillaRouter) HandleFunc(path string, fn http.HandlerFunc) interface{}
- func (r *GorillaRouter) LogRoute(i interface{})
- func (r *GorillaRouter) Mux() http.Handler
- func (r *GorillaRouter) Patch(path string, fn http.HandlerFunc) Router
- func (r *GorillaRouter) Post(path string, fn http.HandlerFunc) Router
- func (r *GorillaRouter) PrintRoute(i interface{})
- func (r *GorillaRouter) Put(path string, fn http.HandlerFunc) Router
- func (r *GorillaRouter) Walk(wfn WalkFn)
- func (r *GorillaRouter) WithControllers(ctrls ...Controller) Router
- func (r *GorillaRouter) WithRoot(path string) Router
- type GormDBManager
- type GormRepository
- type HTTPError
- type Repository
- type Router
- type TxFunction
- type WalkFn
Constants ¶
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" )
const ( // Gorilla is the string identifier for Gorilla Mux implemenetation. Gorilla string = "gorilla" )
Variables ¶
This section is empty.
Functions ¶
func BadGateway ¶
BadGateway http error.
func BadRequest ¶
BadRequest http error.
func GatewayTimeout ¶
GatewayTimeout http error.
func InternalServerError ¶
InternalServerError http error.
func MethodNotAllowed ¶
MethodNotAllowed http error.
func NetworkAuthenticationRequired ¶
NetworkAuthenticationRequired http error.
func NewHTTPError ¶
NewHTTPError returns a new HTTPError instance
func NotAcceptable ¶
NotAcceptable http error.
func NotImplemented ¶
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 ¶
RequestTimeout http error.
func ServiceUnavailable ¶
ServiceUnavailable http error.
func Unauthorized ¶
Unauthorized http error.
func UnprocessableEntity ¶
UnprocessableEntity http error.
Types ¶
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
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
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) ResponseBody ¶
ResponseBody returns JSON response body.
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 ¶
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 ¶
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.