The very basic client app, not adapted for mobiles.
This commit is contained in:
214
client/internal/application/application.go
Normal file
214
client/internal/application/application.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
errApplication = errors.New("application")
|
||||
errNoMainWindow = errors.New("no main window service registered")
|
||||
)
|
||||
|
||||
// Application is a lifecycle controlling structure for application.
|
||||
type Application struct {
|
||||
fyneApp fyne.App
|
||||
services []Service
|
||||
}
|
||||
|
||||
// New creates new instance of lifecycle controlling structure.
|
||||
func New() *Application {
|
||||
appl := &Application{}
|
||||
|
||||
appl.initialize()
|
||||
|
||||
return appl
|
||||
}
|
||||
|
||||
func (a *Application) configure() error {
|
||||
// First iteration - core services.
|
||||
for _, service := range a.services {
|
||||
if !strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Launching configuration procedure for service", "service", service.Name())
|
||||
|
||||
if err := service.Configure(); err != nil {
|
||||
return fmt.Errorf("configure service '%s': %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// Second iteration - rest of the services.
|
||||
for _, service := range a.services {
|
||||
if strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Launching configuration procedure for service", "service", service.Name())
|
||||
|
||||
if err := service.Configure(); err != nil {
|
||||
return fmt.Errorf("configure service '%s': %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Application) connectDependencies() error {
|
||||
// First iteration - core services.
|
||||
for _, service := range a.services {
|
||||
if !strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Connecting dependencies for service.", "service", service.Name())
|
||||
|
||||
if err := service.ConnectDependencies(); err != nil {
|
||||
return fmt.Errorf("connect dependencies for service '%s': %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// Second iteration - rest of the services.
|
||||
for _, service := range a.services {
|
||||
if strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Connecting dependencies for service.", "service", service.Name())
|
||||
|
||||
if err := service.ConnectDependencies(); err != nil {
|
||||
return fmt.Errorf("connect dependencies for service '%s': %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ContextWithTimeout returns context.Context with requested timeout.
|
||||
func (a *Application) ContextWithTimeout(timeout time.Duration) context.Context {
|
||||
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
|
||||
|
||||
// As we do not need to call cancelFunc - make linter happy.
|
||||
// This probably will lead to context leak, so it should be investigated.
|
||||
go func(_ context.CancelFunc) {}(cancelFunc)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (a *Application) initialize() {
|
||||
a.services = make([]Service, 0)
|
||||
|
||||
a.initializeFyne()
|
||||
}
|
||||
|
||||
func (a *Application) launchStartupTasks() error {
|
||||
for _, service := range a.services {
|
||||
if strings.Contains(service.Name(), "mainwindow") {
|
||||
continue
|
||||
}
|
||||
|
||||
if !strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Launching startup tasks for service.", "service", service.Name())
|
||||
|
||||
if err := service.LaunchStartupTasks(); err != nil {
|
||||
return fmt.Errorf("launch startup tasks for core/%s: %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, service := range a.services {
|
||||
if strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Launching startup tasks for service.", "service", service.Name())
|
||||
|
||||
if err := service.LaunchStartupTasks(); err != nil {
|
||||
return fmt.Errorf("launch startup tasks for core/%s: %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
var mainWindowService Service
|
||||
|
||||
for _, srv := range a.services {
|
||||
if srv.Name() == "core/mainwindow" {
|
||||
mainWindowService = srv
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if mainWindowService == nil {
|
||||
return fmt.Errorf("launch startup tasks: %w", errNoMainWindow)
|
||||
}
|
||||
|
||||
if err := mainWindowService.LaunchStartupTasks(); err != nil {
|
||||
return fmt.Errorf("launch startup tasks for %s: %w", mainWindowService.Name(), err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown stops application
|
||||
func (a *Application) Shutdown() error {
|
||||
slog.Info("Stopping pztrn's Bunker...")
|
||||
|
||||
// Сначала тушим фичи.
|
||||
for _, service := range a.services {
|
||||
if !strings.Contains(service.Name(), "features/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Shutting down service.", "service", service.Name())
|
||||
|
||||
if err := service.Shutdown(); err != nil {
|
||||
return fmt.Errorf("shutting down service '%s': %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// Потом тушим ядро.
|
||||
for _, service := range a.services {
|
||||
if !strings.Contains(service.Name(), "core/") {
|
||||
continue
|
||||
}
|
||||
|
||||
slog.Debug("Shutting down service.", "service", service.Name())
|
||||
|
||||
if err := service.Shutdown(); err != nil {
|
||||
return fmt.Errorf("shutting down service '%s': %w", service.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start запускает приложение.
|
||||
func (a *Application) Start() error {
|
||||
if err := a.connectDependencies(); err != nil {
|
||||
return fmt.Errorf("%w: %w", errApplication, err)
|
||||
}
|
||||
|
||||
if err := a.configure(); err != nil {
|
||||
return fmt.Errorf("%w: %w", errApplication, err)
|
||||
}
|
||||
|
||||
if err := a.launchStartupTasks(); err != nil {
|
||||
return fmt.Errorf("%w: %w", errApplication, err)
|
||||
}
|
||||
|
||||
a.fyneApp.Run()
|
||||
|
||||
return nil
|
||||
}
|
17
client/internal/application/fyne.go
Normal file
17
client/internal/application/fyne.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"bunker/commons"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/app"
|
||||
)
|
||||
|
||||
// Fyne возвращает экземпляр Fyne для взаимодействия с ним.
|
||||
func (a *Application) Fyne() fyne.App {
|
||||
return a.fyneApp
|
||||
}
|
||||
|
||||
func (a *Application) initializeFyne() {
|
||||
a.fyneApp = app.NewWithID(commons.ClientAppID)
|
||||
}
|
80
client/internal/application/service.go
Normal file
80
client/internal/application/service.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrServiceAlreadyRegistered returns if trying to register a service with name already taken by other service.
|
||||
ErrServiceAlreadyRegistered = errors.New("service with such name already registered")
|
||||
// ErrServiceNotFound returns if trying to gather service with unknown name.
|
||||
ErrServiceNotFound = errors.New("service with such name wasn't found")
|
||||
)
|
||||
|
||||
// Service is an interface every service should conform to. Specific services will have own interface for
|
||||
// cross-service interation.
|
||||
type Service interface {
|
||||
// Configure configures service. Called after ConnectDependencies and before LaunchStartupTasks.
|
||||
Configure() error
|
||||
// ConnectDependencies gets neccessary dependencies.
|
||||
ConnectDependencies() error
|
||||
// Initialize initializes service's internal state. Called while registering service with Application
|
||||
// lifecycle controller.
|
||||
Initialize() error
|
||||
// Name returns service name.
|
||||
Name() string
|
||||
// LaunchStartupTasks launches tasks on application start. Called after ConnectDependencies and Configure.
|
||||
LaunchStartupTasks() error
|
||||
// Shutdown stops service.
|
||||
Shutdown() error
|
||||
}
|
||||
|
||||
// RegisterService registering service with lifecycle controller for later use in any other service.
|
||||
func (a *Application) RegisterService(srv Service) error {
|
||||
var found bool
|
||||
|
||||
for _, knownService := range a.services {
|
||||
if srv.Name() == knownService.Name() {
|
||||
found = true
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
return fmt.Errorf(
|
||||
"%w: RegisterService: check for service '%s' registration: %w",
|
||||
errApplication,
|
||||
srv.Name(),
|
||||
ErrServiceAlreadyRegistered,
|
||||
)
|
||||
}
|
||||
|
||||
if err := srv.Initialize(); err != nil {
|
||||
return fmt.Errorf("%w: RegisterService: initialize service '%s': %w", errApplication, srv.Name(), err)
|
||||
}
|
||||
|
||||
a.services = append(a.services, srv)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Service returns requested service.
|
||||
func (a *Application) Service(name string) Service {
|
||||
var srv Service
|
||||
|
||||
for _, knownService := range a.services {
|
||||
if knownService.Name() == name {
|
||||
srv = knownService
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if srv == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
Reference in New Issue
Block a user