context

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0 Imports: 11 Imported by: 4

README

Procyon Context

Go Report Card codecov Build Status Gitter PkgGoDev

This gives you a basic understanding of Procyon Context Module. It covers components provided by the framework, such as Logger, Properties, Initializers and Events.

Logger

It provides an interface for loggers. A logger is provided by the framework. You can get it in construction function by using the dependency injection. If you want to implement your logger, you need to implement the interface.

type Logger interface {
	Trace(ctx interface{}, message interface{})
	Debug(ctx interface{}, message interface{})
	Info(ctx interface{}, message interface{})
	Warning(ctx interface{}, message interface{})
	Error(ctx interface{}, message interface{})
	Fatal(ctx interface{}, message interface{})
	Panic(ctx interface{}, message interface{})
	Tracef(ctx interface{}, format string, args ...interface{})
	Debugf(ctx interface{}, format string, args ...interface{})
	Infof(ctx interface{}, format string, args ...interface{})
	Warningf(ctx interface{}, format string, args ...interface{})
	Errorf(ctx interface{}, format string, args ...interface{})
	Fatalf(ctx interface{}, format string, args ...interface{})
	Panicf(ctx interface{}, format string, args ...interface{})
}

Configuration Properties

This interface is used to bind the command-line parameters to your struct's instance.

type ConfigurationProperties interface {
	GetPrefix() string
}

The example is given below. Note that you need to register your properties by using the function core.Register. Otherwise, their instances won't be created by the framework.

type MyConfigurationProperties struct {
	    Name                string  `yaml:"name" json:"name" default:"Test Application"`
        CustomPort          uint    `yaml:"port" json:"port" default:"8090"`
}

func NewMyConfigurationProperties() *MyConfigurationProperties() {
    return &MyConfigurationProperties{}
}

func (properties *MyConfigurationProperties) GetPrefix() string  {
    return "application"
}

When you specify the parameters --application.name and --application.port, they will be bind to your instance. Otherwise, their default values will be used.

Application Context Initializer

This interface is used to initialize the context by custom context initializer. It is invoked while the context is prepared.

type ApplicationContextInitializer interface {
	InitializeContext(context ConfigurableApplicationContext)
}

The example of a custom context initializer is given below. Note that you need to register your initializer by using the function core.Register. Otherwise, their instances won't be created by the framework.

type CustomContextInitializer struct {

}

func NewCustomContextInitializer() CustomContextInitializer {
    return CustomContextInitializer{}
}

func (initializer CustomContextInitializer) InitializeContext(context ConfigurableApplicationContext) {
    // do whatever you want
}

Application Event

All events have to implement this interface. You can have custom events by implementing the interface.

type ApplicationEvent interface {
	GetEventId() ApplicationEventId
	GetParentEventId() ApplicationEventId
	GetSource() interface{}
	GetTimestamp() int64
}
  • GetEventId returns the unique id of the event.
  • GetParentEventId returns the unique id of the parent event of the event. Parent Event is not necessary.
  • GetSource returns the object with which the event is associated.
  • GetTimestamp returns the system time in milliseconds when the event occurred.

The example is given below.

First, Your event has to have an unique id, you can get it by using the function context.GetEventId.

var customEventId = context.GetEventId("github.com.procyon.CustomEvent")

func CustomEventId() ApplicationEventId {
	return customEventId
}

The second thing you need to do is to implement the interface context.ApplicationEvent It's shown below.

type CustomEvent struct {
	source    interface{}
	timestamp int64
}

func NewCustomEvent(obj interface) CustomEvent {
	return CustomEvent{
		source:    obj,
		timestamp: time.Now().Unix(),
	}
}

func (event CustomEvent) GetEventId() context.ApplicationEventId {
	return customEventId
}

func (event CustomEvent) GetParentEventId() context.ApplicationEventId {
	return -1
}

func (event CustomEvent) GetSource() interface{} {
	return event.source
}

func (event CustomEvent) GetTimestamp() int64 {
	return event.timestamp
}

Application Listener

This interface need to be implemented by application event listeners. Event Ids to be subscribed need to be returned by SubscribeEvents. All events must have an unique id. Otherwise, there will be conflicts. That's why when you want to create a custom event, use the function context.GetEventId to have an event id.

type ApplicationListener interface {
	SubscribeEvents() []ApplicationEventId
	OnApplicationEvent(context Context, event ApplicationEvent)
}

The example is given below. Note that you need to register your listeners by using the function core.Register. Otherwise, their instances won't be created by the framework.

