goat

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 35 Imported by: 0

README

GOAT

Goat is a toolkit for creating JSON-speaking RESTful APIs in Go.

Goat aims to streamline development by offering a tightly focused feature set, convention-over-configuration philosophies, and out-of-the-box devops tooling.

Goat is not made for building traditional webapps with a front-end. Instead, it focuses on helping developers create headless webservices that can be used as the backend for server-side rendered static sites or mobile-apps.

Roadmap

Planned features include:

  • A global CLI tool for for scaffolding projects (including out-of-the-box Docker support and Makefiles)
  • Full migration suite
  • Generator scripts for Cobra commands, repositories, models, handlers, migrations, etc.
  • Out-of-the-box AWS Cloudformation support via generated templates and bootstrapping scripts, including Parameter Store management of secrets and ECS clusters.
  • Oauth support
  • Out-of-the-box Cobra utility commands
  • Support for more additional database engines (currently only supports MySQL-compatible databases)
  • Front end integrations via Horns, a comprehensive, fully-themable, open-source UI framework made for Gatsby and React Emotion with React Native support

Documentation

Index

Constants

View Source
const (
	HeaderContentType string = "Content-Type"
	ContentTypeJson   string = "application/json"
	ContentTypeForm   string = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

func ApplyQueryToGorm added in v0.2.1

func ApplyQueryToGorm(db *gorm.DB, q query.Builder, paginate bool)

func AssertRecordDeleted added in v0.2.1

func AssertRecordDeleted[M any](t *testing.T, db *gorm.DB, input M, msg string)

func AssertValidID

func AssertValidID(t *testing.T, id ID)

func AssertValidInitialCreatedAt

func AssertValidInitialCreatedAt(t *testing.T, d time.Time)

func AssertValidInitialDeletedAt

func AssertValidInitialDeletedAt(t *testing.T, d *time.Time)

func AssertValidInitialUpdatedAt

func AssertValidInitialUpdatedAt(t *testing.T, d *time.Time)

func AssertValidModifiedUpdatedAt

func AssertValidModifiedUpdatedAt(t *testing.T, d *time.Time)

func BindRequest added in v0.2.1

func BindRequest[T any](cx *gin.Context) (T, error)

BindRequest returns a T with values set by binding the request JSON from the provided Gin context.

func EnvBool

func EnvBool(key string, defaultValue bool) bool

EnvBool returns the value of the specified env var or defaultValue if the env var is not set.

func EnvDuration added in v0.2.1

func EnvDuration(key string, defaultValue time.Duration) time.Duration

EnvDuration returns the value of the specified env var or defaultValue if the env var is not set.

func EnvFloat64 added in v0.2.1

func EnvFloat64(key string, defaultValue float64) float64

EnvFloat64 returns the value of the specified env var or defaultValue if the env var is not set.

func EnvInt added in v0.1.1

func EnvInt(key string, defaultValue int) int

EnvInt returns the value of the specified env var or defaultValue if the env var is not set.

func EnvInt32 added in v0.2.1

func EnvInt32(key string, defaultValue int32) int32

EnvInt32 returns the value of the specified env var or defaultValue if the env var is not set.

func EnvInt64 added in v0.2.1

func EnvInt64(key string, defaultValue int64) int64

EnvInt64 returns the value of the specified env var or defaultValue if the env var is not set.

func EnvIntSlice added in v0.2.1

func EnvIntSlice(key string, defaultValue []int) []int

EnvIntSlice returns the value of the specified env var or defaultValue if the env var is not set.

func EnvString

func EnvString(key string, defaultValue string) string

EnvString returns the value of the specified env var or defaultValue if the env var is not set.

func EnvStringMap added in v0.2.1

func EnvStringMap(key string, defaultValue map[string]any) map[string]any

EnvStringMap returns the value of the specified env var or defaultValue if the env var is not set.

func EnvStringMapString added in v0.2.1

func EnvStringMapString(key string, defaultValue map[string]string) map[string]string

EnvStringMapString returns the value of the specified env var or defaultValue if the env var is not set.

func EnvStringSlice added in v0.2.1

func EnvStringSlice(key string, defaultValue []string) []string

EnvStringSlice returns the value of the specified env var or defaultValue if the env var is not set.

func EnvTime added in v0.2.1

func EnvTime(key string, defaultValue time.Time) time.Time

EnvTime returns the value of the specified env var or defaultValue if the env var is not set.

func EnvUint16 added in v0.2.1

func EnvUint16(key string, defaultValue uint16) uint16

EnvUint16 returns the value of the specified env var or defaultValue if the env var is not set.

func EnvUint32 added in v0.2.1

func EnvUint32(key string, defaultValue uint32) uint32

EnvUint32 returns the value of the specified env var or defaultValue if the env var is not set.

func EnvUint64 added in v0.2.1

func EnvUint64(key string, defaultValue uint64) uint64

EnvUint64 returns the value of the specified env var or defaultValue if the env var is not set.

func ErrorBesidesRecordNotFound added in v0.2.1

func ErrorBesidesRecordNotFound(err error) bool

ErrorBesidesRecordNotFound true if the provided error is NOT a Gorm "record not found" error.

func ErrorsToError

func ErrorsToError(errs []error) error

func ExitError added in v0.1.1

func ExitError(err error)

func ExitSuccess

func ExitSuccess()

func GetDB added in v0.1.1

func GetDB(c DatabaseConfig) (*gorm.DB, error)

func GetLogger added in v0.1.1

func GetLogger() *zap.SugaredLogger

func GetMainDB added in v0.1.1

func GetMainDB() (*gorm.DB, error)

func GetStrictLogger added in v0.2.1

func GetStrictLogger() *zap.Logger

func GetUrl added in v0.2.1

func GetUrl(key ...string) *url.URL

func HandleCSVRows

func HandleCSVRows(path string, skipHeaderRow bool, breakOnEOF bool, callback CSVCallback) error

func HealthHandler added in v0.2.1

func HealthHandler(path string, message ...string) (string, gin.HandlerFunc)

func Init

func Init() error

Init initializes the Goat runtime services. Goat has three primary concerns, each encapsulated by their own service: - logging - database connections - route-based response hypermedia (linking) This function only needs to be called if you intend to use the database, http, or log services.

func MustInit added in v0.2.1

func MustInit()
func NewResourceLinks(key, path string) *hal.ResourceLinks

func NewValidationError added in v0.2.1

func NewValidationError(field, rule string) error

func OpenCSV

func OpenCSV(path string) (reader *csv.Reader, err error)

func ParseParam added in v0.2.1

func ParseParam[T any](cx *gin.Context, key string, parser ParamParser[T]) (T, error)

func PrintFail

func PrintFail(s string)

func PrintHeading

func PrintHeading(s string)

func PrintIndent

func PrintIndent(s string)

func PrintInfo

func PrintInfo(s string)

func PrintSuccess

func PrintSuccess(s string)

func PrintWarning

func PrintWarning(s string)

func RandomBool

func RandomBool() bool

func RandomDecimal

func RandomDecimal(min, max int64) decimal.Decimal

func RandomDecimalExp

func RandomDecimalExp(min, max int64, exp int32) decimal.Decimal

func RandomIndex

func RandomIndex(length int) int

func RandomInt

func RandomInt(min, max int) int

func RandomInt32

func RandomInt32(min, max int32) int32

func RandomInt64

func RandomInt64(min, max int64) int64

func RecordNotFound

func RecordNotFound(err error) bool

RecordNotFound returns true if the provided error is the Gorm "record not found" error.

func RequireDecimalEqual

func RequireDecimalEqual(t *testing.T, exp, act any, msgAndArgs ...any)

func RespondAccepted added in v0.2.1

func RespondAccepted(cx *gin.Context, data any)

func RespondBadRequest

func RespondBadRequest(cx *gin.Context, err error)

func RespondCreated

func RespondCreated(cx *gin.Context, data any)

func RespondForbidden added in v0.2.1

func RespondForbidden(cx *gin.Context, err error)

func RespondNoContent added in v0.2.1

func RespondNoContent(cx *gin.Context)

func RespondNotFound added in v0.2.1

func RespondNotFound(cx *gin.Context, err error)

func RespondOk added in v0.2.1

func RespondOk(cx *gin.Context, data any)

func RespondServerError

func RespondServerError(cx *gin.Context, err error)

func RespondUnauthorized added in v0.2.1

func RespondUnauthorized(cx *gin.Context, err error)

func RespondUsed added in v0.2.1

func RespondUsed(cx *gin.Context, data any)

func RespondValidationError

func RespondValidationError(cx *gin.Context, err error)

func SetUrl added in v0.2.1

func SetUrl(key string, value *url.URL)

func TimeToPrettyString

func TimeToPrettyString(t time.Time) string

func TimeToYMDHISString

func TimeToYMDHISString(t time.Time) string

func TimeToYMDString

func TimeToYMDString(t time.Time) string

func ValueOrDefault added in v0.2.1

func ValueOrDefault[T any](input *T, defaultValue T) T

ValueOrDefault returns the value of input or defaultValue if input is nil.

func ValueToString added in v0.2.1

func ValueToString(value any) (string, error)

ValueToString attempts to cast the provided value to a string and returns the result.

func VersionHandler added in v0.2.1

func VersionHandler(path string, version ...string) (string, gin.HandlerFunc)

func YMDHISStringToTime

func YMDHISStringToTime(str string) (time.Time, error)

func YMDStringToTime

func YMDStringToTime(str string) (time.Time, error)

Types

type ApiProblem added in v0.2.1

type ApiProblem hal.ApiProblem

type CSVCallback added in v0.1.1

type CSVCallback func(line []string, eof bool) error

type Collection added in v0.2.1

type Collection[T any] hal.Collection[T]

type DatabaseConfig added in v0.2.1

type DatabaseConfig database.Config

type HandlerTest

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

func NewHandlerTest

func NewHandlerTest(router Router) *HandlerTest

func (*HandlerTest) NewRequest added in v0.2.1

func (h *HandlerTest) NewRequest(path string) (*Request, error)

NewRequest creates a new Request using the HandlerTest remoteAddr as the base url.

func (*HandlerTest) Send

func (*HandlerTest) SetRemoteAddr

func (h *HandlerTest) SetRemoteAddr(ip string) *HandlerTest

SetRemoteAddr overwrites the default remote address to use when sending requests.

func (*HandlerTest) SetRequest added in v0.2.1

func (h *HandlerTest) SetRequest(request *Request) *HandlerTest

type ID

type ID uuid.UUID

ID is a UUID type that implements a custom Value function for storing UUIDs as binary(16) columns in a database using Gorm.

func GetIDParam added in v0.2.1

func GetIDParam(cx *gin.Context) (ID, error)

func NewID

func NewID() ID

func NilID

func NilID() ID

func ParseAllIDs

func ParseAllIDs(s []string) ([]ID, error)

func ParseID

func ParseID(s string) (ID, error)

func (ID) MarshalBinary

func (id ID) MarshalBinary() ([]byte, error)

MarshalBinary implements encoding.BinaryMarshaler.

func (ID) MarshalText

func (id ID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*ID) Scan

func (id *ID) Scan(src any) error

func (ID) String

func (id ID) String() string

func (*ID) UnmarshalBinary

func (id *ID) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (ID) Valid

func (id ID) Valid() bool

func (ID) Value

func (id ID) Value() (driver.Value, error)

type ParamParser added in v0.2.1

type ParamParser[T any] func(string) (T, error)

type RedirectHandler added in v0.2.1

type RedirectHandler func(req *http.Request, via []*http.Request) error

type Request added in v0.2.1

type Request struct {
	Method  string
	URL     string
	Headers map[string]string
	// contains filtered or unexported fields
}

func NewRequest added in v0.2.1

func NewRequest(baseUrl string) (*Request, error)

func (*Request) AddFormParam added in v0.2.1

func (r *Request) AddFormParam(key, value string) *Request

func (*Request) AddQueryParam added in v0.2.1

func (r *Request) AddQueryParam(key, value string) *Request

func (*Request) Build added in v0.2.1

func (r *Request) Build() (*http.Request, error)

func (*Request) GetBodyForm added in v0.2.1

func (r *Request) GetBodyForm() (io.Reader, error)

func (*Request) GetBodyJSON added in v0.2.1

func (r *Request) GetBodyJSON() (io.Reader, error)

func (*Request) Send added in v0.2.1

func (r *Request) Send() (*http.Response, error)

func (*Request) SendAndRead added in v0.2.1

func (r *Request) SendAndRead(output any) (*http.Response, error)

func (*Request) SetBodyForm added in v0.2.1

func (r *Request) SetBodyForm(data url.Values) *Request

func (*Request) SetBodyJSON added in v0.2.1

func (r *Request) SetBodyJSON(body any) *Request

func (*Request) SetContentTypeForm added in v0.2.1

func (r *Request) SetContentTypeForm() *Request

func (*Request) SetContentTypeJSON added in v0.2.1

func (r *Request) SetContentTypeJSON() *Request

func (*Request) SetFormParam added in v0.2.1

func (r *Request) SetFormParam(key, value string) *Request

func (*Request) SetHeader added in v0.2.1

func (r *Request) SetHeader(key, value string) *Request

func (*Request) SetMethod added in v0.2.1

func (r *Request) SetMethod(method string) *Request

func (*Request) SetQueryParam added in v0.2.1

func (r *Request) SetQueryParam(key, value string) *Request

func (*Request) SetRedirectHandler added in v0.2.1

func (r *Request) SetRedirectHandler(handler RedirectHandler) *Request

type RequestData added in v0.2.1

type RequestData url.Values

RequestData is an alias of url.Values that supports method chaining.

func MakeRequestData added in v0.2.1

func MakeRequestData() RequestData

func (RequestData) Add added in v0.2.1

func (v RequestData) Add(key, value string) RequestData

func (RequestData) Del added in v0.2.1

func (v RequestData) Del(key string) RequestData

func (RequestData) Get added in v0.2.1

func (v RequestData) Get(key string) string

func (RequestData) Has added in v0.2.1

func (v RequestData) Has(key string) bool

func (RequestData) Set added in v0.2.1

func (v RequestData) Set(key, value string) RequestData

func (RequestData) Values added in v0.2.1

func (v RequestData) Values() url.Values

type Resource added in v0.2.1

type Resource hal.Resource

type Router

type Router http.Router

func InitRouter added in v0.2.1

func InitRouter(baseUrl ...string) (Router, error)

Directories

Path Synopsis
sys
log

Jump to

Keyboard shortcuts

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