micw/server/server.go

52 lines
950 B
Go
Raw Normal View History

2024-11-13 21:10:13 +00:00
package server
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"mic-wallet/server/config"
2024-11-24 14:04:52 +00:00
"mic-wallet/server/core"
2024-11-13 21:10:13 +00:00
"mic-wallet/server/repository"
"net"
"net/http"
)
2024-11-24 14:04:52 +00:00
type serverTask func() error
2024-11-13 21:10:13 +00:00
type Server struct {
2024-11-24 14:04:52 +00:00
Cfg *config.Processed
DB *pgxpool.Pool
Repo *repository.Repository
BlocksCommiter *core.BlocksCommiter
BlocksCreator *core.BlocksCreator
HttpListener net.Listener
HttpsListener net.Listener
GrpcListener net.Listener
Mux *http.ServeMux
ErrChan chan error
2024-11-13 21:10:13 +00:00
}
func New(cfg *config.Processed) *Server {
2024-11-24 14:04:52 +00:00
return &Server{
Cfg: cfg,
ErrChan: make(chan error),
}
2024-11-13 21:10:13 +00:00
}
2024-11-24 14:04:52 +00:00
func (s *Server) execTasks(ctx context.Context, tasks []serverTask) error {
2024-11-13 21:10:13 +00:00
for _, task := range tasks {
go func() {
2024-11-24 14:04:52 +00:00
s.ErrChan <- task()
2024-11-13 21:10:13 +00:00
}()
}
for {
select {
2024-11-24 14:04:52 +00:00
case err := <-s.ErrChan:
2024-11-13 21:10:13 +00:00
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}