type CustomEventListener interface {

}
	
func NewCustomEventListener() CustomEventListener {
    return CustomEventListener{}
}	

func (listener CustomEventListener) SubscribeEvents() []context.ApplicationEventId {
    return []context.ApplicationEventId {
        CustomEventId(),
    }
}

func (listener CustomEventListener) OnApplicationEvent(context context.Context, event context.ApplicationEvent) {
    // do whatever you want...
}

Application Event Publisher

It is used to notify all matching listeners registered. Events might be framework events or application-specific events. A framework event publisher is provided by the framework. You can get it in construction function by using the dependency injection. It's recommend to use for async execution. Also, you can have your custom event publisher by implementing this interface.

type ApplicationEventPublisher interface {
	PublishEvent(context Context, event ApplicationEvent)
}

The example of a custom event publisher is given below. Note that you need to register your publisher by using the function core.Register. Otherwise, their instances won't be created by the framework.

type CustomEventPublisher struct {
	
}

func NewCustomEventPublisher() CustomEventPublisher  {
	return CustomEventPublisher{}
}

func (publisher CustomEventPublisher) PublishEvent(context context.Context, event context.ApplicationEvent) {
    // do whatever you want...
}

License

Procyon Framework is released under version 2.0 of the Apache License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplicationContext

type ApplicationContext interface {
	Context
	peas.ConfigurablePeaFactory
	GetAppId() ApplicationId
	GetApplicationName() string
	GetStartupTimestamp() int64
}

type ApplicationContextClosedEvent

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

func NewApplicationContextClosedEvent

func NewApplicationContextClosedEvent(source ApplicationContext) ApplicationContextClosedEvent

func (ApplicationContextClosedEvent) GetApplicationContext

func (event ApplicationContextClosedEvent) GetApplicationContext() ApplicationContext

func (ApplicationContextClosedEvent) GetEventId

func (ApplicationContextClosedEvent) GetParentEventId

func (event ApplicationContextClosedEvent) GetParentEventId() ApplicationEventId

func (ApplicationContextClosedEvent) GetSource

func (event ApplicationContextClosedEvent) GetSource() interface{}

func (ApplicationContextClosedEvent) GetTimestamp

func (event ApplicationContextClosedEvent) GetTimestamp() int64

type ApplicationContextEvent

type ApplicationContextEvent interface {
	ApplicationEvent
	GetApplicationContext() ApplicationContext
}

type ApplicationContextInitializer

type ApplicationContextInitializer interface {
	InitializeContext(context ConfigurableApplicationContext)
}

type ApplicationContextRefreshedEvent

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

func NewApplicationContextRefreshedEvent

func NewApplicationContextRefreshedEvent(source ApplicationContext) ApplicationContextRefreshedEvent

func (ApplicationContextRefreshedEvent) GetApplicationContext

func (event ApplicationContextRefreshedEvent) GetApplicationContext() ApplicationContext

func (ApplicationContextRefreshedEvent) GetEventId

func (ApplicationContextRefreshedEvent) GetParentEventId

func (event ApplicationContextRefreshedEvent) GetParentEventId() ApplicationEventId

func (ApplicationContextRefreshedEvent) GetSource

func (event ApplicationContextRefreshedEvent) GetSource() interface{}

func (ApplicationContextRefreshedEvent) GetTimestamp

func (event ApplicationContextRefreshedEvent) GetTimestamp() int64

type ApplicationContextStartedEvent

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

func NewApplicationContextStartedEvent

func NewApplicationContextStartedEvent(source ApplicationContext) ApplicationContextStartedEvent

func (ApplicationContextStartedEvent) GetApplicationContext

func (event ApplicationContextStartedEvent) GetApplicationContext() ApplicationContext

func (ApplicationContextStartedEvent) GetEventId

func (ApplicationContextStartedEvent) GetParentEventId

func (event ApplicationContextStartedEvent) GetParentEventId() ApplicationEventId

func (ApplicationContextStartedEvent) GetSource

func (event ApplicationContextStartedEvent) GetSource() interface{}

func (ApplicationContextStartedEvent) GetTimestamp

func (event ApplicationContextStartedEvent) GetTimestamp() int64

type ApplicationContextStoppedEvent

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

func NewApplicationContextStoppedEvent

func NewApplicationContextStoppedEvent(source ApplicationContext) ApplicationContextStoppedEvent

func (ApplicationContextStoppedEvent) GetApplicationContext

