The Application library provides a framework for managing and running multiple applications with priority-based execution in Go.
import "github.com/getevo/evo/v2/lib/application"The Application library offers the following key features:
- Application Management: Register and manage multiple applications
- Priority-Based Execution: Run applications in order of priority
- Lifecycle Management: Standardized application lifecycle with Register, Router, and WhenReady phases
- Debug Mode: Optional debug logging for application execution
- Application Reloading: Support for reloading applications that implement the ReloadInterface
-
Application: Core interface that applications must implement
type Application interface { Register() error // For application registration Router() error // For setting up routes WhenReady() error // Called when the application is ready Name() string // Returns the application name }
-
PriorityInterface: Optional interface for applications to define their priority
type PriorityInterface interface { Priority() Priority }
-
ReloadInterface: Optional interface for applications that can be reloaded
type ReloadInterface interface { Reload() error }
const (
HIGHEST Priority = 0
HIGH Priority = 1
DEFAULT Priority = 5
LOW Priority = 6
LOWEST Priority = 7
)package myapp
import (
"github.com/getevo/evo/v2/lib/application"
)
type App struct{}
func (app App) Register() error {
// Initialize your application
return nil
}
func (app App) Router() error {
// Set up routes
return nil
}
func (app App) WhenReady() error {
// Execute when all applications are ready
return nil
}
func (app App) Name() string {
return "myapp"
}
// Optional: Implement Priority interface
func (app App) Priority() application.Priority {
return application.DEFAULT
}package main
import (
"github.com/getevo/evo/v2/lib/application"
"myapp"
"anotherapp"
)
func main() {
// Get the application instance
var app = application.GetInstance()
// Register applications
app.Register(myapp.App{}, anotherapp.App{})
// Optional: Enable debug mode
app.Debug(true)
// Run all registered applications
app.Run()
}package myapp
import (
"github.com/getevo/evo/v2/lib/application"
)
type App struct{
// Application state
config Config
}
// Implement Application interface
func (app App) Register() error {
// Initialize
return nil
}
func (app App) Router() error {
return nil
}
func (app App) WhenReady() error {
return nil
}
func (app App) Name() string {
return "myapp"
}
// Implement ReloadInterface
func (app App) Reload() error {
// Reload configuration or state
app.config = loadConfig()
return nil
}
// To reload all applications that implement ReloadInterface:
// application.ReloadAll()The Application library works by:
- Registration: Applications are registered with the App instance
- Sorting: Applications are sorted by priority (if they implement PriorityInterface)
- Execution: The Run method executes each application's lifecycle methods in sequence:
- Register() for all applications
- Router() for all applications
- WhenReady() for all applications
Applications that don't implement PriorityInterface are assigned DEFAULT priority. The library uses reflection to detect and call any OnRegister* methods on applications during the registration phase.