minimal

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: MIT Imports: 22 Imported by: 0

README

Minimal

Minimal is a small, opinionated wrapper around Echo and Gorm.
Postgres set up and ready to go.

Absolute minimal configuration for a development environment

//go:embed assets
var embeddedFiles embed.FS

func embedFS(fs embed.FS) http.FileSystem {
	return http.FS(fs)
}

func main() {
	config := minimal.DevelopmentConfig
	dsn = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=5432 sslmode=disable TimeZone=Europe/Oslo",
		"localhost",
		"postgres",
		"postgres",
		"tmp",
	)
	config.DSN = dsn

	s := minimal.New(config, []minimal.Provider{
		&BaseRoutes{},
	}, []any{})

	s.Init(embedFS(embeddedFiles))
}

Routes defined like this:

type BaseRoutes struct{}

func (br *BaseRoutes) Register(e *echo.Echo) {
	e.GET("/", func(c echo.Context) error {
		return c.Render(200, "assets/index.html", nil)
	})
}

Res package

Instead of using c.JSON, you can use the res package which wraps your data type in a general success and failure struct.

Auto-generated API Resource

The snippet below will set up a REST endpoint that has CRUD operations on the Test model.

type UpdateTest struct {
	Name        string
	Description string
}

type Test struct {
	repository.BaseModel

	Name        string
	Description string
}

type TestResource struct {
	minimal.Resource[Test]
}

func NewTestResource() *TestResource {
	api := TestResource{
		minimal.Resource[Test]{
			Name: "tests",
		},
	}

	api.SetWriteBindType(&UpdateTest{})
	api.SetCreateBindType(&UpdateTest{})

	api.CanDeleteById(func(c echo.Context) bool {
		return false
	})

	return &api
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorNoResourceAccess = errors.New("no resource access")
	ErrorNoResourceFound  = errors.New("no resource found")
	ErrorDatabase         = errors.New("database problem")
	ErrorNoBindType       = errors.New("unable to handle this request")
	ErrorInvalidData      = errors.New("bad data")
	ErrorInvalidID        = errors.New("bad id")
)
View Source
var (
	DevelopmentConfig = Config{
		DSN:             "",
		HttpPort:        80,
		AutoTLS:         false,
		Domains:         []string{},
		FriendlyLogging: true,
	}
)

Functions

func AddMiddlewares

func AddMiddlewares(e *echo.Echo)

func Logging

func Logging(e *echo.Echo, friendly bool)

Types

type Config

type Config struct {
	DSN string

	HttpPort uint

	// Whether to use ACME auto-tls.
	AutoTLS bool

	CertKeyPath        string
	CertPrivateKeyPath string

	// FriendlyLogging makes logging look nice instead of wrapping it into JSON.
	FriendlyLogging bool

	Domains []string
}

type Provider

type Provider interface {
	Register(e *echo.Echo)
}

type Resource added in v1.1.2

type Resource[T any] struct {
	Name string
	// contains filtered or unexported fields
}

Resource is an automatic REST api module which lets the consumer simply define the resource and it will have associated database code, et.c. automatically set up.

func (*Resource[T]) CanDeleteById added in v1.1.2

func (r *Resource[T]) CanDeleteById(predicate func(c echo.Context, entity T) bool)

CanDeleteById takes a predicate and determines whether the operation can proceed.

func (*Resource[T]) CanListAll added in v1.1.2

func (r *Resource[T]) CanListAll(predicate func(c echo.Context) bool)

CanListAll takes a predicate and determines whether the operation can proceed.

func (*Resource[T]) CanListById added in v1.1.2

func (r *Resource[T]) CanListById(predicate func(c echo.Context, entity T) bool)

CanListById takes a predicate and determines whether the operation can proceed.

func (*Resource[T]) CanWriteById added in v1.1.4

func (r *Resource[T]) CanWriteById(predicate func(c echo.Context, entity T) bool)

CanWriteById takes a predicate and determines whether the operation can proceed.

func (*Resource[T]) Middlewares added in v1.1.3

func (r *Resource[T]) Middlewares(m ...echo.MiddlewareFunc)

func (*Resource[T]) OnRegister added in v1.1.2

func (r *Resource[T]) OnRegister(f func(e *echo.Echo))

OnRegister sets the registration hook to argument f.

func (*Resource[T]) OverrideDeleteByIdQuery added in v1.1.10

func (r *Resource[T]) OverrideDeleteByIdQuery(predicate func(c echo.Context, q *gorm.DB, entity T) error)

OverrideDeleteByIdQuery lets consumers override the query used in the "Delete By Id" operation.

func (*Resource[T]) OverrideListAllQuery added in v1.1.2

func (r *Resource[T]) OverrideListAllQuery(predicate func(c echo.Context, q *gorm.DB) ([]T, error))

OverrideListAllQuery lets consumers override the query used in the "List All" operation.

func (*Resource[T]) OverrideListByIdQuery added in v1.1.2

func (r *Resource[T]) OverrideListByIdQuery(predicate func(c echo.Context, q *gorm.DB, id uint) (*T, error))

OverrideListByIdQuery lets consumers override the query used in the "List By Id" operation.

func (*Resource[T]) Register added in v1.1.2

func (r *Resource[T]) Register(e *echo.Echo)

Register is called when minimal initializes, and will add routes and trigger the automigration.

func (*Resource[T]) SetCreateBindType added in v1.1.2

func (r *Resource[T]) SetCreateBindType(t any)

SetCreateBindType will typically be a DTO struct.

func (*Resource[T]) SetCreateTransformer added in v1.1.8

func (r *Resource[T]) SetCreateTransformer(tf func(c echo.Context) (*T, error))

func (*Resource[T]) SetWriteBindType added in v1.1.2

func (r *Resource[T]) SetWriteBindType(t any)

SetWriteBindType will typically be a DTO struct.

type Server

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

func New

func New(config Config, routes []Provider, models []any) Server

New creates a minimal Server instance. This is a 'minimal' example of how to configure the library:

//go:embed assets
var embeddedFiles embed.FS

func embedFS(fs embed.FS) http.FileSystem {
	return http.FS(fs)
}

type BaseRoutes struct{}

func (br *BaseRoutes) Register(e *echo.Echo) {
	e.GET("/", func(c echo.Context) error {
		return c.Render(200, "assets/index.html", nil)
	})
}

func main() {
	config := server.DevelopmentConfig
	_ = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=5432 sslmode=disable TimeZone=Europe/Oslo",
		"localhost",
		"postgres",
		"postgres",
		"tmp",
	)
	config.DSN = dsn

	s := server.New(config, []provider.RouteProvider{
		&BaseRoutes{},
	}, []any{})

	s.Init(embedFS(embeddedFiles))
}

func (*Server) Echo

func (s *Server) Echo() *echo.Echo

func (*Server) Init

func (s *Server) Init(fs http.FileSystem)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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