func (event ApplicationContextStoppedEvent) GetApplicationContext() ApplicationContext

func (ApplicationContextStoppedEvent) GetEventId

func (ApplicationContextStoppedEvent) GetParentEventId

func (event ApplicationContextStoppedEvent) GetParentEventId() ApplicationEventId

func (ApplicationContextStoppedEvent) GetSource

func (event ApplicationContextStoppedEvent) GetSource() interface{}

func (ApplicationContextStoppedEvent) GetTimestamp

func (event ApplicationContextStoppedEvent) GetTimestamp() int64

type ApplicationEvent

type ApplicationEvent interface {
	GetEventId() ApplicationEventId
	GetParentEventId() ApplicationEventId
	GetSource() interface{}
	GetTimestamp() int64
}

type ApplicationEventBroadcaster

type ApplicationEventBroadcaster interface {
	RegisterApplicationListener(listener ApplicationListener)
	UnregisterApplicationListener(listener ApplicationListener)
	RemoveAllApplicationListeners()
	BroadcastEvent(context ApplicationContext, event ApplicationEvent)
}

type ApplicationEventId

type ApplicationEventId uint64

func ApplicationContextClosedEventId

func ApplicationContextClosedEventId() ApplicationEventId

func ApplicationContextEventId

func ApplicationContextEventId() ApplicationEventId

func ApplicationContextRefreshedEventId

func ApplicationContextRefreshedEventId() ApplicationEventId

func ApplicationContextStartedEventId

func ApplicationContextStartedEventId() ApplicationEventId

func ApplicationContextStoppedEventId

func ApplicationContextStoppedEventId() ApplicationEventId

func GetEventId

func GetEventId(eventName string) ApplicationEventId

type ApplicationEventPublisher

type ApplicationEventPublisher interface {
	PublishEvent(context Context, event ApplicationEvent)
}

type ApplicationId

type ApplicationId string

type ApplicationListener

type ApplicationListener interface {
	GetApplicationListenerName() string
	SubscribeEvents() []ApplicationEventId
	OnApplicationEvent(context Context, event ApplicationEvent)
}

type BaseApplicationContext

type BaseApplicationContext struct {
	ConfigurableContextAdapter
	peas.ConfigurablePeaFactory
	// contains filtered or unexported fields
}

func NewBaseApplicationContext

func NewBaseApplicationContext(appId ApplicationId, contextId ContextId, configurableContextAdapter ConfigurableContextAdapter) *BaseApplicationContext

func (*BaseApplicationContext) AddApplicationListener

func (ctx *BaseApplicationContext) AddApplicationListener(listener ApplicationListener)

func (*BaseApplicationContext) Configure

func (ctx *BaseApplicationContext) Configure()

func (*BaseApplicationContext) Get

func (ctx *BaseApplicationContext) Get(key string) interface{}

func (*BaseApplicationContext) GetAppId

func (ctx *BaseApplicationContext) GetAppId() ApplicationId

func (*BaseApplicationContext) GetApplicationListeners

func (ctx *BaseApplicationContext) GetApplicationListeners() []ApplicationListener

func (*BaseApplicationContext) GetApplicationName

func (ctx *BaseApplicationContext) GetApplicationName() string

func (*BaseApplicationContext) GetContextId

func (ctx *BaseApplicationContext) GetContextId() ContextId

func (*BaseApplicationContext) GetEnvironment

func (ctx *BaseApplicationContext) GetEnvironment() core.ConfigurableEnvironment

func (*BaseApplicationContext) GetLogger

func (ctx *BaseApplicationContext) GetLogger() Logger

func (*BaseApplicationContext) GetPeaFactory

func (ctx *BaseApplicationContext) GetPeaFactory() peas.ConfigurablePeaFactory

func (*BaseApplicationContext) GetStartupTimestamp

func (ctx *BaseApplicationContext) GetStartupTimestamp() int64

func (*BaseApplicationContext) PublishEvent

func (ctx *BaseApplicationContext) PublishEvent(event ApplicationEvent)

func (*BaseApplicationContext) Put

func (ctx *BaseApplicationContext) Put(key string, value interface{})

func (*BaseApplicationContext) SetApplicationName

func (ctx *BaseApplicationContext) SetApplicationName(name string)

func (*BaseApplicationContext) SetEnvironment

func (ctx *BaseApplicationContext) SetEnvironment(environment core.ConfigurableEnvironment)

func (*BaseApplicationContext) SetLogger

