eather

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2019 License: MIT Imports: 26 Imported by: 0

README

EATHER Project

Application that loading Go plugins that are created by predefined interface using built-in buildmode=plugin.

Overview

Getting started

Simply use the HelloWorld application or start fresh by running

go get -u github.com/EatherGo/eather

Then start server

package main

import (
	"github.com/EatherGo/eather"
)

func main() {
	eather.Start(nil)
}

Copy .env.example to your project as .env with your own settings.

Access http://localhost:8000/ or APP_URL from ENV, and you should see 404 page not found or Hello world and you are ready to start.

Development

Create module

Modules are loading automatically from folders defined in ENV CORE_MODULES_DIR and CUSTOM_MODULES_DIR. If you are using more than two directories for modules there is a posibility to add new modules directory through config. Each module need to be defined by /etc/module.xml in module directory and enabled in global ./config/modules.xml. Modules are builded using buildmode=plugin and module.so is loaded by application. Modules can be Eventable, Installable, Upgradable, Routable, Cronable, Callable described in module.go

Empty module

Create etc directory with module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module>
    <name>Empty</name>
    <version>1.0.0</version>
</module>

Name is used for defining a name for module and version for versioning.

Create ./config/modules.xml and set module to enabled.

<?xml version="1.0" encoding="UTF-8"?>
<modules>
    <module>
        <name>Empty</name>
        <enabled>true</enabled>
    </module>
</modules>

Create main.go with function called same as name of module which should return module itself and error.

package main

import (
	"github.com/EatherGo/eather"
)

type module struct{}

// Empty to export in plugin
func Empty() (f eather.Module, err error) {
	f = module{}
	return
}

Run your application and you should see this output.

Module Empty is not builded. Building...
Module Empty was builded
Module Empty is running 

Now your Empty module is running.

Evantable module

To make module Eventable add function GetEventFuncs() to your main.go

.
.
.

func (m module) GetEventFuncs() []eather.Fire {
	return eventFuncs
}

var eventFuncs = []eather.Fire{
	eather.Fire{Call: "added", Func: added},
	eather.Fire{Call: "removed", Func: removed},
}

var added = func(data ...interface{}) {
	fmt.Println(data)
	time.Sleep(2 * time.Second)
	fmt.Println("Running event after added")
}

var removed = func(data ...interface{}) {
	fmt.Println(data)
	time.Sleep(2 * time.Second)
	fmt.Println("Running event after removed")
}


.
.
.

And add to module.xml listeners for this events

<?xml version="1.0" encoding="UTF-8"?>
<module>
    <name>Empty</name>
    <version>1.0.0</version>
    <events>
        <listener for="test_added" call="added" name="add_some_stuff"></listener>
        <listener for="test_removed" call="removed" name="remove_some_stuff"></listener>
    </events>
</module>

Now your functions added can be triggered by calling eather.GetEvents().Emmit("test_added", map[string]interface{}{"code": "test_code"})

Installable module

To make module Installable you need to add function Install() to your module. This function will run only once when module is installing and is stored to database. Usually used for migrating to database.

.
.
.

func (m module) Install() {
	eather.GetDb().AutoMigrate(&YourModel{})
}

// YourModel struct
type YourModel struct {
	eather.ModelBase
}

.
.
.
Upgradable module

Add Upgrade(version) function to your module to make it Upgradable. This function is called every time you upgrade module version in /etc/module.xml.

.
.
.

func (m module) Upgrade(version string) {
	
}

.
.
.
Routable module

Add function MapRoutes() to make module Routable.

.
.
.

func (m module) MapRoutes() {
	router := eather.GetRouter()

	router.HandleFunc("/", controllerEmpty).Methods("GET")
}

func controllerEmpty(w http.ResponseWriter, r *http.Request) {
	eather.SendJSONResponse(w, eather.Response{Message: "Empty controller"})
}

.
.
.

This will set controllerEmpty for path /.

Cronable module

Add function Crons() and module will be Cronable.

.
.
.

func (m module) Crons() eather.CronList {
	return eather.CronList{
		eather.Cron{Spec: "* * * * *", Cmd: func() { fmt.Println("test") }},
	}
}

.
.
.

This will run every second function Cmd and will print test into the terminal.

Callable module

Add function GetPublicFuncs() to make module Callable. This will allow to call you any function of your module in other parts of application or in another module.

.
.
.

func (m module) GetPublicFuncs() eather.PublicFuncList {
	list := make(eather.PublicFuncList)

	list["test"] = test

	return list
}

func test(data ...interface{}) (interface{}, error) {
	return []string{"testing public function of module"}, nil
}

.
.
.

