mirror of
https://github.com/emo2007/block-accounting.git
synced 2024-11-14 14:16:27 +00:00
59 lines
854 B
Go
59 lines
854 B
Go
|
package service
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"log/slog"
|
||
|
|
||
|
"github.com/emochka2007/block-accounting/internal/interface/rest"
|
||
|
)
|
||
|
|
||
|
type Service interface {
|
||
|
Run(ctx context.Context) error
|
||
|
Stop()
|
||
|
}
|
||
|
|
||
|
type ServiceImpl struct {
|
||
|
log *slog.Logger
|
||
|
rest *rest.Server
|
||
|
}
|
||
|
|
||
|
func NewService(
|
||
|
log *slog.Logger,
|
||
|
rest *rest.Server,
|
||
|
) Service {
|
||
|
return &ServiceImpl{
|
||
|
log: log,
|
||
|
rest: rest,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *ServiceImpl) Run(ctx context.Context) error {
|
||
|
s.log.Info("starting blockd service 0w0")
|
||
|
|
||
|
errch := make(chan error)
|
||
|
|
||
|
defer s.rest.Close()
|
||
|
|
||
|
go func() {
|
||
|
defer func() {
|
||
|
close(errch)
|
||
|
}()
|
||
|
|
||
|
errch <- s.rest.Serve(ctx)
|
||
|
}()
|
||
|
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
s.log.Info("shutting down service")
|
||
|
|
||
|
return nil
|
||
|
case err := <-errch:
|
||
|
return fmt.Errorf("error at service runtime. %w", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *ServiceImpl) Stop() {
|
||
|
s.log.Info(">w< bye bye! :3")
|
||
|
}
|