func (ctx *BaseApplicationContext) SetLogger(logger Logger)

type BootstrapProcessor

type BootstrapProcessor struct {
}

func NewBootstrapProcessor

func NewBootstrapProcessor() BootstrapProcessor

func (BootstrapProcessor) AfterPeaDefinitionRegistryInitialization

func (processor BootstrapProcessor) AfterPeaDefinitionRegistryInitialization(registry peas.PeaDefinitionRegistry)

func (BootstrapProcessor) AfterPeaFactoryInitialization

func (processor BootstrapProcessor) AfterPeaFactoryInitialization(factory peas.ConfigurablePeaFactory)

type ComponentPeaDefinitionScanner

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

func NewComponentPeaDefinitionScanner

func NewComponentPeaDefinitionScanner(registry peas.PeaDefinitionRegistry) ComponentPeaDefinitionScanner

func (ComponentPeaDefinitionScanner) DoScan

func (scanner ComponentPeaDefinitionScanner) DoScan()

type ConfigurableApplicationContext

type ConfigurableApplicationContext interface {
	ApplicationContext
	ConfigurableContext
}

type ConfigurableContext

type ConfigurableContext interface {
	SetLogger(logger Logger)
	GetLogger() Logger
	SetEnvironment(environment core.ConfigurableEnvironment)
	GetEnvironment() core.ConfigurableEnvironment
	GetPeaFactory() peas.ConfigurablePeaFactory
	AddApplicationListener(listener ApplicationListener)
}

type ConfigurableContextAdapter

type ConfigurableContextAdapter interface {
	Configure()
	OnConfigure()
	FinishConfigure()
}

type ConfigurationProperties

type ConfigurationProperties interface {
	GetConfigurationPrefix() string
}

type ConfigurationPropertiesBinder

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

func (ConfigurationPropertiesBinder) Bind

func (binder ConfigurationPropertiesBinder) Bind(target interface{}) error

type ConfigurationPropertiesBindingProcessor

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

func NewConfigurationPropertiesBindingProcessor

func NewConfigurationPropertiesBindingProcessor(env core.Environment, typeConverterService core.TypeConverterService) ConfigurationPropertiesBindingProcessor

func (ConfigurationPropertiesBindingProcessor) AfterPeaInitialization

func (processor ConfigurationPropertiesBindingProcessor) AfterPeaInitialization(peaName string, pea interface{}) (interface{}, error)

func (ConfigurationPropertiesBindingProcessor) BeforePeaInitialization

func (processor ConfigurationPropertiesBindingProcessor) BeforePeaInitialization(peaName string, pea interface{}) (interface{}, error)

type Context

type Context interface {
	GetContextId() ContextId
	Get(key string) interface{}
	Put(key string, value interface{})
}

type ContextId

type ContextId string

type EventListenerProcessor

type EventListenerProcessor struct {
}

func NewEventListenerProcessor

func NewEventListenerProcessor() EventListenerProcessor

func (EventListenerProcessor) AfterPeaDefinitionRegistryInitialization

func (processor EventListenerProcessor) AfterPeaDefinitionRegistryInitialization(registry peas.PeaDefinitionRegistry)

func (EventListenerProcessor) AfterPeaFactoryInitialization

func (processor EventListenerProcessor) AfterPeaFactoryInitialization(factory peas.ConfigurablePeaFactory)

type LogFormatter

type LogFormatter struct {
	logrus.TextFormatter
	// contains filtered or unexported fields
}

func NewLogFormatter

func NewLogFormatter() *LogFormatter

func (*LogFormatter) Format

func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error)

func (*LogFormatter) GetLevelString

func (f *LogFormatter) GetLevelString(entry *logrus.Entry) string

func (*LogFormatter) GetSumContextId

func (f *LogFormatter) GetSumContextId(contextId ContextId) string

type LogLevel