Now it is possible to call this function from any part of application. It will print the return of test function to terminal.

    if callable := eather.GetRegistry().Get("Empty").GetCallable(); callable != nil {
		data, _ := callable.GetPublicFuncs().Call("test")

		fmt.Println(data)
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCache added in v0.3.0

func GetCache() *bigcache.BigCache

GetCache to get cache instance

func GetRouter added in v0.3.0

func GetRouter() *mux.Router

GetRouter - return route collection

func InitVersion added in v0.3.0

func InitVersion()

InitVersion - initialize version with automigration

func LoadModules added in v0.3.0

func LoadModules(dirs []string)

LoadModules will load all modules inside modules directory

func RegisterRoutes added in v0.3.0

func RegisterRoutes(corsOpts *cors.Cors)

RegisterRoutes - listen for routes

func SendJSONResponse added in v0.3.0

func SendJSONResponse(w http.ResponseWriter, r Response)

SendJSONResponse will set type to application/json and send to response

func Start

func Start(conf ConfigInterface)

Start eather application initialize http server and load all modules

func StartCrons added in v0.3.0

func StartCrons(cronList []Cron)

StartCrons will start crons in background

Types

type Callable added in v0.5.0

type Callable interface {
	GetPublicFuncs() PublicFuncList
}

Callable interface for modules that are with GetPublicFuncs posibility to add custom public functions

type Cmd added in v0.3.0

type Cmd func()

Cmd function of cron

type Config added in v0.3.0

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

Config structure of config for Eather

func (*Config) AddCron added in v0.3.0

func (c *Config) AddCron(spec Spec, cmd Cmd)

AddCron will append new cron to the config

func (*Config) AddModuleDirs added in v0.3.2

func (c *Config) AddModuleDirs(dir ...string)

AddModuleDirs will replace moduleDirs by new

func (*Config) GetCorsOpts added in v0.4.0

func (c *Config) GetCorsOpts() *cors.Cors

GetCorsOpts returns cor config

func (*Config) GetCrons added in v0.3.2

func (c *Config) GetCrons() CronList

GetCrons returns slice of Cron

func (*Config) GetModuleDirs added in v0.3.2

func (c *Config) GetModuleDirs() []string

GetModuleDirs returns directories of modules

func (*Config) GetServerConf added in v0.4.0

func (c *Config) GetServerConf() *http.Server

GetServerConf returns server configuration

func (*Config) SetCorsOpts added in v0.4.0

func (c *Config) SetCorsOpts(cors *cors.Cors)

SetCorsOpts will set cors for application

func (*Config) SetServerConfig added in v0.4.0

func (c *Config) SetServerConfig(s *http.Server)

SetServerConfig will set server configuration

type ConfigInterface added in v0.4.0

type ConfigInterface interface {
	AddCron(spec Spec, cmd Cmd)
	AddModuleDirs(dir ...string)
	SetCorsOpts(cors *cors.Cors)
	SetServerConfig(*http.Server)

	GetCrons() CronList
	GetModuleDirs() []string
	GetCorsOpts() *cors.Cors
	GetServerConf() *http.Server
}

ConfigInterface interface of config

func GetConfig added in v0.3.2

func GetConfig() ConfigInterface

GetConfig will return default config settings

type Cron added in v0.3.0

type Cron struct {
	Spec Spec
	Cmd  Cmd
}

Cron structure

type CronList added in v0.3.0

type CronList []Cron

CronList structure of list of crons

type Cronable added in v0.5.0

type Cronable interface {
	Crons() CronList
}

Cronable interface for modules that are with func Crons return cronlist to add custom crons from modules to global list

type DataResponse added in v0.3.1

type DataResponse map[string]interface{}

DataResponse set DataResponse type

type Database added in v0.3.0

type Database struct {
	*gorm.DB
}

Database struct - structure of database

func GetDb added in v0.3.0

func GetDb() *Database

GetDb - get instance of database

type DatabaseCreatedAt added in v0.4.0

type DatabaseCreatedAt struct {
	CreatedAt time.Time `json:",omitempty"`
}

DatabaseCreatedAt set default created_at column

type DatabaseDeletedAt added in v0.4.0

type DatabaseDeletedAt struct {
	DeletedAt *time.Time `sql:"index" json:",omitempty"`
}

DatabaseDeletedAt set default deleted_at column

type DatabaseID added in v0.4.0

type DatabaseID struct {
	ID uuid.UUID `gorm:"primary_key" json:",omitempty"`
}

DatabaseID set default ID column

func (*DatabaseID) BeforeCreate added in v0.4.0

func (base *DatabaseID) BeforeCreate(scope *gorm.Scope) error

BeforeCreate will set a UUID rather than numeric ID.

type DatabaseUpdatedAt added in v0.4.0

type DatabaseUpdatedAt struct {
	UpdatedAt time.Time `json:",omitempty"`
}

DatabaseUpdatedAt set default updated_at column

type Event added in v0.4.0

type Event struct {
	Name  string `json:"name"`
	Fires []Fire `json:"fires"`
}

Event structure

type EventCollection added in v0.3.0

type EventCollection map[string]Event

EventCollection is definition of events collection

type EventFunc added in v0.4.0

type EventFunc func(data ...interface{})

EventFunc type of events func

type Eventable added in v0.4.0

type Eventable interface {
	GetEventFuncs() []Fire
}

Eventable interface for modules that are with events add this func to module to enable events

type Events added in v0.3.0

type Events struct {
	Collection EventCollection
}

Events struct - collection of events

func (*Events) Add added in v0.3.0

func (r *Events) Add(eventName string, f EventFunc, call string, name string)

Add event to the collection

func (*Events) Emmit added in v0.3.0

func (r *Events) Emmit(name string, data ...interface{})

Emmit the event from the collection data will be passed to the event func

func (*Events) GetCollection added in v0.3.0

func (r *Events) GetCollection() EventCollection

GetCollection will return collection of events

func (*Events) Remove added in v0.3.0

func (r *Events) Remove(name string)

Remove the event from the collection

type EventsInterface added in v0.3.0

type EventsInterface interface {
	Emmit(name string, data ...interface{})
	Add(eventName string, f EventFunc, call string, name string)
	Remove(name string)
	GetCollection() EventCollection
}

EventsInterface interface of events

func GetEvents added in v0.3.0

func GetEvents() EventsInterface

GetEvents - get collection of all registered events

type Fire added in v0.4.0

type Fire struct {
	Call string    `json:"call"`
	Name string    `json:"name"`
	Func EventFunc `json:"-"`
}

Fire struct of Fires

type Installable added in v0.4.0

type Installable interface {
	Install()
}

Installable interface for modules that are with func Install add this func to run install func during installation of module

type ModelBase added in v0.4.0

ModelBase contains common columns for all tables.

type Module added in v0.3.0

type Module interface{}

Module interface

type ModuleVersion added in v0.3.0

type ModuleVersion struct {
	ID uint `gorm:"primary_key" json:",omitempty"`
	DatabaseCreatedAt
	DatabaseUpdatedAt
	DatabaseDeletedAt
	Name    string `json:",omitempty"`
	Version string `json:",omitempty"`
}

ModuleVersion struct - structure of moduleVersion in database

type ModuleXML added in v0.4.0

type ModuleXML types.ModuleXML

ModuleXML of type ModuleXML

func (ModuleXML) GetVersion added in v0.4.0

func (m ModuleXML) GetVersion() string

GetVersion - load version from database

func (ModuleXML) UpdateVersion added in v0.4.0

func (m ModuleXML) UpdateVersion()

UpdateVersion - set the new version of the module to the database

type PublicFunc added in v0.5.0

type PublicFunc func(data ...interface{}) (interface{}, error)

PublicFunc function type for public function

type PublicFuncList added in v0.5.0

type PublicFuncList map[string]PublicFunc

PublicFuncList is a list of function

func (PublicFuncList) Call added in v0.5.0

func (pfl PublicFuncList) Call(name string, data ...interface{}) (i interface{}, err error)

Call function to call the public function of module

type Registry added in v0.3.0

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

Registry struct - collection for registry

func (*Registry) Add added in v0.3.0

func (r *Registry) Add(object Module, name string)

Add module to the registry object

func (*Registry) Contains added in v0.3.0

func (r *Registry) Contains(name string) bool

Contains check if module is already in registry

func (*Registry) Get added in v0.3.0

Get module from registry by name

func (*Registry) GetCollection added in v0.4.0

func (r *Registry) GetCollection() RegistryCollection

GetCollection returns collection of all modules

func (*Registry) Remove added in v0.3.0

func (r *Registry) Remove(name string)

Remove module from collection

type RegistryCollection added in v0.4.0

type RegistryCollection map[string]RegistryModule

RegistryCollection map of all modules in registry

type RegistryInterface added in v0.3.0

type RegistryInterface interface {
	Get(name string) RegistryModuleInterface
	GetCollection() RegistryCollection
	Add(object Module, name string)
	Contains(name string) bool
	Remove(name string)
}

RegistryInterface - interface for a registry

func GetRegistry added in v0.3.0

func GetRegistry() RegistryInterface

GetRegistry load registry collection

type RegistryModule added in v0.5.0

type RegistryModule struct {
	Module Module
}

RegistryModule structure

func (RegistryModule) GetCallable added in v0.5.0

func (rm RegistryModule) GetCallable() Callable

GetCallable will return nil or callable interface

func (RegistryModule) GetCronable added in v0.5.0

func (rm RegistryModule) GetCronable() Cronable

GetCronable will return nil or cronable interface

type RegistryModuleInterface added in v0.5.0

type RegistryModuleInterface interface {
	GetCallable() Callable
	GetCronable() Cronable
}

RegistryModuleInterface interface for registryModule

type Response added in v0.3.0

type Response struct {
	Status     bool         `json:"status"`
	Message    string       `json:"message"`
	Data       DataResponse `json:"data"`
	StatusCode int          `json:"statusCode"`
}

Response struct - customize response for routes

type Routable added in v0.4.0

type Routable interface {
	MapRoutes()
}

Routable interface for modules that are with func Routable add this func to map routes for module

type Routes added in v0.3.0

type Routes struct {
	Collection map[string]func(w http.ResponseWriter, r *http.Request) Response
}

Routes struct - collection of routes

type Spec added in v0.3.0

type Spec string

Spec for specification when cron should be running

type Upgradable added in v0.4.0

type Upgradable interface {
	Upgrade(version string)
}

Upgradable interface for modules that are with func Upgrade add this func to run Upgrade after upgrading module version

Directories

Path Synopsis
examples
HelloWorld command

Jump to

Keyboard shortcuts

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