type LogLevel uint32
const (
	PanicLevel LogLevel = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

type Logger

type Logger interface {
	Trace(ctx interface{}, message interface{})
	Debug(ctx interface{}, message interface{})
	Info(ctx interface{}, message interface{})
	Warning(ctx interface{}, message interface{})
	Error(ctx interface{}, message interface{})
	Fatal(ctx interface{}, message interface{})
	Panic(ctx interface{}, message interface{})
	Print(ctx interface{}, message interface{})
	Tracef(ctx interface{}, format string, args ...interface{})
	Debugf(ctx interface{}, format string, args ...interface{})
	Infof(ctx interface{}, format string, args ...interface{})
	Warningf(ctx interface{}, format string, args ...interface{})
	Errorf(ctx interface{}, format string, args ...interface{})
	Fatalf(ctx interface{}, format string, args ...interface{})
	Panicf(ctx interface{}, format string, args ...interface{})
	Printf(ctx interface{}, format string, args ...interface{})
}

type LoggingConfiguration

type LoggingConfiguration interface {
	ApplyLoggingProperties(properties configure.LoggingProperties)
}

type ScannedPeaDefinition

type ScannedPeaDefinition struct {
	*peas.SimplePeaDefinition
	// contains filtered or unexported fields
}

func NewScannedPeaDefinition

func NewScannedPeaDefinition(componentName string, peaType goo.Type) ScannedPeaDefinition

func (ScannedPeaDefinition) GetComponentName

func (definition ScannedPeaDefinition) GetComponentName() string

type ScannedPeaNameGenerator

type ScannedPeaNameGenerator struct {
}

func NewScannedPeaNameGenerator

func NewScannedPeaNameGenerator() ScannedPeaNameGenerator

func (ScannedPeaNameGenerator) GenerateName

func (generator ScannedPeaNameGenerator) GenerateName(peaDefinition peas.PeaDefinition) string

type SimpleApplicationEventBroadcaster

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

func NewSimpleApplicationEventBroadcaster

func NewSimpleApplicationEventBroadcaster() *SimpleApplicationEventBroadcaster

func (*SimpleApplicationEventBroadcaster) BroadcastEvent

func (broadcaster *SimpleApplicationEventBroadcaster) BroadcastEvent(context ApplicationContext, event ApplicationEvent)

func (*SimpleApplicationEventBroadcaster) RegisterApplicationListener

func (broadcaster *SimpleApplicationEventBroadcaster) RegisterApplicationListener(listener ApplicationListener)

func (*SimpleApplicationEventBroadcaster) RemoveAllApplicationListeners

func (broadcaster *SimpleApplicationEventBroadcaster) RemoveAllApplicationListeners()

func (*SimpleApplicationEventBroadcaster) UnregisterApplicationListener

func (broadcaster *SimpleApplicationEventBroadcaster) UnregisterApplicationListener(listener ApplicationListener)

type SimpleLogger

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

func NewSimpleLogger

func NewSimpleLogger() *SimpleLogger

func (*SimpleLogger) ApplyLoggingProperties

func (l *SimpleLogger) ApplyLoggingProperties(properties configure.LoggingProperties)

func (*SimpleLogger) Debug

func (l *SimpleLogger) Debug(ctx interface{}, message interface{})

func (*SimpleLogger) Debugf

func (l *SimpleLogger) Debugf(ctx interface{}, format string, args ...interface{})

func (*SimpleLogger) Error

func (l *SimpleLogger) Error(ctx interface{}, message interface{})

func (*SimpleLogger) Errorf

func (l *SimpleLogger) Errorf(ctx interface{}, format string, args ...interface{})

func (*SimpleLogger) Fatal

func (l *SimpleLogger) Fatal(ctx interface{}, message interface{})

func (*SimpleLogger) Fatalf

func (l *SimpleLogger) Fatalf(ctx interface{}, format string, args ...interface{})

func (*SimpleLogger) Info

func (l *SimpleLogger) Info(ctx interface{}, message interface{})

func (*SimpleLogger) Infof

func (l *SimpleLogger) Infof(ctx interface{}, format string, args ...interface{})

func (*SimpleLogger) Panic

func (l *SimpleLogger) Panic(ctx interface{}, message interface{})

func (*SimpleLogger) Panicf

func (l *SimpleLogger) Panicf(ctx interface{}, format string, args ...interface{})

func (*SimpleLogger) Print

func (l *SimpleLogger) Print(ctx interface{}, message interface{})

func (*SimpleLogger) Printf

func (l *SimpleLogger) Printf(ctx interface{}, format string, args ...interface{})

func (*SimpleLogger) Trace

func (l *SimpleLogger) Trace(ctx interface{}, message interface{})

func (*SimpleLogger) Tracef

func (l *SimpleLogger) Tracef(ctx interface{}, format string, args ...interface{})

func (SimpleLogger) Warning

func (l SimpleLogger) Warning(ctx interface{}, message interface{})

func (SimpleLogger) Warningf

func (l SimpleLogger) Warningf(ctx interface{}, format string, args ...interface{})

Jump to

Keyboard shortcuts

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